Wrong output when comparing using arrayList.get()


#1

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??


#2

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


#3

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


#4

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.