Primitive Types n Four integer types byte short
Primitive Types n Four integer types: Ø Ø byte short int (most common) long n Two floating-point types: Ø float Ø double (most common) n One character type: Ø char n One boolean type: Ø boolean 1
Primitive Types, cont. 2
Examples of Primitive Values n Integer values: 0 -1 365 12000 n Floating-point values: 0. 99 -22. 8 3. 14159 5. 0 n Character values: `a` `A` `#` ` ` n Boolean values: true false 3
Motivation for Primitive Types n Why are there several different integer types? Ø storage space Ø operator efficiency n More generally, why are there different types at all? Ø reflects how people understand different kinds of data, e. g. , letter vs. numeric grades Ø makes code more readable (which is a big deal) Ø helps prevent programmer errors 4
Literals n Values such as 2, 3. 7, or ’y’ are called constants or literals. n Integer literals can be preceded by a + or - sign, but cannot contain commas. n Every integer literal is either of type int or type long. n The type of an integer literal can be determined by…looking at it! 5
Integer Literal Type n An integer literal is of type long if it is suffixed with an letter L or l; otherwise it is of type int. Ø note that capital L is preferred n Integer literals of type long: Ø 2 L Ø -372 L Ø 2147483648 l 777 L 1996 L 0 l n Integer literals of type int: Ø 2 Ø -372 Ø 2147483648 777 1996 0 6
Floating Point Literals n Floating point literals: Ø Can be preceded by a + or - sign, but cannot contain commas Ø Can be specified in (a type of) scientific notation n Examples: Ø 865000000. 0 can also be written as 8. 65 e 8 Ø 0. 000483 can also be written as 4. 83 e-4 n The number in front of the “e” does not need to contain a decimal point, e. g. 4 e-4 7
Floating Point Literal Type n Every floating point literal is either of type float or type double. n The type of a floating point literal can be determined by…looking at it! n An floating point literal is of type float if it is suffixed with an letter F or f; otherwise it is of type double. n Floating point literals of type float: Ø 2. 5 F Ø 8. 65 e 8 f 0. 0 f Ø 3 f Ø -16 F +35. 4 f 4 e-4 F -16. 0 F 8
Assignment Compatibilities n Java is said to be strongly typed, which means that there are limitations on mixing variables and values in expressions and assignments. n What is the type of the LHS and RHS for each statement? int x = 0; long y = 0; float z = 0. 0 f; int w; w x x y z y = = = x; y; z; z; 3. 6; 25; // // // legal; what does it do? illegal (3. 6 is of type double) legal, but…why? 9
Assignment Compatibilities n Sometimes automatic conversions between types do take place: short s; int x; s = 83; x = s; double. Variable; int. Variable = 7; double. Variable = int. Variable; 10
Assignment Compatibilities, cont. n In general, a value (or expression) of one numeric type can be assigned to a variable of any type further to the right, as follows: byte --> short --> int --> long --> float --> double but not to a variable of any type further to the left. n Makes sense intuitively because, for example, any legal byte value is a legal short value. n On the other hand, many legal short values are not legal byte values. 11
Assignment Compatibilities, cont. n Example – all of the following are legal, and will compile: byte b = 0; short s; int i; long l; float f; double d; s = b; i = b; l = i; f = l; // This one is interesting, why? d = f; b = 10; 12
Assignment Compatibilities, cont. n Example – NONE (except the first) of the following will compile: byte b; short s; int i; long l; float f; double d; d = 1. 0; // This one compiles f = d; l = f; i = l; s = i; b = s; 13
Type Casting n A type cast creates a value in a new type from an original type. n A type cast can be used to force an assignment when otherwise it would be illegal (thereby over-riding the compiler, in a sense). n Example: double distance; distance = 9. 0; int points; points = distance; // illegal points = (int)distance; // legal 14
Type Casting, cont. n The value of (int)distance is 9, but the value of distance, both before and after the cast, is 9. 0. n The type of distance does NOT change and remains double. n What happens if distance contains 9. 7? Ø Any value right of the decimal point is truncated (as oppossed to rounded). 15
Type Casting, cont. n A cast can be performed from any primitive type to any other primitive type, however… n Remember to “cast with care, ” because the results can be unpredictable. int x; long z = ? ; // ? Could be a computation or input x = (int)z; 16
Arithmetic Operations n Arithmetic expressions: Ø Formed using the +, -, *, / and % operators Ø Operators have operands, which are literals, variables or sub-expressions. n Expressions with two or more operators can be viewed as a series of steps, each involving only two operands. Ø The result of one step produces an operand which is used in the next step. Ø Java is left-associative. Ø Most of the basic rules of precedence apply. n Example: int x = 0, y = 50, z = 20; double balance = 50. 25, rate = 0. 05; x = x + y + z; balance = balance + balance * rate; balance = (balance + balance) * rate; 17
Expression Type n An arithmetic expression can have operands of different numeric types. Ø x + (y * z) / w Ø Note that this does not contradict our rules for assignment. n Every arithmetic expression has a (resulting) type. Ø k = x + (y * z) / w; // Does this compile? n Given an arithmetic expression: Ø If any operand in the expression is of type double, then the expression has type double. Ø Otherwise, if any operand in the expression is of type float, then the expression has type float. Ø Otherwise, if any operand in the expression is of type long, then the expression has type long. Ø Otherwise the expression has type int. 18
Expression Type, cont. n Example: int hours. Worked double pay. Rate = 40; = 8. 25; double total. Pay; Then the expression in the assignment: total. Pay = hours. Worked * pay. Rate is a double with a value of 500. 0. 19
Operators with integer and floating point numbers n See the program: Ø http: //www. cs. fit. edu/~pbernhar/teaching/cse 1001/expressions Ø http: //www. cs. fit. edu/~pbernhar/teaching/cse 1001/integral. Conversion 20
- Slides: 20