1 / 45

308-203A Introduction to Computing II Lecture 1: Java Review

308-203A Introduction to Computing II Lecture 1: Java Review. Fall Session 2000. Primitive data types. Size 8 bit 16 bit 16 bit 32 bit 64 bit 32 bit 64 bit “1 bit”. Type byte char short int long float double boolean. Values [-128, 127] [0, 65,536] [-32,768 , 32,767]

reina
Download Presentation

308-203A Introduction to Computing II Lecture 1: Java Review

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 308-203AIntroduction to Computing IILecture 1: Java Review Fall Session 2000

  2. Primitive data types Size 8 bit 16 bit 16 bit 32 bit 64 bit 32 bit 64 bit “1 bit” 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] [1.4023984e-45 , 3.40282347e+38] [4.94065645841246544e-324, 1.79769313486231570e+308] { false, true}

  3. Variable declarations <type> <var-name> [ = <initializer>] ; Examples: char x = ‘a’; int i, j = 5, k; float pi = 3.14159;

  4. x ‘a’ Variable declarations <type> <var-name> [ = <initializer>] ; Examples: char x = ‘a’; int i, j = 5, k; float pi = 3.14159;

  5. x i j k ‘a’ ?? 5 ?? Variable declarations <type> <var-name> [ = <initializer>] ; Examples: char x = ‘a’; int i, j = 5, k; float pi = 3.14159;

  6. x i j k ‘a’ ?? 5 ?? pi 3.14159 Variable declarations <type> <var-name> [ = <initializer>] ; Examples: char x = ‘a’; int i, j = 5, k; float pi = 3.14159;

  7. Assignments and expressions <var-name> = <expr> ; Examples: double pi, radius, circ; ... circ = 2 * pi * radius ; int x; boolean xGreaterThanFive; ... xGreaterThanFive = (x > 5);

  8. pi radius 1.0 3.14159 circ ???? Assignments and expressions <var-name> = <expr> ; Examples: double pi, radius, circ; ... circ = 2 * pi * radius ; int x; boolean xGreaterThanFive; ... xGreaterThanFive = (x > 5);

  9. pi radius 1.0 3.14159 circ 6.28318 Assignments and expressions <var-name> = <expr> ; Examples: double pi, radius, circ; ... circ = 2 * pi * radius ; int x; boolean xGreaterThanFive; ... xGreaterThanFive = (x > 5); =

  10. x 10 xGreaterThanFive ???? Assignments and expressions <var-name> = <expr> ; Examples: double pi, radius, circ; ... circ = 2 * pi * radius ; int x; boolean xGreaterThanFive; ... xGreaterThanFive = (x > 5);

  11. x 10 xGreaterThanFive true Assignments and expressions <var-name> = <expr> ; Examples: double pi, radius, circ; ... circ = 2 * pi * radius ; int x; boolean xGreaterThanFive; ... xGreaterThanFive = (x > 5); =

  12. Type-casting (<type>) <expr> 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

  13. Strings and Arrays Examples: String str = “Hi there”; char myArr[3]; myArr[0] = ‘c’; myArr[1] = ‘a’; myArr[2] = ‘t’;

  14. Strings and Arrays Examples: String str = “Hi there”; char myArr[3]; myArr[0] = ‘c’; myArr[1] = ‘a’; myArr[2] = ‘t’; str “Hi there”

  15. 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] ??

  16. 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] ‘c’ [1] ‘a’ [2] ‘t’

  17. A simple program public class hello { public static void main(String args[ ]) { System.out.println(“Hello world”); } }

  18. Making decisions (If only the only “if ” in “life”were between the “l” and the “e”) • if (<boolean>) then <statement>; Example int x = 10; if (x > 5) { System.out.println(“x is greater than 5”); }

  19. Making decisions (If only the only “if ” in “life”were between the “l” and the “e”) • if (<boolean>) then <statement>; Example int x = 10; boolean xGreaterThanFive = (x > 5); if (xGreaterThanFive) { System.out.println(“x is greater than 5”); }

  20. Making decisions • <boolean> ? <expr1> : <expr2> Shorthand for if x = (condition ? a : b); if (condition) x = a ; else x = b ;

  21. Making decisions • <boolean> ? <expr1> : <expr2> Example int x = 10; System.out.println( (x>5) ? “x is greater than 5” : “x is less than 6”);

  22. Making decisions • switch (<expr>) • { • case <constant>: <body_1>; • case <constant>: <body_2>; • … • default: <body_d> • }

  23. 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”); }

  24. Loops • while (<condition>) <body>; • Example: • int j = 0; • while (j < 3) • { • System.out.println(j); • j++; • } Output: 0 1 2

  25. Loops • do <body> while (<condition>); • Example: • int j = 0; • do • { • System.out.println(j); • j++; • } • while (j < 3) ; Output: 0 1 2

  26. Loops • for (<initializer>; <condition> ; <incr>) <body>; • Example: • for (int j = 0; j < 3; j++) System.out.println(j); • Output: • 0 • 1 • 2

  27. 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”); } }

  28. Reference variables <type> <var-name>; • The variable “points” to some data somewhere • <type> is the kind of object being referenced • Strings and arrays are treated as references • (as are user-defined types) • Special value: null

  29. Reference variables y x Example: Thing x = theBlob; Thing y = null; y = x; boolean same = (x == y); null theBlob

  30. same ??? Reference variables y x Example: Thing x = theBlob; Thing y = null; y = x; boolean same = (x == y); theBlob

  31. same true Reference variables y x Example: Thing x = theBlob; Thing y = null; y = x; boolean same = (x == y); theBlob

  32. Strings are references, too Example: String a = “YO!”; String b = “YO!”; boolean same = (a == b);

  33. Strings are references, too “YO!” a Example: String a = “YO!”; String b = “YO!”; boolean same = (a == b);

  34. Strings are references, too “YO!” a Example: String a = “YO!”; String b = “YO!”; boolean same = (a == b); “YO!” b

  35. Strings are references, too “YO!” a Example: String a = “YO!”; String b = “YO!”; boolean same = (a == b); “YO!” b same false!!

  36. Strings are references, too “YO!” a Example: String a = “YO!”; String b = “YO!”; boolean same = a.equals(b); “YO!” b same true

  37. Arrays are references, too Example: int[ ] foo = new int[2]; int[ ] bar = foo; foo[0] = 523; bar[0] = 325; System.out.println(foo[0]);

  38. Arrays are references, too foo [0] ?? Example: int[ ] foo = new int[2]; int[ ] bar = foo; foo[0] = 523; bar[0] = 325; System.out.println(foo[0]); [1] ??

  39. Arrays are references, too foo [0] ?? Example: int[ ] foo = new int[2]; int[ ] bar = foo; foo[0] = 523; bar[0] = 325; System.out.println(foo[0]); [1] ?? bar

  40. Arrays are references, too foo [0] 523 Example: int[ ] foo = new int[2]; int[ ] bar = foo; foo[0] = 523; bar[0] = 325; System.out.println(foo[0]); [1] ?? bar

  41. Arrays are references, too foo [0] 325 Example: int[ ] foo = new int[2]; int[ ] bar = foo; foo[0] = 523; bar[0] = 325; System.out.println(foo[0]); [1] ?? bar

  42. Arrays are references, too foo [0] 325 Example: int[ ] foo = new int[2]; int[ ] bar = foo; foo[0] = 523; bar[0] = 325; System.out.println(foo[0]); [1] ?? bar Output: 325!!!

  43. Arrays are references, too foo [0] 523 Example: int[ ] foo = new int[2]; int[ ] bar = (int[ ]) foo.clone(); foo[0] = 523; bar[0] = 325; System.out.println(foo[0]); [1] ?? bar [0] 523 [1] ??

  44. Arrays are references, too foo [0] 523 Example: int[ ] foo = new int[2]; int[ ] bar = (int[ ]) foo.clone(); foo[0] = 523; bar[0] = 325; System.out.println(foo[0]); [1] ?? bar [0] 325 [1] ?? Output: 523

  45. Any Questions?

More Related