Slide 1

Slide 1 text

Variables, Types, & Operators in Java AP Computer Science Holy Innocents’ Episcopal School

Slide 2

Slide 2 text

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 }

Slide 3

Slide 3 text

Java Variables

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

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)

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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;

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

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;

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

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;

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

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)

Slide 17

Slide 17 text

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;

Slide 18

Slide 18 text

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;

Slide 19

Slide 19 text

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 {...}

Slide 20

Slide 20 text

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(){...}

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

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 }

Slide 23

Slide 23 text

(Primitive) Variable Declaration

Slide 24

Slide 24 text

(Primitive) Variable Declaration int i;

Slide 25

Slide 25 text

(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

Slide 26

Slide 26 text

(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.

Slide 27

Slide 27 text

(Primitive) Variable Assignment int i; Java will interpret this data as an int until otherwise directed 0x198f1327 i 0x198f1347

Slide 28

Slide 28 text

(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

Slide 29

Slide 29 text

(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

Slide 30

Slide 30 text

(Primitive) Variable Declaration + Assignment

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

(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

Slide 33

Slide 33 text

(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.

Slide 34

Slide 34 text

(Primitive) Variable Declaration + Assignment

Slide 35

Slide 35 text

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

Slide 36

Slide 36 text

(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

Slide 37

Slide 37 text

(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

Slide 38

Slide 38 text

Referencing Variables

Slide 39

Slide 39 text

Referencing Variables int i = 127;

Slide 40

Slide 40 text

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

Slide 41

Slide 41 text

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

Slide 42

Slide 42 text

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

Slide 43

Slide 43 text

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

Slide 44

Slide 44 text

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

Slide 45

Slide 45 text

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

Slide 46

Slide 46 text

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

Slide 47

Slide 47 text

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

Slide 48

Slide 48 text

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

Slide 49

Slide 49 text

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

Slide 50

Slide 50 text

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

Slide 51

Slide 51 text

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);

Slide 52

Slide 52 text

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.

Slide 53

Slide 53 text

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

Slide 54

Slide 54 text

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

Slide 55

Slide 55 text

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

Slide 56

Slide 56 text

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

Slide 57

Slide 57 text

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).

Slide 58

Slide 58 text

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).

Slide 59

Slide 59 text

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).

Slide 60

Slide 60 text

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).

Slide 61

Slide 61 text

(Primitive) Variable Declaration long num;

Slide 62

Slide 62 text

(Primitive) Variable Declaration long num; Changing the type changes the size set aside for the variable

Slide 63

Slide 63 text

(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

Slide 64

Slide 64 text

(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

Slide 65

Slide 65 text

Primitive Types - Signed Integers

Slide 66

Slide 66 text

Primitive Types - Signed Integers byte b = 0;

Slide 67

Slide 67 text

Primitive Types - Signed Integers byte b = 0; 00000000

Slide 68

Slide 68 text

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

Slide 69

Slide 69 text

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

Slide 70

Slide 70 text

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

Slide 71

Slide 71 text

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

Slide 72

Slide 72 text

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

Slide 73

Slide 73 text

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

Slide 74

Slide 74 text

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

Slide 75

Slide 75 text

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

Slide 76

Slide 76 text

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

Slide 77

Slide 77 text

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

Slide 78

Slide 78 text

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

Slide 79

Slide 79 text

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?

Slide 80

Slide 80 text

Primitive Data Types

Slide 81

Slide 81 text

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

Slide 82

Slide 82 text

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

Slide 83

Slide 83 text

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

Slide 84

Slide 84 text

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

Slide 85

Slide 85 text

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

Slide 86

Slide 86 text

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

Slide 87

Slide 87 text

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

Slide 88

Slide 88 text

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

Slide 89

Slide 89 text

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

Slide 90

Slide 90 text

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

Slide 91

Slide 91 text

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

Slide 92

Slide 92 text

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

Slide 93

Slide 93 text

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

Slide 94

Slide 94 text

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

Slide 95

Slide 95 text

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

Slide 96

Slide 96 text

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

Slide 97

Slide 97 text

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

Slide 98

Slide 98 text

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

Slide 99

Slide 99 text

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

Slide 100

Slide 100 text

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

Slide 101

Slide 101 text

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

Slide 102

Slide 102 text

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

Slide 103

Slide 103 text

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

Slide 104

Slide 104 text

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

Slide 105

Slide 105 text

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

Slide 106

Slide 106 text

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

Slide 107

Slide 107 text

Declaration typeName variableIdentifier; int counter; double weight; boolean canDrive; char c; Creates space in memory based on type of the variable.

Slide 108

Slide 108 text

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

Slide 109

Slide 109 text

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

Slide 110

Slide 110 text

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

Slide 111

Slide 111 text

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

Slide 112

Slide 112 text

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;

Slide 113

Slide 113 text

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;

Slide 114

Slide 114 text

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).

Slide 115

Slide 115 text

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?

Slide 116

Slide 116 text

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?

Slide 117

Slide 117 text

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.

Slide 118

Slide 118 text

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?

Slide 119

Slide 119 text

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

Slide 120

Slide 120 text

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

Slide 121

Slide 121 text

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

Slide 122

Slide 122 text

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

Slide 123

Slide 123 text

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;

Slide 124

Slide 124 text

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;

Slide 125

Slide 125 text

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;

Slide 126

Slide 126 text

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;

Slide 127

Slide 127 text

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;