Presentation is loading. Please wait.

Presentation is loading. Please wait.

CompSci 6 5.1 Primitive Types  Primitive Types (base types)  Built-in data types; native to most hardware  Note: not objects (will use mostly first.

Similar presentations


Presentation on theme: "CompSci 6 5.1 Primitive Types  Primitive Types (base types)  Built-in data types; native to most hardware  Note: not objects (will use mostly first."— Presentation transcript:

1 CompSci 6 5.1 Primitive Types  Primitive Types (base types)  Built-in data types; native to most hardware  Note: not objects (will use mostly first four) boolean (1bit) int (4 bytes) double (8 bytes) char (2 bytes)  Constants/Literals (by example): boolean f = false; int i = 32769; double d = 0.333333; char c = ’x’; byte (1 byte = 8 bits) short (2 bytes) long (8 bytes) float (4 bytes) byte b = 33; short s = 21; long l = 289L; float = 3.141592F;

2 CompSci 6 5.2 Named Constants  Probably should name most constants  Should have no “Magic Numbers” in program  By convention, use all caps in the identifier  Use the final keyword  Keeps you from accidentally changing value; final boolean CORRECT = true; final int SQUAD_SIZE = 12; final double MIN_GPA = 2.5; final char FAIL = ’F’; final String AUTHOR = ”Dietolf Ramm”;  Note Constants provided by Java: (from API) Math.PI, Math.E, Integer.MAX_VALUE

3 CompSci 6 5.3 Arithmetic Operators  Arithmetic o +, -, *, /, % ( remainder or mod )  Increment/Decrement o e.g., k++, k--, ++k, --k  Logical (results in boolean value) o =, > o Used only for numbers except == and != o For boolean only: !, &&, ||  String Concatenation o “I’m “ + 19 + “ years old and live in “ + city  Assignment o variable = expression o variable op= expression o ( shorthand for: variable = variable op expression )

4 CompSci 6 5.4 Operators  Arithmetic  +, -, *, /, % ( remainder or mod )  Work for both integers and reals  Except watch / and % for integers o What is 13/5 ? 13%5 ? 3/5 ? 3%5 ?  Increment/Decrement  e.g., k++, k–- o Written as k++; not k = k++; !!  Can write code like k = 3 * p++ - m / 5;  Usually not a good idea : confuses. Use a separate line for increment of p.

5 CompSci 6 5.5 Operator Precedence  Determines order of operation  For arithmetic, matches grammar school learning o multiplication and division before addition and subtraction o what is the value of 4.0 + 5.0 / 9.0 * 27.0 ? o (what is the value for the integer version?)  Parentheses override precedence rules (and don’t do harm when not needed)  For equal precedence (e.g., * and / ) work strictly left to right except for assignment and prefix operations which work right to left  Some of the operators, grouped from high to low : ++ --, * / %, + -, >=, == !=, &&, ||, = op=

6 CompSci 6 5.6 Operators  Combining Assignment and Arithmetic  variable op= expression  ( shorthand for: variable = variable op expression )  Thus the following lines contain equivalent statements k = k – 1; k -= 1; k--; q = q * r; q *= r; s = s / 2; s /= 2;

7 CompSci 6 5.7 Assignment  (Familiar by now) x = 13.3; k = k + 7; area = 2.0 * Math.PI * radius * radius;  Vocalize as “gets” or “becomes”  Don’t use “equals”: Not Equality  Casting: (type) int k = 3; double t; t = k; // OK, No information lost int m; double s = 3.5; m = s; // ILLEGAL, information lost (accidentally?) m = (int) s; // OK, information lost – intent shown

8 CompSci 6 5.8 Casting  Implicit Casting  Types on two sides of operator differ ( int and double ) or ( double and int )  Promotes calculation double  Can cascade down the expression  Compare the following two lines; tempC = 5 / 9 * (tempF + 40) – 40; tempF = 9.0 / 5 * (tempC + 40) – 40;  Try for 212 O F or 100 O C

9 CompSci 6 5.9 The Math class  Contains useful static methods  static means the method does not operate on an object  Use class name ( Math ) rather than object name when using the dot operator.  Contains common math functions  Look up Math class in Java API  Use of some common methods double x = Math.sqrt(y) * Math.cos(theta); long j = Math.round(3.8 * Math.pow(h, n));


Download ppt "CompSci 6 5.1 Primitive Types  Primitive Types (base types)  Built-in data types; native to most hardware  Note: not objects (will use mostly first."

Similar presentations


Ads by Google