Java equals, == and !=

Primitives:
int i = 1;
int ii = 1;
(i==ii) is true, (i!=ii) is false

Objects:
== compares references (true only if they are the same references, point to the same memory)
obj.equals(obj2) – by default compares references unless explicitly overridden (

String:
String s = “text”;
String ss = “text”;
(s==ss) is true, (s.equals(ss)) true

class A{}
A a = new A();
A aa = new A();
(a==aa) is false, (a.equals(aa)) is false

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s