04 Variables Data Types
04 Variables Data Types
Data Types
DEPARTMENT OF INFORMATICS | BJÖRN SVENSSON
2. Variables and Data Types
Using the Debugger
This material will rely on the Debugger in VSCode to show what is going on “behind
the scenes” with variables.
3. Primitive Data Types
Java’s 8 primitive data types:
• byte
• short
• int
• long
• float
• double
• char
• boolean
Primitive implies that the data types are built into the language. They are not constructs
made up on other data types. They have reserved key words to mark their types.
Note: String is not in this list…
4. byte
byte age = 26;
The byte data type is an 8-bit signed two's complement integer. It has a minimum value of -128 and
a maximum value of 127 (inclusive). The byte data type can be useful for saving memory in large
arrays, where the memory savings actually matters. They can also be used in place of int where
their limits help to clarify your code; the fact that a variable's range is limited can serve as a form of
documentation.
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
Questions:
• What does bit and 8-bit mean?
• What is two’s complement?
• Why does byte have a minimum and maximum value?
• Why -128 and 127?
5. Down the rabbit hole
The Base 10 number system
Symbols: 0,1,2,3,4,5,6,7,8,9
Base 10 only has 10 symbols. We can count to 9 at most. Thus; to represent larger
numbers we have to add/repeat symbols:
Nine 9
Twelve 12
Fifty-six 56
One hunderd fifty-six 156
Two hundred eighty-eight 288
Nine ?
Twelve ??
Fifty-six ??
One hunderd fifty-six ???
Two hundred eighty-eight ???
8. Down the rabbit hole
Positional notation
Each digit has a value 2x greater than the digit to its right
x8 x4 x2 x1
One 0 0 0 1
Two 0 0 1 0
Three 0 0 1 1
Four 0 1 0 0
Five 0 1 0 1
Ten 1 0 1 0
Fifteen 1 1 1 1
It seems like 15 is the max number that we can represent with 4 numbers in base
2. Similarly, 9999 is the max number that we can represent with 4 numbers in
base 10.
9. Down the rabbit hole
x128 x64 x32 x16 x8 x4 x2 x1
0 0 0 0 0 0 0 0 0
The 8th bit (counting from the right) is now the sign.
0 = Positive number
1 = negative number
Let’s try …
11. Down the rabbit hole
Sign x64 x32 x16 x8 x4 x2 x1
15 0 0 0 0 1 1 1 1
1 + 2
Sign x4 x2 x1
0 0 0 1
0 0 1 0
0 0 1 1
14. Down the rabbit hole
Let’s work with 4 bits (because it’s easier) and try to do some
math.
3 + 4
Sign x4 x2 x1
0 0 1 1
0 1 0 0
0 1 1 1
15. Down the rabbit hole
Let’s work with 4 bits (because it’s easier) and try to do some
math.
3 + (-4)
Sign x4 x2 x1
0 0 1 1
1 1 0 0
1 1 1 1 - 7 ???
16. Down the rabbit hole
Let’s work with 4 bits (because it’s easier) and try to do some
math.
5 + (-5)
Sign x4 x2 x1
1 1
0 1 0 1
1 1 0 1
1 0 0 1 0 2 ???
17. Down the rabbit hole – One’s complement
Decimal Sign x4 x2 x1 Sign is still used
7 0 1 1 1
All bits are flipped for negative numbers. That
is:
6 0 1 1 0
0 is 1
5 0 1 0 1
1 is 0
4 0 1 0 0
3 0 0 1 1
2 0 0 1 0
1 0 0 0 1
0 0 0 0 0
The downsides:
-0 1 0 0 0 • We have -0 …?
-1 1 1 1 0 • And adding negative
-2 1 1 0 1 numbers doesn’t work …
-3 1 1 0 0
-4 1 0 1 1
-5 1 0 1 0
-6 1 0 0 1
-7 1 0 0 0
18. Down the rabbit hole – Two’s complement
Decimal x -8 x4 x2 x1 -0 is gone and replaced by -1
7 0 1 1 1 And we now have -8 (but not positive 8)
6 0 1 1 0
The first bit now works as both a sign and x -8
5 0 1 0 1
(negative 8)
4 0 1 0 0
This causes math to work:
3 0 0 1 1
2
1
0
0
0
0
1
0
0
1
5 + (-5)
0 0 0 0 0 x8 x4 x2 x1
-1 1 1 1 1
-2 1 1 1 0
1 1 1
-3 1 1 0 1
0 1 0 1
-4 1 1 0 0
-5
-6
1
1
0
0
1
1
1
0
1 0 1 1
-7 1 0 0 1
-8 1 0 0 0 1 0 0 0 0
19. Down the rabbit hole – byte
x -128 x64 x32 x16 x8 x4 x2 x1
-128 1 0 0 0 0 0 0 0
The byte data type is an 8-bit signed two's complement integer. It has a minimum value of -128
and a maximum value of 127 (inclusive). The byte data type can be useful for saving memory in
large arrays, where the memory savings actually matters. They can also be used in place of int
where their limits help to clarify your code; the fact that a variable's range is limited can serve as a
form of documentation.
20. byte
byte age = 26;
The byte data type is an 8-bit signed two's complement integer. It has a minimum value of -128 and
a maximum value of 127 (inclusive). The byte data type can be useful for saving memory in large
arrays, where the memory savings actually matters. They can also be used in place of int where
their limits help to clarify your code; the fact that a variable's range is limited can serve as a form of
documentation.
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
The short data type is a 16-bit signed two's complement integer. It has a
minimum value of -32,768 and a maximum value of 32,767 (inclusive). As with
byte, the same guidelines apply: you can use a short to save memory in large
arrays, in situations where the memory savings actually matters.
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
22. int
int salary = 350000;
By default, the int data type is a 32-bit signed two's complement integer, which
has a minimum value of -231 and a maximum value of 231-1
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
23. Float and double
double length = 175.9;
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
24. boolean
boolean isMarried = false;
The boolean data type has only two possible values: true and false. Use this
data type for simple flags that track true/false conditions. This data type
represents one bit of information, but its "size" isn't something that's precisely
defined.
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
25. Size comparison
The number 120 represented as both a 8-bit (1 byte) and a 16-bit (2 byte)
integer
x -32768 x16384 x8192 x4096 x2048 x1024 x512 x256 x128 x64 x32 x16 x8 x4 x2 x1
0 0 0 0 0 0 0 0 1 1 1 1 1 0 0 0
All of those zeroes in the 16-bit int still take up memory space regardless if you
“use” them or not.
26. Is 2 bytes a lot?
1 Byte = 8 bits
1 Kilobyte = 1024 bytes
1 Megabyte = 1024 Kilobytes
1 Gigabyte = 1024 Megabytes
char: The char data type is a single 16-bit Unicode character. It has a minimum
value of '\u0000' (or 0) and a maximum value of '\uffff' (or 65,535 inclusive).
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
Question
• What does it mean that the character is 16 bit?
• Actually, how are characters physically stored in computers? We know how
it’s done with numbers now. But what about A? Or $?
28. char
29. char - ASCII
https://commons.wikimedia.org/wiki/File:ASCII-Table-wide.svg
30. char – UTF-16
UTF-16, simplified, is a bigger version of ASCII containing everything for
Nordic characters, math operators, Klingon characters (yes, from star trek) to
emojis. Each character is mapped to a 16 bit number
https://en.wikipedia.org/wiki/List_of_Unicode_characters
31. Spoiler: What is String?
String is a list of chars.
// Array of Char
char[] name = {'J','o','h','n'};
This is to show that String is not primitive. It’s a list of char, which is primitive.
Now – what practical consequences does primitive vs non-primitive have?
32. Primitive Data Types vs Reference Types
Primitive data types are defined in the Java source code. Anything that is not a
primitive data type in Java is a “reference type”, which you can think of as an Object.
Thus, anything that is not a primitive data type, is a sort of “construct” achieved by
using the primitive data types.
33. Primitive Data Types vs reference types
int age = 20; Variables of primitive data types contain their
double height = 178.5;
boolean isMarried = false; values directly, while variables of non-primitive
data types contain references to their values.
String name = "Gary Stu";
String address = "Paradisgatan 2";
Person person1 = new Person();
Paradisgatan 2
name isMarried
Gary Stu
false
age
20 height
178.5 person1 : Person
person1
34. Primitive Data Types vs reference types
int age = 20;
double height = 178.5;
boolean isMarried = false;
Code in debugger:
(@19) is a reference.*
person1 @23
37. Primitive Data Types vs reference types
Person person1 = new Person();
Person person2 = person1;
Person person3 = person1;
person1 @
9
person2 @ @ person1 : Person
9 9
person3 @
9
38. Primitive Data Types vs reference types
This class has four instance variables; two primitives and two
non-primitives. What happens when we instantiate this class?
public Person() {
person1 @8
@9 Gary
@8 person1 : Person
firstName = @9
lastName = @10
age = 24 @10 Stu
height = 165.8
40. Primitive Data Types vs reference types
When does primitive vs. non-primitive data types matter?
App.changeValues(age);
System.out.println(output);
}
App.changeValues(age);
System.out.println(output);
}
}
42. Primitive Data Types vs reference types
Your Computer’s Random-Access Memory
App.changeValues(age);
System.out.println(output);
}
}
43. Primitive Data Types vs reference types
Your Computer’s Random-Access Memory
App.changeValues(age);
System.out.println(output); 20
}
}
44. Primitive Data Types vs reference types
Your Computer’s Random-Access Memory
App.changeValues(age);
System.out.println(output); 99
}
}
45. Primitive Data Types vs reference types
Your Computer’s Random-Access Memory
App.changeValues(age);
System.out.println(output); 99
}
}
46. Primitive Data Types vs reference types
Your Computer’s Random-Access Memory
App.changeValues(age);
System.out.println(output);
}
@7 age: 20
public static void changeValues(int x) {
x = 99;
}
}
47. Primitive Data Types vs reference types
Your Computer’s Random-Access Memory
App.changeValues(age);
System.out.println(output);
}
@7 age: 20
public static void changeValues(int x) {
x = 99;
}
}
48. Primitive Data Types vs reference types
The exact same program, but with Objects (Person) Your Computer’s Random-Access Memory
instead of int!
App.changeValues(person1);
App.changeValues(person1);
App.changeValues(person1);
App.changeValues(person1);
App.changeValues(person1);
App.changeValues(person1);
App.changeValues(person1);
Terminal
age: 99
58. Primitive Data Types vs reference types
The exact same program, but with String instead of int! Your Computer’s Random-Access Memory
App.changeValues(age, relatives);
System.out.println(output);
}
App.changeValues(age, relatives);
@5 20
System.out.println(output);
}
App.changeValues(age, relatives);
@5 20
@8 6
String output = "age: " + age + "\nrelatives: "
+ relatives;
System.out.println(output);
}
App.changeValues(age, relatives);
@5 20
@8 6
String output = "age: " + age + "\nrelatives: "
+ relatives;
System.out.println(output);
}
App.changeValues(age, relatives);
@5 20
@8 6
String output = "age: " + age + "\nrelatives: "
+ relatives;
System.out.println(output); x @5 y @8
}
App.changeValues(age, relatives);
@5 20
@8 6
String output = "age: " + age + "\nrelatives: "
+ relatives;
App.changeValues(age, relatives);
@5 20
@8 6
String output = "age: " + age + "\nrelatives: "
+ relatives;
App.changeValues(age, relatives);
@5 20
@8 6
String output = "age: " + age + "\nrelatives: "
+ relatives;
App.changeValues(age, relatives);
@5 20
@8 6
String output = "age: " + age + "\nrelatives: "
+ relatives;
App.changeValues(age, relatives);
@5 20
@8 6
String output = "age: " + age + "\nrelatives: "
+ relatives;
Terminal
age: 20
relatives: 6
69. What about String?
String name = "Gary Stu"; Will name and name2 have the same
String address = "Ole Römers väg 6";
reference, or different references?
String name2 = name;
String address2 = "Paradisgatan 2";
70. What about String?
String name = "Gary Stu";
String address = "Ole Römers väg 6";
Will address and address2 have the same
reference, or different references?
String name2 = "Mary Sue";
String address2 = "Ole Römers väg 6";
… what?
71. What about String?
String name = "G";
name += "ary";
name += " ";
name += "Stu";
72. What about String?
String name = "G";
name += "ary";
name += " ";
name += "Stu";
73. What about String?
String name = "G";
name += "ary";
name += " ";
name += "Stu";
74. What about String?
String name = "G";
name += "ary";
name += " ";
name += "Stu";
}
}
78. Primitive Data Types vs reference types
Person person1 = new Person("Gary", "Paradisgatan 2");
Person person2 = new Person("Ham", "Paradisgatan 2");
System.out.println(myAddress);
}
}
Terminal
Same reference: true
Same content: true
82. What about String?
• String is an Object
• Specifically, it’s an Array of char
• Java has a special section in RAM called the String Pool
where it stores Strings
• Only one copy of each String is stored in the String Pool
• If you assign an already existing String to a variable, you will
simply get a reference to that String in the String Pool
• If there is no such String, it will be added to the Pool, and
you will get a reference to that String
83. Primitive Data Types vs reference types
String myAddress = new String("Paradisgatan 2");
String yourAddress = new String("Paradisgatan 2");
String myName = "Gary";
String yourName = ”Gary";
int age = 26;
Person person1 = new Person();
yourAddress @1
8 @1 Paradisgatan 2
age
myName @1 7
26 9
@1 Paradisgatan 2
yourName @1 8
9
person1 @2 @2 person1 : Person @1 Gary
1 1 9
84. Null
• Can be thought of as a placeholder that signifies the absence of a
reference.
• A literal representing a null reference, that is a reference that points to
no object in RAM
• Can be assigned to any reference type.
• Can be checked using the == operator
• A variable (of reference type) assigned null means that the variable
does not hold a reference to any RAM location.
• Primitive types like int and boolean cannot be null.
person1 @9
@9 person1 : Person
@2 Gary
name = @21
1
Address = @23
person2 @2 Paradisgatan 2
3
86. Null
Person person1 = new Person("Gary", "Paradisgatan 2");
Person person2 = null;
Terminal
Is person1 null? false
Is person2 null? true
87. Null
Person person1 = new Person("Gary", "Paradisgatan 2");
Person person2 = null;
Terminal
Gary
Exception in thread "main" java.lang.NullPointerException: Cannot invoke
"Person.getName()" because "person2" is null
at App.main(App.java:14)
88. Null
Person person1 = new Person("Gary", "Paradisgatan 2");
Person person2 = new Person();
Terminal
Gary
null
89. Null
Person person1 = new Person("Gary", "Paradisgatan 2");
Person person2 = new Person();
Terminal
Gary
Exception in thread "main" java.lang.NullPointerException: Cannot invoke
"String.length()" because "name2" is null
at App.main(App.java:15)
90. Null
Person person1 = new Person("Gary", "Paradisgatan 2");
Person person2 = new Person("", "");
Terminal output:
Gary
0 "" (empty string) is
printed here. Note that
empty string and null are
different
91. Null
public static void main(String[] args) {
App.renamePerson(person, newName);
}
App.renamePerson(person, newName);
}
App.renamePerson(person, null);
}
App.renamePerson(null, null);
}
Terminal
Exception in thread "main" java.lang.NullPointerException: Cannot invoke
"Person.setName(String)" because "person" is null
at App.renamePerson(App.java:15)
at App.main(App.java:11)
95. Variable scope – class and method scope
package se.lu.ics.models;
Instance variables have class scope.
public class Employee {
private String employeeId; They are accessible to any method or
private String name;
constructor within the class
public Employee(String name) {
}
this.name = name; The name parameter has a constructor scope. It is only accessible
within the constructor. This allows for setName() having a
public void print() {
String output = "Employee ID: " + employeeId + parameter with the same name.
"\n" + "Name: " + name;
System.out.println(output);
}
public String getEmployeeId() { The variable output has a local scope. It is only accessible within
return employeeId;
} the print() method. Note, however, that the print() method can
public void setEmployeeId(String id) {
access the instance variables.
this.employeeId = id;
}
int methodScopedVariable = 0;