Beruflich Dokumente
Kultur Dokumente
Unit 03
Section Goals
•To learn:
– The basic building blocks of Java
•Identifiers cannot:
– Begin with a digit
– Be the same as a reserved word.
An_Identifier
a_2nd_Identifier
An-Identifier
2nd_Identifier
Go2 goto
$10 10$
Java is Case-Sensitive
•Yourname, yourname, yourName, YourName
– These are four different identifiers
•Conventions:
– Package: all lower case
•theexample
•Keywords
–abstract assert boolean break byte case
catch char class continue default do double
else extends final finally float for if
implements import instanceof int interface
long native new package private protected
public return short static strictfp super
switch synchronized this throw throws
transient try void volatile while
•Initialized to zero
Categories:
Size: 1 byte
a. integer 1. byte
Range: -27 27 - 1
Size: 8 bytes
4. long
Range: -263 263 - 1
Primitives: Floating Points
•“General” numbers
– Can have fractional parts
•Initialized to zero
Categories:
a. integer
Size: 4 bytes
b. floating point 1. float Range: ±1.4 x 10-45 ±3.4 x 1038
c. character
Size: 8 bytes
d. boolean 2. double
Range: ±4.9 x 10-324 ±1.8 x 10308
Primitives: Characters
•Char is any unsigned Unicode character
Categories:
a. integer
b. floating point
Size: 2 bytes
c. character
c. character char
Range: \u0000 \uFFFF
d. boolean
Primitives: Booleans
•boolean values are distinct in Java
– Can only have a true or false value
– An int value can NOT be used in place of a boolean
•Initialized to false
Categories:
a. integer
b. floating point
c. character
c. character
Size: 1 byte
d. boolean boolean Range: true | false
Primitive Literals
•A literal is a value
0X1a | 0X
0x1a | 0x1A | 0X 0X1A
Primitive Literals: Floating Point
•Float literals end with an f (or F):
– 7.1f
int d; double f;
int a, b; short e; long g;
short c; e = (short)d; f = g;
a = b + c; g = f; //error
Implicit vs. Explicit Casting
•Implicit casting is automatic when no loss of information is
possible.
– byte short int long float double
•Examples:
int i, j;
i = 0;
int k=i+1;
float x=1.0, y=2.0;
System.out.println(i); //prints 0
System.out.println(k); //prints 1
System.out.println(j); //compile error
Arrays
•An array holds several values of the same type
•Examples:
int MAX = 5;
boolean bit[] = new boolean[MAX];
float[] value = new float[2*3];
int[] number = {10, 9, 8, 7, 6};
System.out.println(bit[0]); // prints “false”
System.out.println(value[3]); // prints “0.0”
System.out.println(number[1]); // prints “9”
Operators and Precedence
•Operators are the “glue” of expressions
2. /* Everything between
is a comment */
System.out.println(
“This is part of the same line”);