Two instance variables, name
and salary
, along with a simple constructor. (Assume setters & getters written beneath).
public class Employee {
private String name;
private int salary;
public Employee(String name, int salary) {
this.name = name;
this.salary = salary;
}
... // getters & setters, too...
}
If we create a new ob Employee empRef = new Employee("John", 1000);
we can represent the memory abstractly with the following diagram
null
value is represented as a box with a line crossed through it, or by the word null
.public class Employee {
private String name;
private int salary;
public Employee(String name, int salary) {
this.name = name;
this.salary = salary;
}
}
public class Department {
private Employee chair;
private String deptName;
public Department(String newDeptName, Employee newChair) {
deptName = newDeptName;
chair = newChair;
}
}
Draw the memory diagram after the following program is executed:
String name = "Lomax";
Employee l = new Employee(name, 300);
int salary = 200;
Employee s = new Employee("Stoner", salary);
Draw the memory diagram after the following program is executed:
String name = "Lomax";
Employee empRef = new Employee(name, 300);
Department deptRef = new Department("English", empRef);
.
) to access a field or method of an objectString myName = empRef.getName()
dereferences empRef
to call the getName
method for that object
null
A reference must be assigned a pointee before dereference operations will work.
NullPointerException
when attempting to dereference.null
: special reference value that encodes the idea of "pointing to nothing”, which is also the initial value of referencesnull
An assignment of one reference variable to another makes them the same pointee
Employee empRef = new Employee(“john”, 1000);
Employee second = empRef;
second == empRef; // true!
Two references which both refer to a single pointee are said to be aliasing
empRef
is an alias for second
" or "second
is an alias for empRef
"Shallow copy (of a reference) is achieved through aliasing
Deep copy creates a new copy of the pointee that has an identical structure but exists at a different location in memory.
Shallow copy: copies only the reference
Employee second = empRef;
Deep copy: constructs a new object and copies the values of the other object's instance variables.
Employee deepCopy = new Employee(empRef.getName(), empRef.getSalary());
Employee empRef = new Employee("John", 1000);
Employee second = empRef;
String johnName = empRef.getName();
int johnSalary = empRef.getSalary();
Employee deepCopy = new Employee(johnName, johnSalary);
Double equals (==
) checks if two reference variables are referencing the same object (referential equality)
true
for shallow copiesfalse
for deep copiesAn equals method checks if the values (data fields) of the two objects are the same (structural equality)
true
for shallow copiestrue
for deep copiesequals()
public class Employee {
private String name;
private int salary;
public Employee(String name, int salary) {
this.name = name;
this.salary = salary;
}
public boolean equals(Employee emp) {
if(this == emp)
return true;
else{
return this.name == emp.name &&
this.salary == emp.salary;
}
}
}