Equals – why ? and why not ==

Object oriented programming allows for objects to be created on the fly with the keyword “new”. The new will create a new object on the heap memory space and then give that memory location to the variable name, for example.

class NewClass 
{
   private int x;
} ....
 
NewClass a = new NewClass();
NewClass b = new NewClass();

What is happening in memory is something like below, the a variable has a small space and so does b, because they are just “pointers” to the actual memory locations of the class that was created.

Variable Value
a 0x0102
b 0x0110

where as the memory on the heap space would actually hold the NewClass details for example.

Memory location Value
0x0102 NewClass – overhead details
0x0104 NewClass – x value
…. ….
0x0110 NewClass – overhead details
0x0112 NewClass – x value

So the actual value of a and b are just values within memory, this could give reason why the “==” (equals) does not work e.g.

if (a==b)

even if both of them have the same internal values of the NewClass x value, they are still actual pointing to different locations (which makes sense otherwise you are comparing the same object which would be silly). There is a few functions to do the comparsions of objects, Equals, Compare for example and if you implement these functions within your class you can then “compare” the two objects instead of the memory locations that are held within the variables (a 0x0102 and b 0x0110)

To implement a Equal function it would be something similar to

	public boolean Equals(Object obj)
	{
		if (x == ((ClassA)obj).xValue())
			return true;
                return false;
	}

where the obj (Object) passed is the same type as the Class e.g. NewClass and return true if they are same (after casting the object obj value to a ClassA structure).

Here is a bigger example with code for two java files, if you save this class as normal for java to have it the same as the class name e.g. ClassA.java

public class ClassA {
	private int x;
 
	public ClassA() { x =0;}
	public ClassA(int value) { x = value;}
 
	public int xValue() { return x; }
	public void setX (int value) { x = value;}
 
	public boolean Equals(Object obj)
	{
		if (x == ((ClassA)obj).xValue())
			return true;
		return false;
	}
 
	public int Compare(Object obj)
	{
		ClassA objA = (ClassA)obj;
		if (x > objA.xValue())
			return 1;
		else if (x < objA.xValue())
			return -1;
		else
			return 0;
	}
}

this file also includes the Compare function method, so the returns have to be normal to java comparison e.g. 1 means > (greater than) , 0 = same, -1 < (less than), it is up to you to implement to make sure that this kinder of comparison adhears to how you want the class is greater than another class, could be a X value as above, or could that you call the first class number 1 and the second of that type of class 2.. it is up to you. and then save this as Test.java

public class Test {
 
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		ClassA a = new ClassA();
		a.setX(10);
		ClassA a2 = new ClassA();
		a2.setX(10);
		if (a==a2)
			System.out.println("Yep the same");
		else
			System.out.println("nope not the same");
 
		if (a.Equals(a2))
			System.out.println("Yep the same");
		else
			System.out.println("nope not the same");
		a2.setX(11);
		int comparsion = a.Compare(a2);
		if (comparsion == 0)
			System.out.println("Same");
		else if (comparsion == 1)
			System.out.println("greater");
		else	// only -1 left.
			System.out.println("less than");
 
	}
 
}

compile them you just need to javac Test.java and that will compile the ClassA.java file as well, then to run

java Test
 
nope not the same
Yep the same
less than