Pre-knowledge

 

For objects, both commands work actually in the same way as String Literals, but the way in which they use memory could be something that is harder to understand.

creating a situation with objects

Adding objects will look like this. The objects could contain the same value (see code –> “ey bro”) but have different memory locations. objects java

*the image shows that object 1 and object 2 are not in the same spot, this means that they have different memory locations.

package Compare;

class Compare {
	
	void start() {
		TestObject object1 = new TestObject();
		TestObject object2 = new TestObject();		
	}

	public static void main(String[] args) {
		new Compare().start();
	}
}


class TestObject {
	String text = "ey bro";
}

Scenario’s

1. comparing objects

Object 1 is not equal to object 2 (in this case) because, every object is saved in a new space in memory.compare objects java

You can use == and .equals():

==
package Compare;

class Compare {
	
	void start() {
		TestObject object1 = new TestObject();
		TestObject object2 = new TestObject();
		if (object1 == object2) {
			System.out.println("true");
		} else {
			System.out.println("false");
		}
		
	}

	public static void main(String[] args) {
		new Compare().start();
	}
}


class TestObject {
	
	String text = "ey bro";
}

output is false

 

.equals()
package Compare;

class Compare {
	
	void start() {
		TestObject object1 = new TestObject();
		TestObject object2 = new TestObject();
		if (object1.equals(object2)) {
			System.out.println("true");
		} else {
			System.out.println("false");
		}
		
	}

	public static void main(String[] args) {
		new Compare().start();
	}
}


class TestObject {
	
	String text = "ey bro";
}

output is false

2. comparing references

Comparing the memory location of one and the same object will give true

java objects

==
package Compare;

class Compare {
	
	void start() {
		TestObject object1 = new TestObject();
		TestObject object2 = object1;
		if (object1.equals(object2)) {
			System.out.println("true");
		} else {
			System.out.println("false");
		}
		
	}

	public static void main(String[] args) {
		new Compare().start();
	}
}


class TestObject {
	
	String text = "ey bro";
}

output is true

 

.equals()
package Compare;

class Compare {
	
	void start() {
		TestObject object1 = new TestObject();
		TestObject object2 = object1;
		if (object1 == object2) {
			System.out.println("true");
		} else {
			System.out.println("false");
		}
		
	}

	public static void main(String[] args) {
		new Compare().start();
	}
}


class TestObject {
	
	String text = "ey bro";
}

output is true

You can use == and .equals():

3. Comparing object values

But sometimes it’s your goal to compare the value of objects as shown in the code of step 4.

package Compare;

class Compare {
	
	void start() {
		TestObject object1 = new TestObject();
		TestObject object2 = new TestObject();
		if (object1 == object2) {
			System.out.println("true");
		} else {
			System.out.println("false");
		}
		
	}

	public static void main(String[] args) {
		new Compare().start();
	}
}


class TestObject {
	
	String text = "ey bro";
}

Mostly this is what you imagine in your head:

compare object values

WRONG, the value in the objects is not a new text but a reference to a text

Actually, this is your situation:

java reference

You forgot to specify what you want to compare. You created 2 objects, therefore you have 2 objects in space. However, the text is saved in one place. Therefore, specify the variable you want to compare. And you will get your solution:

==
package Compare;

class Compare {
	
	void start() {
		TestObject object1 = new TestObject();
		TestObject object2 = new TestObject();
		if (object1.text == object2.text) {
			System.out.println("true");
		} else {
			System.out.println("false");
		}
		
	}

	public static void main(String[] args) {
		new Compare().start();
	}
}


class TestObject {
	
	String text = "ey bro";
}

output is true

 

.equals()
package Compare;

class Compare {
	
	void start() {
		TestObject object1 = new TestObject();
		TestObject object2 = new TestObject();
		if (object1.text.equals(object2.text)) {
			System.out.println("true");
		} else {
			System.out.println("false");
		}
		
	}

	public static void main(String[] args) {
		new Compare().start();
	}
}


class TestObject {
	
	String text = "ey bro";
}

output is true