A Introduction to Computing II Lecture 1: Java Review Fall Session 2000
Primitive data types Type byte char short int long float double boolean Values [-128, 127] [0, 65,536] [-32,768, 32,767] [-2,147,483,648, 2,147,483,647] [-9,223,372,036,854,775,807, 9,223,372,036,854,775,806] [ e-45, e+38] [ e-324, e+308] { false, true} Size 8 bit 16 bit 32 bit 64 bit 32 bit 64 bit “1 bit”
Variable declarations [ = ] ; Examples: char x = ‘a’; int i, j = 5, k; float pi = ;
Variable declarations [ = ] ; Examples: char x = ‘a’; int i, j = 5, k; float pi = ; ‘a’ x
Variable declarations [ = ] ; Examples: char x = ‘a’; int i, j = 5, k; float pi = ; ‘a’ x ?? i 5 j k
Variable declarations [ = ] ; Examples: char x = ‘a’; int i, j = 5, k; float pi = ; ‘a’ x ?? i 5 j k pi
Assignments and expressions = ; Examples: double pi, radius, circ;... circ = 2 * pi * radius ; int x; boolean xGreaterThanFive;... xGreaterThanFive = (x > 5);
Assignments and expressions = ; Examples: double pi, radius, circ;... circ = 2 * pi * radius ; int x; boolean xGreaterThanFive;... xGreaterThanFive = (x > 5); pi 1.0 radius circ ????
Assignments and expressions = ; Examples: double pi, radius, circ;... circ = 2 * pi * radius ; int x; boolean xGreaterThanFive;... xGreaterThanFive = (x > 5); pi 1.0 radius circ =
Assignments and expressions = ; Examples: double pi, radius, circ;... circ = 2 * pi * radius ; int x; boolean xGreaterThanFive;... xGreaterThanFive = (x > 5); 10 x xGreaterThanFive ????
Assignments and expressions = ; Examples: double pi, radius, circ;... circ = 2 * pi * radius ; int x; boolean xGreaterThanFive;... xGreaterThanFive = (x > 5); 10 x xGreaterThanFive true =
Type-casting ( ) Converts one type to another. Examples: float f = 1.5; int j = (int) f; // j == 1 short s = 200; double d = (double) s; // d == 2.0e2
Strings and Arrays Examples: String str = “Hi there”; char myArr[3]; myArr[0] = ‘c’; myArr[1] = ‘a’; myArr[2] = ‘t’;
Strings and Arrays Examples: String str = “Hi there”; char myArr[3]; myArr[0] = ‘c’; myArr[1] = ‘a’; myArr[2] = ‘t’; str “Hi there”
Strings and Arrays Examples: String str = “Hi there”; char myArr[3]; myArr[0] = ‘c’; myArr[1] = ‘a’; myArr[2] = ‘t’; str “Hi there” myArr ?? [0] [1] [2]
Strings and Arrays Examples: String str = “Hi there”; char myArr[3]; myArr[0] = ‘c’; myArr[1] = ‘a’; myArr[2] = ‘t’; str “Hi there” myArr ‘c’ ‘a’ ‘t’ [0] [1] [2]
A simple program public class hello { public static void main(String args[ ]) { System.out.println(“Hello world”); }
Making decisions (If only the only “if ” in “life” were between the “l” and the “e”) if ( ) then ; Example int x = 10; if (x > 5) { System.out.println(“x is greater than 5”); }
Making decisions (If only the only “if ” in “life” were between the “l” and the “e”) if ( ) then ; Example int x = 10; boolean xGreaterThanFive = (x > 5); if (xGreaterThanFive) { System.out.println(“x is greater than 5”); }
Making decisions ? : Shorthand for if x = (condition ? a : b); if (condition) x = a ; else x = b ;
Making decisions ? : Example int x = 10; System.out.println( (x>5) ? “x is greater than 5” : “x is less than 6”);
Making decisions switch ( ) { case : ; … default: }
Making decisions Example: char myChar = getAChar(); switch (myChar) { case ‘a’: option_a(); break; case ‘b’: option_b(); break; case ‘c’: option_c(); break; default: System.out.println(“Unknown option”); }
Loops while ( ) ; Example: int j = 0; while (j < 3) { System.out.println(j); j++; } Output: 0 1 2
Loops do while ( ); Example: int j = 0; do { System.out.println(j); j++; } while (j < 3) ; Output: 0 1 2
Loops for ( ; ; ) ; Example: for (int j = 0; j < 3; j++) System.out.println(j); Output: 0 1 2
Another simple program public class reverse { public static void main(String args[ ]) { for (int i = args.length-1; i >= 0; i--) System.out.println(args[i] + “ ”); System.out.println(“\n”); }
Reference variables The variable “points” to some data somewhere is the kind of object being referenced Strings and arrays are treated as references (as are user-defined types) Special value: null ;
Reference variables Example: Thing x = theBlob; Thing y = null; y = x; boolean same = (x == y); null x y theBlob
Reference variables Example: Thing x = theBlob; Thing y = null; y = x; boolean same = (x == y); x y theBlob ??? same
Reference variables Example: Thing x = theBlob; Thing y = null; y = x; boolean same = (x == y); x y theBlob true same
Strings are references, too Example: String a = “YO!”; String b = “YO!”; boolean same = (a == b);
Strings are references, too Example: String a = “YO!”; String b = “YO!”; boolean same = (a == b); a “YO!”
Strings are references, too Example: String a = “YO!”; String b = “YO!”; boolean same = (a == b); a “YO!” b
Strings are references, too Example: String a = “YO!”; String b = “YO!”; boolean same = (a == b); a “YO!” b false!! same
Strings are references, too Example: String a = “YO!”; String b = “YO!”; boolean same = a.equals(b); a “YO!” b true same
Arrays are references, too Example: int[ ] foo = new int[2]; int[ ] bar = foo; foo[0] = 523; bar[0] = 325; System.out.println(foo[0]);
Arrays are references, too Example: int[ ] foo = new int[2]; int[ ] bar = foo; foo[0] = 523; bar[0] = 325; System.out.println(foo[0]); foo ?? [0] [1]
Arrays are references, too Example: int[ ] foo = new int[2]; int[ ] bar = foo; foo[0] = 523; bar[0] = 325; System.out.println(foo[0]); foo ?? [0] [1] bar
Arrays are references, too Example: int[ ] foo = new int[2]; int[ ] bar = foo; foo[0] = 523; bar[0] = 325; System.out.println(foo[0]); foo 523 ?? [0] [1] bar
Arrays are references, too Example: int[ ] foo = new int[2]; int[ ] bar = foo; foo[0] = 523; bar[0] = 325; System.out.println(foo[0]); foo 325 ?? [0] [1] bar
Arrays are references, too Example: int[ ] foo = new int[2]; int[ ] bar = foo; foo[0] = 523; bar[0] = 325; System.out.println(foo[0]); foo 325 ?? [0] [1] bar Output: 325!!!
Arrays are references, too Example: int[ ] foo = new int[2]; int[ ] bar = (int[ ]) foo.clone(); foo[0] = 523; bar[0] = 325; System.out.println(foo[0]); foo 523 ?? [0] [1] bar 523 ?? [0] [1]
Arrays are references, too Example: int[ ] foo = new int[2]; int[ ] bar = (int[ ]) foo.clone(); foo[0] = 523; bar[0] = 325; System.out.println(foo[0]); foo 523 ?? [0] [1] bar Output: ?? [0] [1]
Any Questions?