Variables, Types, and Operators in Java

Variables, Types, and Operators in Java

This is an introduction to variables, types, and operators in Java as used in my AP Computer Science classes

D1522fd426dafd8e3d085e97f63220db?s=128

Cameron Lane

August 26, 2013
Tweet

Transcript

  1. Variables, Types, & Operators in Java AP Computer Science Holy

    Innocents’ Episcopal School
  2. HelloWorld.java 1 /** 2 * HelloWorld.java 3 */ 4 5

    /** 6 * @author cameronlane 7 * 8 */ 9 public class HelloWorld { 10 11 ! /** 12 ! * @param args 13 ! */ 14 ! public static void main(String[] args) { 15 ! ! // prints Hello, World! to the console 16 ! ! System.out.println("Hello, World!"); 17 ! } 18 19 }
  3. Java Variables

  4. Java Variables • In Java, all variables must have a

    type and an identifier
  5. Java Variables • In Java, all variables must have a

    type and an identifier • size in memory
  6. Java Variables • In Java, all variables must have a

    type and an identifier • size in memory • possible operations (strongly-typed)
  7. Java Variables • In Java, all variables must have a

    type and an identifier • size in memory • possible operations (strongly-typed) • variables refer to values (primitives) or pointers to memory locations (objects)
  8. Identifiers Identifiers are the human-readable labels associated variables, classes, or

    methods
  9. Identifiers Identifiers are the human-readable labels associated variables, classes, or

    methods Identifier Rules: (compiler enforced)
  10. Identifiers Identifiers are the human-readable labels associated variables, classes, or

    methods Identifier Rules: (compiler enforced) A-Z,a-z, 0-9, _ and $ int symb*l = 0;
  11. Identifiers Identifiers are the human-readable labels associated variables, classes, or

    methods Identifier Rules: (compiler enforced) A-Z,a-z, 0-9, _ and $ Exception in thread "main" java.lang.Error: Unresolved compilation problems: Syntax error on token "*", ; expected Name cannot be resolved to a variable
  12. Identifiers Identifiers are the human-readable labels associated variables, classes, or

    methods Identifier Rules: (compiler enforced) A-Z,a-z, 0-9, _ and $ cannot start with number or symbol int 1numberFirst = 0;
  13. Identifiers Identifiers are the human-readable labels associated variables, classes, or

    methods Identifier Rules: (compiler enforced) A-Z,a-z, 0-9, _ and $ cannot start with number or symbol Exception in thread "main" java.lang.Error: Unresolved compilation problem: Syntax error on token "1", delete this token
  14. Identifiers Identifiers are the human-readable labels associated variables, classes, or

    methods Identifier Rules: (compiler enforced) A-Z,a-z, 0-9, _ and $ cannot start with number or symbol no reserved words int public = 0;
  15. Identifiers Identifiers are the human-readable labels associated variables, classes, or

    methods Identifier Rules: (compiler enforced) A-Z,a-z, 0-9, _ and $ cannot start with number or symbol no reserved words Unresolved compilation problem: Syntax error on token "public", invalid VariableDeclaratorId
  16. Identifiers Identifiers are the human-readable labels associated variables, classes, or

    methods Identifier Rules: (compiler enforced) A-Z,a-z, 0-9, _ and $ cannot start with number or symbol no reserved words Style Conventions: (programmer enforced)
  17. Identifiers Identifiers are the human-readable labels associated variables, classes, or

    methods Identifier Rules: (compiler enforced) A-Z,a-z, 0-9, _ and $ cannot start with number or symbol no reserved words Style Conventions: (programmer enforced) use descriptive names int aThing = 0;
  18. Identifiers Identifiers are the human-readable labels associated variables, classes, or

    methods Identifier Rules: (compiler enforced) A-Z,a-z, 0-9, _ and $ cannot start with number or symbol no reserved words Style Conventions: (programmer enforced) use descriptive names int studentCounter = 0;
  19. Identifiers Identifiers are the human-readable labels associated variables, classes, or

    methods Identifier Rules: (compiler enforced) A-Z,a-z, 0-9, _ and $ cannot start with number or symbol no reserved words Style Conventions: (programmer enforced) Upper CamelCase for class names use descriptive names public class JumpingCritter {...}
  20. Identifiers Identifiers are the human-readable labels associated variables, classes, or

    methods Identifier Rules: (compiler enforced) A-Z,a-z, 0-9, _ and $ cannot start with number or symbol no reserved words Style Conventions: (programmer enforced) Upper CamelCase for class names lower camelCase for everything else use descriptive names public static void getAllStudents(){...}
  21. Identifiers Identifiers are the human-readable labels associated variables, classes, or

    methods Identifier Rules: (compiler enforced) A-Z,a-z, 0-9, _ and $ cannot start with number or symbol no reserved words Style Conventions: (programmer enforced) don’t use $ Upper CamelCase for class names lower camelCase for everything else use descriptive names
  22. HelloWorldImproved.java 1 import java.util.Scanner; 2 3 4 public class HelloWorldImproved

    5 { 6 7 ! /** 8 ! * @param args 9 ! */ 10 ! public static void main(String[] args) 11 ! { 12 ! ! Scanner keyboard = new Scanner(System.in); 13 ! ! System.out.println("Enter your name:"); 14 ! ! String name = keyboard.nextLine(); 15 ! ! // prints whatever the value of name is when the 16 ! ! program runs 17 ! ! System.out.println(name); 18 ! ! keyboard.close(); 19 20 ! } 21 22 }
  23. (Primitive) Variable Declaration

  24. (Primitive) Variable Declaration int i;

  25. (Primitive) Variable Declaration int i; Set aside 4 bytes (32

    bits) of memory for a variable with the label i 0x198f1327 i 0x198f1347 Java will interpret this data as an int until otherwise directed
  26. (Primitive) Variable Declaration int i; Set aside 4 bytes (32

    bits) of memory for a variable with the label i 0x198f1327 i 0x198f1347 Java will interpret this data as an int until otherwise directed Declaration creates a variable of a given type for use in your program, but does not assign that variable a value.
  27. (Primitive) Variable Assignment int i; Java will interpret this data

    as an int until otherwise directed 0x198f1327 i 0x198f1347
  28. (Primitive) Variable Assignment i = 0; int i; Java will

    interpret this data as an int until otherwise directed 0x198f1327 i 0x198f1347 Fill up the memory location referenced by the label i with the binary representation of 0 00000000000000000000000000000000
  29. (Primitive) Variable Assignment i = 0; Assignment gives a value

    to a variable that has already been declared. int i; Java will interpret this data as an int until otherwise directed 0x198f1327 i 0x198f1347 Fill up the memory location referenced by the label i with the binary representation of 0 00000000000000000000000000000000
  30. (Primitive) Variable Declaration + Assignment

  31. (Primitive) Variable Declaration + Assignment int i = 0;

  32. (Primitive) Variable Declaration + Assignment int i = 0; 0x198f1327

    i 0x198f1347 00000000000000000000000000000000 Java will interpret this data as an int until otherwise directed Set aside 4 bytes of memory referenced by the label i and fill it with the binary representation of 0
  33. (Primitive) Variable Declaration + Assignment int i = 0; 0x198f1327

    i 0x198f1347 00000000000000000000000000000000 Java will interpret this data as an int until otherwise directed Set aside 4 bytes of memory referenced by the label i and fill it with the binary representation of 0 Usually, we initialize variables by declaring and assigning them a value in a single line of code.
  34. (Primitive) Variable Declaration + Assignment

  35. (Primitive) Variable Declaration + Assignment int i = 127;

  36. (Primitive) Variable Declaration + Assignment int i = 127; 0x198f1327

    i 0x198f1347 00000000000000000000000001111111 Java will interpret this data as an int until otherwise directed Set aside 4 bytes of memory referenced by the label i and fill it with the binary representation of 127
  37. (Primitive) Variable Declaration + Assignment int i = 127; 0x198f1327

    i 0x198f1347 00000000000000000000000001111111 Java will interpret this data as an int until otherwise directed Usually, we initialize variables by declaring and assigning them a value in a single line of code. Set aside 4 bytes of memory referenced by the label i and fill it with the binary representation of 127
  38. Referencing Variables

  39. Referencing Variables int i = 127;

  40. Referencing Variables int i = 127; 0x198f1327 i 0x198f1347 00000000000000000000000001111111

  41. Referencing Variables int i = 127; 0x198f1327 i 0x198f1347 00000000000000000000000001111111

    System.out.println(i);
  42. Referencing Variables int i = 127; 0x198f1327 i 0x198f1347 00000000000000000000000001111111

    System.out.println(i); Console output: 127
  43. Referencing Variables int i = 127; 0x198f1327 i 0x198f1347 00000000000000000000000001111111

    System.out.println(i); Any references to a primitive variable after it is declared and initialized will return the value stored in the variable Console output: 127
  44. Assigning Values to Variables int i = 127; System.out.println(i); i

    = 128; System.out.println(i);
  45. Assigning Values to Variables int i = 127; 0x198f1327 i

    0x198f1347 00000000000000000000000001111111 System.out.println(i); i = 128; System.out.println(i);
  46. Assigning Values to Variables int i = 127; 0x198f1327 i

    0x198f1347 00000000000000000000000001111111 System.out.println(i); i = 128; System.out.println(i); Console output: 127
  47. Assigning Values to Variables int i = 127; 0x198f1327 i

    0x198f1347 00000000000000000000000001111111 System.out.println(i); i = 128; System.out.println(i);
  48. Assigning Values to Variables int i = 127; 0x198f1327 i

    0x198f1347 00000000000000000000000001111111 System.out.println(i); i = 128; 00000000000000000000000010000000 System.out.println(i);
  49. Assigning Values to Variables int i = 127; 0x198f1327 i

    0x198f1347 00000000000000000000000001111111 System.out.println(i); i = 128; 00000000000000000000000010000000 System.out.println(i); Console output: 128
  50. Assigning Values to Variables int i = 127; 0x198f1327 i

    0x198f1347 00000000000000000000000001111111 System.out.println(i); i = 128; 00000000000000000000000010000000 System.out.println(i);
  51. Assigning Values to Variables int i = 127; 0x198f1327 i

    0x198f1347 00000000000000000000000001111111 System.out.println(i); After a variable is declared and initialized, we can change its value at any time by reassigning it. Any previous data stored in the variable is destroyed. i = 128; 00000000000000000000000010000000 System.out.println(i);
  52. Assigning Values to Variables int i = 127; 0x198f1327 i

    0x198f1347 00000000000000000000000001111111 System.out.println(i); After a variable is declared and initialized, we can change its value at any time by reassigning it. Any previous data stored in the variable is destroyed. i = 128; 00000000000000000000000010000000 System.out.println(i); Note that I don’t have to refer to a variable’s type after it has been initialized.
  53. Assigning Values to Variables int i = 5 + 5;

    System.out.println(i); i = i * 500 + 7; System.out.println(i);
  54. Assigning Values to Variables int i = 5 + 5;

    0x198f1327 i 0x198f1347 00000000000000000000000000001010 System.out.println(i); i = i * 500 + 7; System.out.println(i); One can also assign the result of expressions to a variable
  55. Assigning Values to Variables int i = 5 + 5;

    0x198f1327 i 0x198f1347 00000000000000000000000000001010 System.out.println(i); i = i * 500 + 7; System.out.println(i); Console output: 10 One can also assign the result of expressions to a variable
  56. Assigning Values to Variables int i = 5 + 5;

    0x198f1327 i 0x198f1347 00000000000000000000000000001010 System.out.println(i); i = i * 500 + 7; System.out.println(i); One can also assign the result of expressions to a variable
  57. Assigning Values to Variables int i = 5 + 5;

    0x198f1327 i 0x198f1347 00000000000000000000000000001010 System.out.println(i); i = i * 500 + 7; 00000000000000000001001110001111 System.out.println(i); One can also assign the result of expressions to a variable The right hand side of an assignment is always evaluated first, so it is possible to reference declared variables (and reassign them).
  58. Assigning Values to Variables int i = 5 + 5;

    0x198f1327 i 0x198f1347 00000000000000000000000000001010 System.out.println(i); i = i * 500 + 7; 00000000000000000001001110001111 System.out.println(i); Console output: 5007 One can also assign the result of expressions to a variable The right hand side of an assignment is always evaluated first, so it is possible to reference declared variables (and reassign them).
  59. Assigning Values to Variables int i = 5 + 5;

    0x198f1327 i 0x198f1347 00000000000000000000000000001010 System.out.println(i); i = i * 500 + 7; 00000000000000000001001110001111 System.out.println(i); One can also assign the result of expressions to a variable The right hand side of an assignment is always evaluated first, so it is possible to reference declared variables (and reassign them).
  60. Assigning Values to Variables int i = 5 + 5;

    0x198f1327 i 0x198f1347 00000000000000000000000000001010 System.out.println(i); Note that Java evaluates mathematical expressions using the same order of operations you know from algebra class. i = i * 500 + 7; 00000000000000000001001110001111 System.out.println(i); One can also assign the result of expressions to a variable The right hand side of an assignment is always evaluated first, so it is possible to reference declared variables (and reassign them).
  61. (Primitive) Variable Declaration long num;

  62. (Primitive) Variable Declaration long num; Changing the type changes the

    size set aside for the variable
  63. (Primitive) Variable Declaration long num; Set aside 8 bytes (64

    bits) of memory for a variable with the label num 0x198f1327 num 0x198f1367 Changing the type changes the size set aside for the variable
  64. (Primitive) Variable Declaration + Assignment 0000000000000000000000000000000000000000000000000000000000000000 0x198f1327 long num =

    0; num 0x198f1367 Set aside 8 bytes (64 bits) of memory for a variable with the label num Changing the type changes the size set aside for the variable
  65. Primitive Types - Signed Integers

  66. Primitive Types - Signed Integers byte b = 0;

  67. Primitive Types - Signed Integers byte b = 0; 00000000

  68. Primitive Types - Signed Integers byte b = 0; short

    s = 0; 00000000
  69. Primitive Types - Signed Integers byte b = 0; short

    s = 0; 0000000000000000 00000000
  70. Primitive Types - Signed Integers byte b = 0; short

    s = 0; int i = 0; 0000000000000000 00000000
  71. Primitive Types - Signed Integers byte b = 0; short

    s = 0; int i = 0; 00000000000000000000000000000000 0000000000000000 00000000
  72. Primitive Types - Signed Integers byte b = 0; short

    s = 0; int i = 0; long l = 0; 00000000000000000000000000000000 0000000000000000 00000000
  73. Primitive Types - Signed Integers byte b = 0; short

    s = 0; int i = 0; long l = 0; 0000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000 0000000000000000 00000000
  74. Primitive Types - Signed Integers byte b = 0; short

    s = 0; int i = 0; long l = 0; 0000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000 0000000000000000 00000000 1 byte
  75. Primitive Types - Signed Integers byte b = 0; short

    s = 0; int i = 0; long l = 0; 0000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000 0000000000000000 00000000 1 byte 2 bytes
  76. Primitive Types - Signed Integers byte b = 0; short

    s = 0; int i = 0; long l = 0; 0000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000 0000000000000000 00000000 1 byte 2 bytes 4 bytes
  77. Primitive Types - Signed Integers byte b = 0; short

    s = 0; int i = 0; long l = 0; 0000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000 0000000000000000 00000000 1 byte 2 bytes 4 bytes 8 bytes
  78. Primitive Types - Signed Integers byte b = 0; short

    s = 0; int i = 0; long l = 0; 0000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000 0000000000000000 00000000 One bit of each signed integer is used to represent the sign of the integer. 1 byte 2 bytes 4 bytes 8 bytes
  79. Primitive Types - Signed Integers byte b = 0; short

    s = 0; int i = 0; long l = 0; 0000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000 0000000000000000 00000000 1 byte 2 bytes 4 bytes 8 bytes What is the range of values that can be stored in each type?
  80. Primitive Data Types

  81. Primitive Data Types Type Size Range byte 1 byte -27

    to 27 short 2 bytes -215 to 215 int 4 bytes -231 to 231 long 8 bytes -263 to 263 float 4 bytes 3.4E±38 double 8 bytes 1.8E±308 boolean 1 byte true or false char 2 bytes Unicode character codes
  82. Primitive Data Types Type Size Range byte 1 byte -27

    to 27 short 2 bytes -215 to 215 int 4 bytes -231 to 231 long 8 bytes -263 to 263 float 4 bytes 3.4E±38 double 8 bytes 1.8E±308 boolean 1 byte true or false char 2 bytes Unicode character codes
  83. Primitive Data Types Type Size Range byte 1 byte -27

    to 27 short 2 bytes -215 to 215 int 4 bytes -231 to 231 long 8 bytes -263 to 263 float 4 bytes 3.4E±38 double 8 bytes 1.8E±308 boolean 1 byte true or false char 2 bytes Unicode character codes
  84. Primitive Data Types Type Size Range byte 1 byte -27

    to 27 short 2 bytes -215 to 215 int 4 bytes -231 to 231 long 8 bytes -263 to 263 float 4 bytes 3.4E±38 double 8 bytes 1.8E±308 boolean 1 byte true or false char 2 bytes Unicode character codes
  85. Primitive Data Types Type Size Range byte 1 byte -27

    to 27 short 2 bytes -215 to 215 int 4 bytes -231 to 231 long 8 bytes -263 to 263 float 4 bytes 3.4E±38 double 8 bytes 1.8E±308 boolean 1 byte true or false char 2 bytes Unicode character codes
  86. Primitive Data Types Type Size Range byte 1 byte -27

    to 27 short 2 bytes -215 to 215 int 4 bytes -231 to 231 long 8 bytes -263 to 263 float 4 bytes 3.4E±38 double 8 bytes 1.8E±308 boolean 1 byte true or false char 2 bytes Unicode character codes
  87. Primitive Data Types Type Size Range byte 1 byte -27

    to 27 short 2 bytes -215 to 215 int 4 bytes -231 to 231 long 8 bytes -263 to 263 float 4 bytes 3.4E±38 double 8 bytes 1.8E±308 boolean 1 byte true or false char 2 bytes Unicode character codes
  88. Primitive Data Types Type Size Range byte 1 byte -27

    to 27 short 2 bytes -215 to 215 int 4 bytes -231 to 231 long 8 bytes -263 to 263 float 4 bytes 3.4E±38 double 8 bytes 1.8E±308 boolean 1 byte true or false char 2 bytes Unicode character codes
  89. Primitive Data Types Type Size Range byte 1 byte -27

    to 27 short 2 bytes -215 to 215 int 4 bytes -231 to 231 long 8 bytes -263 to 263 float 4 bytes 3.4E±38 double 8 bytes 1.8E±308 boolean 1 byte true or false char 2 bytes Unicode character codes
  90. Primitive Data Types Type Size Range byte 1 byte -27

    to 27 short 2 bytes -215 to 215 int 4 bytes -231 to 231 long 8 bytes -263 to 263 float 4 bytes 3.4E±38 double 8 bytes 1.8E±308 boolean 1 byte true or false char 2 bytes Unicode character codes
  91. Primitive Data Types Type Size Range byte 1 byte -27

    to 27 short 2 bytes -215 to 215 int 4 bytes -231 to 231 long 8 bytes -263 to 263 float 4 bytes 3.4E±38 double 8 bytes 1.8E±308 boolean 1 byte true or false char 2 bytes Unicode character codes
  92. Primitive Data Types Type Size Range byte 1 byte -27

    to 27 short 2 bytes -215 to 215 int 4 bytes -231 to 231 long 8 bytes -263 to 263 float 4 bytes 3.4E±38 double 8 bytes 1.8E±308 boolean 1 byte true or false char 2 bytes Unicode character codes
  93. Primitive Data Types Type Size Range byte 1 byte -27

    to 27 short 2 bytes -215 to 215 int 4 bytes -231 to 231 long 8 bytes -263 to 263 float 4 bytes 3.4E±38 double 8 bytes 1.8E±308 boolean 1 byte true or false char 2 bytes Unicode character codes
  94. Primitive Data Types Type Size Range byte 1 byte -27

    to 27 short 2 bytes -215 to 215 int 4 bytes -231 to 231 long 8 bytes -263 to 263 float 4 bytes 3.4E±38 double 8 bytes 1.8E±308 boolean 1 byte true or false char 2 bytes Unicode character codes
  95. Primitive Data Types Type Size Range byte 1 byte -27

    to 27 short 2 bytes -215 to 215 int 4 bytes -231 to 231 long 8 bytes -263 to 263 float 4 bytes 3.4E±38 double 8 bytes 1.8E±308 boolean 1 byte true or false char 2 bytes Unicode character codes
  96. Primitive Data Types Type Size Range byte 1 byte -27

    to 27 short 2 bytes -215 to 215 int 4 bytes -231 to 231 long 8 bytes -263 to 263 float 4 bytes 3.4E±38 double 8 bytes 1.8E±308 boolean 1 byte true or false char 2 bytes Unicode character codes
  97. Primitive Data Types Type Size Range byte 1 byte -27

    to 27 short 2 bytes -215 to 215 int 4 bytes -231 to 231 long 8 bytes -263 to 263 float 4 bytes 3.4E±38 double 8 bytes 1.8E±308 boolean 1 byte true or false char 2 bytes Unicode character codes
  98. Primitive Data Types Type Size Range byte 1 byte -27

    to 27 short 2 bytes -215 to 215 int 4 bytes -231 to 231 long 8 bytes -263 to 263 float 4 bytes 3.4E±38 double 8 bytes 1.8E±308 boolean 1 byte true or false char 2 bytes Unicode character codes
  99. Primitive Data Types Type Size Range byte 1 byte -27

    to 27 short 2 bytes -215 to 215 int 4 bytes -231 to 231 long 8 bytes -263 to 263 float 4 bytes 3.4E±38 double 8 bytes 1.8E±308 boolean 1 byte true or false char 2 bytes Unicode character codes
  100. Primitive Data Types Type Size Range byte 1 byte -27

    to 27 short 2 bytes -215 to 215 int 4 bytes -231 to 231 long 8 bytes -263 to 263 float 4 bytes 3.4E±38 double 8 bytes 1.8E±308 boolean 1 byte true or false char 2 bytes Unicode character codes
  101. Primitive Data Types Type Size Range byte 1 byte -27

    to 27 short 2 bytes -215 to 215 int 4 bytes -231 to 231 long 8 bytes -263 to 263 float 4 bytes 3.4E±38 double 8 bytes 1.8E±308 boolean 1 byte true or false char 2 bytes Unicode character codes
  102. Primitive Data Types Type Size Range byte 1 byte -27

    to 27 short 2 bytes -215 to 215 int 4 bytes -231 to 231 long 8 bytes -263 to 263 float 4 bytes 3.4E±38 double 8 bytes 1.8E±308 boolean 1 byte true or false char 2 bytes Unicode character codes
  103. Primitive Data Types Type Size Range byte 1 byte -27

    to 27 short 2 bytes -215 to 215 int 4 bytes -231 to 231 long 8 bytes -263 to 263 float 4 bytes 3.4E±38 double 8 bytes 1.8E±308 boolean 1 byte true or false char 2 bytes Unicode character codes
  104. Primitive Data Types Type Size Range byte 1 byte -27

    to 27 short 2 bytes -215 to 215 int 4 bytes -231 to 231 long 8 bytes -263 to 263 float 4 bytes 3.4E±38 double 8 bytes 1.8E±308 boolean 1 byte true or false char 2 bytes Unicode character codes
  105. Primitive Data Types Type Size Range byte 1 byte -27

    to 27 short 2 bytes -215 to 215 int 4 bytes -231 to 231 long 8 bytes -263 to 263 float 4 bytes 3.4E±38 double 8 bytes 1.8E±308 boolean 1 byte true or false char 2 bytes Unicode character codes
  106. Declaration typeName variableIdentifier; int counter; double weight; boolean canDrive; char

    c;
  107. Declaration typeName variableIdentifier; int counter; double weight; boolean canDrive; char

    c; Creates space in memory based on type of the variable.
  108. Assignment variableIdentifier = value; counter = 5 * 5; weight

    = 4.4; canDrive = false; c = 0x48;
  109. Assignment variableIdentifier = value; counter = 5 * 5; weight

    = 4.4; canDrive = false; c = 0x48; = is the assignment operator
  110. Assignment variableIdentifier = value; counter = 5 * 5; weight

    = 4.4; canDrive = false; c = 0x48; Assigns result of right hand side to variable on left hand side = is the assignment operator
  111. Initialization (Declaration + Assignment) typeName variableIdentifier = value; int counter

    = 0; double weight = 45.7; boolean canDrive = false; char c = ‘H’; Creates space in memory based on type of the variable, then assigns result of right hand side
  112. int 3 /** a normal assignment statement */ 4 !!

    int counter = 0; 5 !! 6 !! /** reassigning the value of counter */ 7 !! counter = 10; 8 9 !! /** the largest and smallest possible int values 10 !! * have constants associated with them 11 !! */ 12 !! int big = Integer.MAX_VALUE; 13 !! int small = Integer.MIN_VALUE; 14 !! 15 !! /** assigining the result of an expression 16 !! * that returns an int */ 17 !! int result = 5 + 5; 18 19 !! /** note that this returns and int! */ 20 !! int quotient = 10 / 3;
  113. double 22 !! /** double values */ 23 !! double

    weight = 1.5; 24 !! 25 !! /** if either of the operands is a double, the 26 !! * result is a double 27 !! */ 28 !! double product = 4 * 3.2;
  114. double 22 !! /** double values */ 23 !! double

    weight = 1.5; 24 !! 25 !! /** if either of the operands is a double, the 26 !! * result is a double 27 !! */ 28 !! double product = 4 * 3.2; A double is 8 bytes of space, but there are infinitely many real numbers (even between 0 and 1).
  115. double 22 !! /** double values */ 23 !! double

    weight = 1.5; 24 !! 25 !! /** if either of the operands is a double, the 26 !! * result is a double 27 !! */ 28 !! double product = 4 * 3.2; How can finite space be used to represent infinitely many numbers?
  116. double 22 !! /** double values */ 23 !! double

    weight = 1.5; 24 !! 25 !! /** if either of the operands is a double, the 26 !! * result is a double 27 !! */ 28 !! double product = 4 * 3.2; http://introcs.cs.princeton.edu/java/91float/ How can finite space be used to represent infinitely many numbers?
  117. boolean 31 !! boolean isDifficult = false; 32 !! boolean

    isEasy = true; 33 !! boolean isAwesome = isEasy; A boolean could be represented in one bit, but it actually uses and entire byte.
  118. char 34 ! 35 !! /** char takes unicode character

    code (hex, bin , dec ) 36 !! * or letter in single quotes */ 37 !! char hex = 0x48; 38 !! char bin = 0b1001000; 39 !! char dec = 72; 40 !! char letter = 'H'; 41 !! A char is 16 bits of unsigned space. What is the range of values that can be assigned to a char?
  119. Code Examples Describe what will happen when each code segment

    is executed
  120. Code Examples Describe what will happen when each code segment

    is executed int factor = 5 * 10 + 3;
  121. Code Examples Describe what will happen when each code segment

    is executed int factor = 5 * 10 + 3; double root = “x”;
  122. Code Examples Describe what will happen when each code segment

    is executed int factor = 5 * 10 + 3; double root = “x”; boolean isActive = False;
  123. Code Examples Describe what will happen when each code segment

    is executed int factor = 5 * 10 + 3; char c = “H”; double root = “x”; boolean isActive = False;
  124. Code Examples Describe what will happen when each code segment

    is executed int factor = 5 * 10 + 3; int factor = 5.0375; char c = “H”; double root = “x”; boolean isActive = False;
  125. Code Examples Describe what will happen when each code segment

    is executed int factor = 5 * 10 + 3; int factor = 5.0375; double weight = 45.7; char c = “H”; double root = “x”; boolean isActive = False;
  126. Code Examples Describe what will happen when each code segment

    is executed int factor = 5 * 10 + 3; int factor = 5.0375; double weight = 45.7; char c = “H”; double root = “x”; boolean isActive = False; double number = 6;
  127. Code Examples Describe what will happen when each code segment

    is executed int factor = 5 * 10 + 3; int factor = 5.0375; double weight = 45.7; boolean canDrive = “false”; char c = “H”; double root = “x”; boolean isActive = False; double number = 6;