Java Reference
In-Depth Information
Solution 1
To determine whether the two object references point to the same object, make use of
the == and != operators. The following solution demonstrates the comparison of two
object references to determine whether they refer to the same object.
// Compare if two objects contain the same values
Team team1 = new Team();
Team team2 = new Team();
team1.setName("Jokers");
team1.setCity("Crazyville");
team2.setName("Jokers");
team2.setCity("Crazyville");
if (team1 == team2){
System.out.println("These object references refer to
the same object.");
} else {
System.out.println("These object references do NOT
refer to the same object.");
}
// Compare two objects to see if they refer to the same
object
Team team3 = team1;
Team team4 = team1;
if (team3 == team4){
System.out.println("These object references refer to
the same object.");
} else {
System.out.println("These object references do NOT
refer to the same object.");
}
The results of running the code:
Search WWH ::




Custom Search