# Problem with .equals()

• 09-12-2012, 11:02 PM
trantommyd
Problem with .equals()
I can't figure out why my program thinks that the integer 1 is equal to integer 2 (and integer 3 and so on). What am I overlooking here?

Code:

import java.lang.*;

public class MyFour<T>
{
private T item1, item2, item3, item4;
private static boolean same;
private static String word = "word";

// "it's contructor recieves values for setting all four items."
public MyFour(T i1, T i2, T i3, T i4)
{
item1 = i1;
item2 = i2;
item3 = i3;
item4 = i4;
}

//a method that returns true if all 4 of the parameters are the same
public static boolean allEqual(MyFour ex)
{
same = false;

if((ex.item1).equals(ex.item3));
{
same = true;
}

System.out.println(ex.item1);
System.out.println(ex.item3);

return same;
}

//a method that shifts all the items up one position
public void shiftLeft(T i1, T i2, T i3, T i4)
{
T one, two, three, four;

one = i2;
two = i3;
three = i4;
four = i1;
}

public static void toString(MyFour ex)
{
String a = ex.item1.toString();
String b = ex.item2.toString();
String c = ex.item3.toString();
String d = ex.item4.toString();

System.out.println("(" + a + ", " + b + ", " + c + ", " + d + ")");
}

public static void main(String[] args)
{
MyFour<String> ex = new MyFour<String>(word, word, word, word);
toString(ex);
System.out.println(allEqual(ex));

MyFour<Integer> ex2 = new MyFour<Integer>(1, 2, 3, 4);
toString(ex2);
System.out.println(allEqual(ex2));
}
}

• 09-12-2012, 11:08 PM
trantommyd
I always figure it out minutes after posting here.....
I believe my problem lies with declaring the boolean value same as a static variable. I rewrote the whole allEquals() method to, very simply:

Code:

public boolean allEqual(){
if(item1.equals(item2)) return true;
return false;
}