type and an identifier • size in memory • possible operations (strongly-typed) • variables refer to values (primitives) or pointers to memory locations (objects)
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
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
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
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)
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;
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;
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 {...}
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(){...}
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
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.
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
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
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
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.
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
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
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
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);
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.
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
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
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
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).
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).
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).
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).
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
s = 0; int i = 0; long l = 0; 0000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000 0000000000000000 00000000
s = 0; int i = 0; long l = 0; 0000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000 0000000000000000 00000000 1 byte
s = 0; int i = 0; long l = 0; 0000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000 0000000000000000 00000000 1 byte 2 bytes
s = 0; int i = 0; long l = 0; 0000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000 0000000000000000 00000000 1 byte 2 bytes 4 bytes
s = 0; int i = 0; long l = 0; 0000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000 0000000000000000 00000000 1 byte 2 bytes 4 bytes 8 bytes
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
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?
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
= 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
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;
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).
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?
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?
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?