Wrong output when comparing using arrayList.get()


while comparing like if( a.get(i) != b.get(j))

for input [1, 1, 1], output is [1]
for input [5000, 5000, 5000], output is [5000, 5000, 5000]

if stored in int variable first and then compared, i.e.,
int first = a.get(i);
int second = b.get(j);
if(first != second)

output is fine.
why that happens??


I think the reason is == operator compares the reference not the content. while equals complares the content not object reference. as you created two integers so the equals compares their address.
well im also learning so maybe im wrong


== does an object comparison.
Use Integer.compare(a.get(i), a.get(j)) instead


Can you please explain why it’s working for input [1, 1, 1] and not for [5000, 5000, 5000] , as in both cases == will do integer object comparison.