0% found this document useful (0 votes)
3 views

04 Variables Data Types

Uploaded by

vengat.r.ramu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

04 Variables Data Types

Uploaded by

vengat.r.ramu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 97

Variables and

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

How does this actually work?


How can you tell that 21 is greater than 12? They both have 1 and 2 in them!
6. Down the rabbit hole
Positional notation
Each digit has a value 10x greater than the digit to its right

x1000 x100 x10 x1


One 1
Five 5
Fifteen 1 5
Twenty nine 2 9
One hundred 1 0 0
Two hundred forty 2 4 0
One thousand six hundred eighty-nine 1 6 8 9

But what does this have to do with bit and byte?


7. Down the rabbit hole
The Base two number system
Symbols: 0 and 1
Base two only has two symbols. How do we represent values that are greater than 1?

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

x128 x64 x32 x16 x8 x4 x2 x1


255 1 1 1 1 1 1 1 1

Wait a minute … how do we represent negative numbers in base 2?


10. Down the rabbit hole
Sign 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

Sign x64 x32 x16 x8 x4 x2 x1


-15 1 0 0 0 1 1 1 1

Sign x64 x32 x16 x8 x4 x2 x1


-79 1 1 0 0 1 1 1 1

Sign x64 x32 x16 x8 x4 x2 x1


-100 1 1 1 0 0 1 0 0
12. Down the rabbit hole
Sign x64 x32 x16 x8 x4 x2 x1
-127 1 1 1 1 1 1 1 1

Sign x64 x32 x16 x8 x4 x2 x1


127 0 1 1 1 1 1 1 1

We can no longer get to 255. But at least we can represent


negative numbers!
13. Down the rabbit hole
Let’s work with 4 bits (because it’s easier) and try to do some
math.

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

x128 x64 x32 x16 x8 x4 x2 x1


127 0 1 1 1 1 1 1 1

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

8 bit ints are, in general called bytes


21. short
short capacity = 20000;

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;

Can hold decimal values

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 -128 x64 x32 x16 x8 x4 x2 x1


0 1 1 1 1 0 0 0

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

That’s a lot of ones and zeroes …


27. char
char letterGrade = 'A';

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

Char can hold a single character.

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

For a complete list, see:

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'};

String strName = "John";

For now, think of an array as a list.

More on arrays later …

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

Your Computer’s Random-Access Memory


address

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;

String name = "Gary Stu";


String address = "Paradisgatan 2";
Person person1 = new Person();

Code in debugger:
(@19) is a reference.*

Ref Type Note: the primitives have no


Primitive references attached to them!
Primitive
* It’s not the real reference, it’s an
Primitive
arbitrary one assigned by the
Ref Type
debugger so that it’s easier for you to
Ref Type
see what is what. But the debugger
Ref Type only does this to reference types!
35. Primitive Data Types vs reference types
int age = 20;
double height = 178.5;
boolean isMarried = false;

String name = "Gary Stu";


String address = "Paradisgatan 2";
Person person1 = new Person();

String in the debugger:

A note on the VSCode debugger’s representation of Strings


like "Gary Stu"

The debugger does not show the strings themselves being


assigned references like @25. However if you expand them
you will see that they contain byte arrays (byte[]) which in
turn hold the actual string contents (chars represented as
bytes), and these byte[] are assigned references. So String
is not primitive, even though it may look that way unless
you expand them in the debugger.
36. Primitive Data Types vs reference types
Ignoring args for now. We shall de-mystify that
one later …

Your Computer’s Random-Access Memory


address @27
@27 Paradisgatan 2
name @25 isMarried
@25 Gary Stu false
age
20 height
178.5 @23 person1 : Person

person1 @23
37. Primitive Data Types vs reference types
Person person1 = new Person();
Person person2 = person1;
Person person3 = person1;

String name = "Gary Stu";


String name2 = name;

Your Computer’s Random-Access Memory


name @24

name2 @24 @24 Gary Stu

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 class Person {


private String firstName;
private String lastName;
private int age;
private double height;

public Person() {

public Person(String firstName, String lastName, int age, double height) {


this.firstName = firstName;
this.lastName = lastName;
this.age = age;
this.height = height;
}
}
39. Primitive Data Types vs reference types
Person person1 = new Person("Gary", "Stu" , 24 , 165.8);

Your Computer’s Random-Access Memory

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?

public class App {

public static void main(String[] args) {


int age = 20;

App.changeValues(age);

String output = "age: " + age;

System.out.println(output);
}

public static void changeValues(int x) {


x = 99;
} Terminal
age: 20
}
41. Primitive Data Types vs reference types
Your Computer’s Random-Access Memory

public class App {

public static void main(String[] args) { age


int age = 20; 20

App.changeValues(age);

String output = "age: " + age;

System.out.println(output);
}

public static void changeValues(int x) {


x = 99;
}

}
42. Primitive Data Types vs reference types
Your Computer’s Random-Access Memory

public class App {

public static void main(String[] args) { age


int age = 20; 20

App.changeValues(age);

String output = "age: " + age;

System.out.println(output);
}

public static void changeValues(int x) {


x = 99;
}

}
43. Primitive Data Types vs reference types
Your Computer’s Random-Access Memory

public class App {

public static void main(String[] args) { age


int age = 20; 20

App.changeValues(age);

String output = "age: " + age; x

System.out.println(output); 20
}

public static void changeValues(int x) {


x = 99;
}

}
44. Primitive Data Types vs reference types
Your Computer’s Random-Access Memory

public class App {

public static void main(String[] args) { age


int age = 20; 20

App.changeValues(age);

String output = "age: " + age; x

System.out.println(output); 99
}

public static void changeValues(int x) {


x = 99;
}

}
45. Primitive Data Types vs reference types
Your Computer’s Random-Access Memory

public class App {

public static void main(String[] args) { age


int age = 20; 20

App.changeValues(age);

String output = "age: " + age; x

System.out.println(output); 99
}

public static void changeValues(int x) {


x = 99;
}

}
46. Primitive Data Types vs reference types
Your Computer’s Random-Access Memory

public class App {

public static void main(String[] args) { age


int age = 20; 20

App.changeValues(age);

String output = "age: " + age; output @7

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

public class App {

public static void main(String[] args) { age


int age = 20; 20

App.changeValues(age);

String output = "age: " + age; output @7

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!

public class Person {


private int age;

public Person(int age) {


this.age = age;
}

public int getAge() {


return age;
}

public void setAge(int age) {


this.age = age;
}
}
49. Primitive Data Types vs reference types
Your Computer’s Random-Access Memory
public class App {
public static void main(String[] args) {
person1 @8
Person person1 = new Person(24);

App.changeValues(person1);

String output = "age: " + person1.getAge();


@8 person1 : Person
System.out.println(output); age = 24
}

public static void changeValues(Person personToChange) {


personToChange.setAge(99);
}
}
50. Primitive Data Types vs reference types
Your Computer’s Random-Access Memory
public class App {
public static void main(String[] args) {
person1 @8
Person person1 = new Person(24);

App.changeValues(person1);

String output = "age: " + person1.getAge();


@8 person1 : Person
System.out.println(output); age = 24
}

public static void changeValues(Person personToChange) {


personToChange.setAge(99);
}
}
51. Primitive Data Types vs reference types
Your Computer’s Random-Access Memory
public class App {
public static void main(String[] args) {
person1 @8
Person person1 = new Person(24);

App.changeValues(person1);

String output = "age: " + person1.getAge();


@8 person1 : Person
System.out.println(output); age = 24
}

public static void changeValues(Person personToChange) {


personToChange.setAge(99); @8 personToChange
}
}
52. Primitive Data Types vs reference types
Your Computer’s Random-Access Memory
public class App {
public static void main(String[] args) {
person1 @8
Person person1 = new Person(24);

App.changeValues(person1);

String output = "age: " + person1.getAge();


@8 person1 : Person
System.out.println(output); age = 24
}

public static void changeValues(Person personToChange) {


personToChange.setAge(99); @8 personToChange
}
}
53. Primitive Data Types vs reference types
Your Computer’s Random-Access Memory

public class Person {


private int age; person1 @8

public Person(int age) {


this.age = age;
}
@8 person1 : Person
public int getAge() { age = 24
return age;
}
@8 personToChange
public void setAge(int age) {
this.age = age; age
}
} 99
54. Primitive Data Types vs reference types
Your Computer’s Random-Access Memory

public class Person {


private int age; person1 @8

public Person(int age) {


this.age = age;
}
@8 person1 : Person
public int getAge() { age = 99
return age;
}
@8 personToChange
public void setAge(int age) {
this.age = age; age
}
} 99

Look at the assignment!


The instance variable age of the instance/object is assigned the
value of the parameter age of the method which was passed the
argument 99
55. Primitive Data Types vs reference types
Your Computer’s Random-Access Memory
public class App {
public static void main(String[] args) {
person1 @8
Person person1 = new Person(24);

App.changeValues(person1);

String output = "age: " + person1.getAge();


@8 person1 : Person
System.out.println(output); age = 99
}

public static void changeValues(Person personToChange) {


personToChange.setAge(99); @8 personToChange
}
} age
99
56. Primitive Data Types vs reference types
Your Computer’s Random-Access Memory
public class App {
public static void main(String[] args) {
person1 @8
Person person1 = new Person(24);

App.changeValues(person1);

String output = "age: " + person1.getAge();


@8 person1 : Person
System.out.println(output); age = 99
}

public static void changeValues(Person personToChange) {


personToChange.setAge(99); output @9 @9 age: 99
}
}
57. Primitive Data Types vs reference types
Your Computer’s Random-Access Memory
public class App {
public static void main(String[] args) {
person1 @8
Person person1 = new Person(24);

App.changeValues(person1);

String output = "age: " + person1.getAge();


@8 person1 : Person
System.out.println(output); age = 99
}

public static void changeValues(Person personToChange) {


personToChange.setAge(99); output @9 @9 age: 99
}
}

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

public class App {

public static void main(String[] args) {


String age = "20";
String relatives = "6";

App.changeValues(age, relatives);

String output = "age: " + age + "\nrelatives: "


+ relatives;

System.out.println(output);
}

public static void changeValues(String x, String y) {


x = "100";
y = "200";
}
}
59. Primitive Data Types vs reference types
The exact same program, but with String instead of int! Your Computer’s Random-Access Memory

public class App {


age @5
public static void main(String[] args) {
String age = "20";
String relatives = "6";

App.changeValues(age, relatives);
@5 20

String output = "age: " + age + "\nrelatives: "


+ relatives;

System.out.println(output);
}

public static void changeValues(String x, String y) {


x = "100";
y = "200";
}
}
60. Primitive Data Types vs reference types
The exact same program, but with String instead of int! Your Computer’s Random-Access Memory

public class App {


age @5
public static void main(String[] args) {
String age = "20"; relatives @8
String relatives = "6";

App.changeValues(age, relatives);
@5 20
@8 6
String output = "age: " + age + "\nrelatives: "
+ relatives;

System.out.println(output);
}

public static void changeValues(String x, String y) {


x = "100";
y = "200";
}
}
61. Primitive Data Types vs reference types
The exact same program, but with String instead of int! Your Computer’s Random-Access Memory

public class App {


age @5
public static void main(String[] args) {
String age = "20"; relatives @8
String relatives = "6";

App.changeValues(age, relatives);
@5 20
@8 6
String output = "age: " + age + "\nrelatives: "
+ relatives;

System.out.println(output);
}

public static void changeValues(String x, String y) {


x = "100";
y = "200";
}
}
62. Primitive Data Types vs reference types
The exact same program, but with String instead of int! Your Computer’s Random-Access Memory

public class App {


age @5
public static void main(String[] args) {
String age = "20"; relatives @8
String relatives = "6";

App.changeValues(age, relatives);
@5 20
@8 6
String output = "age: " + age + "\nrelatives: "
+ relatives;

System.out.println(output); x @5 y @8
}

public static void changeValues(String x, String y) {


x = "100";
y = "200";
}
}

Note: Variables x, and age have the same


reference. Same for relatives and y
63. Primitive Data Types vs reference types
The exact same program, but with String instead of int! Your Computer’s Random-Access Memory

public class App {


age @5
public static void main(String[] args) {
String age = "20"; relatives @8
String relatives = "6";

App.changeValues(age, relatives);
@5 20
@8 6
String output = "age: " + age + "\nrelatives: "
+ relatives;

System.out.println(output); x @11 y @12


}

public static void changeValues(String x, String y) {


x = "100";
y = "200"; @11 100
}
}
64. Primitive Data Types vs reference types
The exact same program, but with String instead of int! Your Computer’s Random-Access Memory

public class App {


age @5
public static void main(String[] args) {
String age = "20"; relatives @8
String relatives = "6";

App.changeValues(age, relatives);
@5 20
@8 6
String output = "age: " + age + "\nrelatives: "
+ relatives;

System.out.println(output); x @11 y @12


}

public static void changeValues(String x, String y) {


x = "100";
y = "200"; @11 100 @12 200
}
}
65. Primitive Data Types vs reference types
The exact same program, but with String instead of int! Your Computer’s Random-Access Memory

public class App {


age @5
public static void main(String[] args) {
String age = "20"; relatives @8
String relatives = "6";

App.changeValues(age, relatives);
@5 20
@8 6
String output = "age: " + age + "\nrelatives: "
+ relatives;

System.out.println(output); x @11 y @12


}

public static void changeValues(String x, String y) {


x = "100";
y = "200"; @11 100 @12 200
}
}
66. Primitive Data Types vs reference types
The exact same program, but with String instead of int! Your Computer’s Random-Access Memory

public class App {


age @5
public static void main(String[] args) {
String age = "20"; relatives @8
String relatives = "6";

App.changeValues(age, relatives);
@5 20
@8 6
String output = "age: " + age + "\nrelatives: "
+ relatives;

System.out.println(output); output @11


}

public static void changeValues(String x, String y) {


x = "100"; @11 age: 20
y = "200"; relatives: 6
}
}
67. Primitive Data Types vs reference types
The exact same program, but with String instead of int! Your Computer’s Random-Access Memory

public class App {


age @5
public static void main(String[] args) {
String age = "20"; relatives @8
String relatives = "6";

App.changeValues(age, relatives);
@5 20
@8 6
String output = "age: " + age + "\nrelatives: "
+ relatives;

System.out.println(output); output @11


}

public static void changeValues(String x, String y) {


x = "100"; @11 age: 20
y = "200"; relatives: 6
}
} Terminal
age: 20
relatives: 6
68. Primitive Data Types vs reference types
Switch out String for int …

public class App {

public static void main(String[] args) {


int age = 20;
int relatives = 6;
App.changeValues(age, relatives);
String output = "age: " + age + "\nrelatives: " + relatives;
System.out.println(output);
}

public static void changeValues(int x, int y) {


x = 100;
y = 200;
}
}

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

Every time we change the String, the


reference of the name variable changes?
Why?
75. What about String?
Answer:
1. The String doesn’t change
2. String never changes. It’s immutable
3. A new String is created. And the variable
gets a reference to the new String
76. What about String?
// Person.java
public class Person {
private String name;
private String address;

public Person(String name, String address) {


this.name = name;
this.address = address;
}
}
77. What about String?
// App.Java
public class App {

public static void main(String[] args) {

Person person1 = new Person("Gary", "Paradisgatan 2");


Person person2 = new Person("Ham", "Paradisgatan 2");

String myAddress = "Paradisgatan 2";


String yourAddress = "Paradisgatan 2";

}
}
78. Primitive Data Types vs reference types
Person person1 = new Person("Gary", "Paradisgatan 2");
Person person2 = new Person("Ham", "Paradisgatan 2");

String myAddress = "Paradisgatan 2";


String yourAddress = "Paradisgatan 2";

Your Computer’s Random-Access Memory

myAddress @22 yourAddress @22


person1 @9
@9 person1 : Person
@1 Gary
name = @18
8
Address = @22
person2 @1 @2 Paradisgatan 2
0 @1 person2 : Person 2
0 name = @19
Address = @22
@1 Ham
9
79. What about String?
Force String to create two copies of the same String (with different references)
// App.Java
public class App {

public static void main(String[] args) {


String myAddress = new String("Paradisgatan 2");
String yourAddress = new String("Paradisgatan 2");

System.out.println(myAddress);
}
}

Representation in Eclipse debugger Representation in VSCode debugger


80. What about String?
Force String to create two copies of the same String (with different references)
// App.Java
public class App {

public static void main(String[] args) {


String myAddress = new String("Paradisgatan 2");
String yourAddress = new String("Paradisgatan 2");

boolean sameReference = myAddress == yourAddress;


boolean sameContent = myAddress.equals(yourAddress);

System.out.println("Same reference: " + sameReference);


System.out.println("Same content: " + sameContent);
}
}

Terminal In reality the two strings do not have the same


Same reference: false reference, even though they have the same content.
Same content: true The Eclipse debugger provides a better representation
of what is actually going on than the VSCode one does
81. What about String?
Same experiment without forcing Java to create separate strings
// App.Java
public class App {

public static void main(String[] args) {


String myAddress = "Paradisgatan 2";
String yourAddress = "Paradisgatan 2";

boolean sameReference = myAddress == yourAddress;


boolean sameContent = myAddress.equals(yourAddress);

System.out.println("Same reference: " + sameReference);


System.out.println("Same content: " + sameContent);
}
}

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

Your Computer’s Random-Access Memory Str


ing
Po
o l
myAddress @17

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.

Person person = null;


85. Null
Person person1 = new Person("Gary", "Paradisgatan 2");
Person person2 = null;

Your Computer’s Random-Access Memory

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;

boolean isPerson1Null = person1 == null;


boolean isPerson2Null = person2 == null;

System.out.println("Is person1 null? " + isPerson1Null);


System.out.println("Is person2 null? " + isPerson2Null);

Terminal
Is person1 null? false
Is person2 null? true
87. Null
Person person1 = new Person("Gary", "Paradisgatan 2");
Person person2 = null;

String name1 = person1.getName();


System.out.println(name1);

String name2 = person2.getName();


System.out.println(name2);

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

String name1 = person1.getName();


System.out.println(name1);

String name2 = person2.getName();


System.out.println(name2);

Terminal
Gary
null
89. Null
Person person1 = new Person("Gary", "Paradisgatan 2");
Person person2 = new Person();

String name1 = person1.getName();


System.out.println(name1);

String name2 = person2.getName();


int length = name2.length();
System.out.println(length);

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("", "");

String name1 = person1.getName();


System.out.println(name1);

String name2 = person2.getName();


int length = name2.length();
System.out.println(length);
System.out.println(name2);

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

Person person = new Person("Gary", "Paradisgatan 2");


String newName = "Ham";

App.renamePerson(person, newName);
}

public static void renamePerson(Person person, String newName) {


person.setName(newName);
}
92. Null
public static void main(String[] args) {

Person person = new Person("Gary", "Paradisgatan 2");


String newName = null;

App.renamePerson(person, newName);
}

public static void renamePerson(Person person, String newName) {


person.setName(newName);
}
93. Null
public static void main(String[] args) {

Person person = new Person("Gary", "Paradisgatan 2");

App.renamePerson(person, null);
}

public static void renamePerson(Person person, String newName) {


person.setName(newName);
}
94. Null
public static void main(String[] args) {

Person person = new Person("Gary", "Paradisgatan 2");

App.renamePerson(null, null);
}

public static void renamePerson(Person person, String newName) {


person.setName(newName);
}

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

public String getName() {


return name;
}

public void setName(String name) {


this.name = name;
}
}
96. Variable scope – loop scope
public class App {
public static void main(String[] args) throws Exception {

int methodScopedVariable = 0;

for (int i = 0; i < 10; i++) {


methodScopedVariable++;
}

// methodScopedVariable is still in scope here


System.out.println("methodScopedVariable: " + methodScopedVariable);

// i is out of scope here; trying to access it would result in a compilation error


System.out.println("Can we access i here? " + i);
}
}

You might also like