You are on page 1of 14

Software Project Spring 2023

Software Project (Java Programming)

Lecture 3. Reference Type in Java

Joon-Woo Lee

School of Computer Science and Engineering


College of Software
Chung-Ang University
Software Project Spring 2023

Primitive Type vs Reference Type

• Java's data types are largely divided into primitive types and
reference types.
• The reference type is a type that refers to the address of an object,
and includes
• array type
• enum type
• class
• interface type.

2
Software Project Spring 2023

Primitive Type vs Reference Type


Data Type

Primitive Type Reference Type

Integer
Array Type
byte char short
Enum Type
int long
Class
Floating-point
Interface
float double

Boolean

boolean
3
Software Project Spring 2023

Primitive Type vs Reference Type

• The difference between the default type and the reference type is the
stored value in the variable.
• Default types store the values themselves, but reference types store
the memory address at which the object was created.
• Variables is allocated in the stack.
• The object itself of the reference type is allocated in the heap.

Primitive Type Reference Type

Value Address Address Object

4
Software Project Spring 2023

Primitive Type vs Reference Type


• The following figure represents this situation.
• int age = 25;
• double price = 100.5;
• String name = “Java Programming”;
• int[] numarr = {1, 2, 3};
Heap
Stack address: 100 String object

reference name 100 “Java Programming”


type numarr 200
price 100.5 address: 200
primitive
type age 25
4 6 2

Array object
5
Software Project Spring 2023

Runtime Data Area


• When the JVM is operated by the java command, the JVM uses the
runtime data area allocated by the operating system as follows.

Method Thread-1 Thread-1


Class-1 Class-n
constants, constants, Stack Stack
static fields ... static fields
Frame-n Frame-n
methods, methods,
construtors construtors variable-n variable-n

variable-1 variable-1

...
Heap

...

...
“Java Programming” “Apple”
Frame-1 Frame-1
1 2 3 variable-n variable-n

Object-1
variable-1 variable-1

Object-2

6
Software Project Spring 2023

Runtime Data Area

• Method area
• The area where the contents of the byte code file are stored
• Heap area
• The area where the object is created
• Stack area
• The area where the frames and variables that are generated each time the
method is called are stored

7
Software Project Spring 2023

==, != Operator for Reference Type


• The ==, != operator examines whether the values of the variables are
the same or not.
• Since the value of the reference type variable is the address of the
object, the ==, != operator for the reference type variable serves to
compare the address.
• refVar1 == refVar2 : false
• refVar2 == refVar3 : true
Heap
Stack address: 100 String object

refVar1 100 “Java Programming”


refVar2 200
refVar3 200 address: 200

4 6 2

Array object
8
Software Project Spring 2023

==, != Operator for Reference Type


package ch05.sec03;

public class ReferenceVariableCompareExample {


public static void main(String[] args) {
int[] arr1;
int[] arr2;
int[] arr3;

arr1 = new int[] { 1, 2, 3 };


arr2 = new int[] { 1, 2, 3 };
arr3 = arr2;

System.out.println(arr1 == arr2);
System.out.println(arr2 == arr3);
}
}
9
Software Project Spring 2023

null and NullPointerException


• The reference type variable may have a null value indicating that the
address is not yet stored.
• We can check whether a reference type variable is null value.
• refVar1 == null
• NullPointerException error means that you tried to use the data or method
in the object when the variable is null value.

Stack address: 100 String object

refVar1 100 “Java Programming”


refVar2 200
refVar3 null address: 200

4 6 2

Array object
10
Software Project Spring 2023

null and NullPointerException


package ch05.sec04;

public class NullPointerExceptionExample{


public static void main(String[] args) {
int[] intArray = null;
// intArray[0] = 10;

String str = null;


// System.out.println(str.length);
}
}
11
Software Project Spring 2023

Garbage Object and Garbage Collector


• In some cases, null is intentionally substituted for the reference type
variable.
• If null is substituted for a variable, the object will no longer be available
because it will lose its address.
• If an object is not referenced by any variable, the object becomes an object
that cannot be used by the program.
• Java treats these objects as garbage object and automatically removes
them by running a garbage collector.
Stack address: 100

refVar1 100 “Java Programming”

refVar1 null

Garbage object
12
Software Project Spring 2023

String Type
• String is generated as a String object in Java.
• Java is designed to share a String object if the string literal is the same.
• String refVar2 = “Python”; String refVar3 = “Python” (left figure)
• If you create an object with new operator, which is the operator that creates a
new object, you can create an object with the same string but different object.
• String refVar2 = new String(“Python”);
• String refVar3 = new String(“Python”); (right figure)
Heap Heap
Stack address: 100 String object Stack address: 100 String object
“Java Programming”
refVar1 100 “Java Programming” refVar1 100
address: 200
refVar2 200 refVar2 200
address: 200 “Python”
refVar3 200 refVar3 300
address: 300
“Python”
“Python”

13
Software Project Spring 2023

Array Type
• Array is generated as an array in Java.
• The array itself is generated in the heap area.
• The variable includes the address of the array.
• The array only manages values of the same type.
• The length of the array cannot be increased or decreased.

Stack
address: 200

refVar2 200
0 1 2 3 4 5 6

Array object
14

You might also like