Presentation is loading. Please wait.

Presentation is loading. Please wait.

Primitive Types Four integer types: Two floating-point types:

Similar presentations


Presentation on theme: "Primitive Types Four integer types: Two floating-point types:"— Presentation transcript:

1 Primitive Types Four integer types: Two floating-point types:
byte short int (most common) long Two floating-point types: float double (most common) One character type: char One boolean type: boolean

2 Primitive Types, cont.

3 Examples of Primitive Values
Integer values: Floating-point values: Character values: `a` `A` `#` ` ` Boolean values: true false

4 Motivation for Primitive Types
Why are there several different integer types? storage space operator efficiency More generally, why are there different types at all? reflects how people understand different kinds of data, e.g., letter vs. numeric grades. helps prevent programmer errors.

5 Literals Values such as 2, 3.7, or ’y’ are called constants or literals. Integer literals can be preceded by a + or - sign, but cannot contain commas. There are two distinct properties that every integer literal has: format – either decimal, hexadecimal or octal, and type – either long or int Both the format and the type of an integer literal can be determined by…looking at it!

6 Integer Literal Type Now lets talk about the type of an integer literal… 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 Integer literals of type long: 2L L 0372L 0x l 0xDadaCafeL 0xC0B0L 1996L L 0x00FF00FFL 0l

7 Integer Literal Type See the program:

8 Floating Point Literals
Just like with integer literals… Floating point literals can also be preceded by a + or - sign, but cannot contain commas. There are two distinct properties that every floating point literal has: format – either fully expanded notation or scientific notation type – either float or double Both the format and the type of a floating point literal can be determined by…looking at it!

9 Floating Point Literal Type
Now lets talk about the type of a floating point literal… An floating point literal is of type float if it is suffixed with an letter F or f; otherwise it is of type double. Floating point literals of type float: 2.5F 0.0f 8.65e8f 4e-4F 3f +35.4f -16F

10 Assignment Compatibilities
Java is said to be strongly typed, which means that there are limitations on mixing variables and values in expressions and assignments. int x = 0; long y = 0; float z = 0.0f; x = y; // illegal x = z; // illegal y = z; // illegal z = 3.6; // illegal (3.6 is of type double) y = 25; // legal, but…why?

11 Assignment Compatibilities
Sometimes automatic conversions between types do take place: short s; int x; s = 83; x = s; double doubleVariable; int intVariable; intVariable = 7; doubleVariable = intVariable;

12 Assignment Compatibilities, cont.
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. Makes sense intuitively because, for example, any legal byte value is a legal short value. On the other hand, many legal short values are not legal byte values.

13 Assignment Compatibilities, cont.
Example – all of the following are legal, and will compile: byte b; 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;

14 Assignment Compatibilities, cont.
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; f = d; l = f; i = l; s = i; b = s;

15 Type Casting A type cast creates a value in a new type from an original type. A type cast can be used to force an assignment when otherwise it would be illegal (thereby over-riding the compiler, in a sense). Example: double distance; distance = 9.0; int points; points = distance; // illegal points = (int)distance; // legal

16 Type Casting, cont. The value of (int)distance is 9, but the value of distance, both before and after the cast, is 9.0. The type of distance does NOT change and remains double. What happens if distance contains 9.7? Any value right of the decimal point is truncated (as oppossed to rounded). Remember to “cast with care,” because the results can be unpredictable. int x; long z = ?; x = (int)z;

17 Characters as Integers
Like everything else, each character is represented by a binary sequence. The binary sequence corresponding to a character is a positive integer. Which integer corresponds to each character is dictated by a standardized character encoding. Each character is assigned a unique integer code The codes are different for upper and lower case letters, e.g., 97 may be the integer value for ‘a’ and 65 for ‘A’ Some characters are printable, others are not Why should different computers and languages use the same code? ASCII and Unicode are the most common character codes.

18 Unicode Character Set Most programming languages use the ASCII character encoding. American Standard Code for Information Interchange (ASCII) (only) encodes characters from the north American keyboard uses one byte of storage Java uses the Unicode character encoding. The Unicode character set: uses two bytes of storage includes many international character sets (in contrast to ASCII) codes characters from the North American keyboard the same way that ASCII does Hey, lets google ASCII!!!!

19 Assigning a char to an int
A value of type char can be assigned to a variable of type int to obtain its Unicode value. Example: char answer = ’y’; System.out.println(answer); System.out.println((int)answer); >y >121 See the program at:

20 Arithmetic Operations
Arithmetic expressions: Formed using the +, -, *, / and % operators Operators have operands, which are literals, variables or sub-expressions. 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. Example: int x = 0, y = 50, z = 20; double balance = 50.25, rate = 0.05; x = x + y + z; balance = balance + (balance * rate)

21 Expression Type An arithmetic expression can have operands of different numeric types. x + (y * z) / w Note that this does not contradict our rules for assignment. Every arithmetic expression has a (resulting) type. k = x + (y * z) / w; // Does this compile? 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.

22 Expression Type, cont. Example: int hoursWorked = 40;
double payRate = 8.25; double totalPay; Then the expression in the assignment: totalPay = hoursWorked * payRate is a double with a value of

23 Operators with integer and floating point numbers
See the program:


Download ppt "Primitive Types Four integer types: Two floating-point types:"

Similar presentations


Ads by Google