0

I was reading about == operator in java and found that it used to compare memory reference the below example is from given link.

String obj1 = new String("xyz");

// now obj2 and obj1 reference the same place in memory
String obj2 = obj1;

if(obj1 == obj2)
   System.out.printlln("obj1==obj2 is TRUE");
else
  System.out.println("obj1==obj2 is FALSE");

Note in the code above that obj2 and obj1 both reference the same place in memory because of this line: “String obj2 = obj1;”. And because the “==” compares the memory reference for each object, it will return true. And, the output of the code above will be:

After that I write code randomly to check == operator but why it returning true in this example?

String obj1 = "ABC";
String obj2 = "ABC";
if(obj1 == obj2)
System.out.println("obj1==obj2 is TRUE");
else
System.out.println("obj1==obj2 is FALSE");

Does "ABC" string saved in the one memory place then obj1 and obj2 sharing that memory reference?

Even int also returning true.

int obj1=3;
int obj2=3;
  • Some reading: http://stackoverflow.com/questions/28251650/how-java-string-immutable –  Sep 27 '15 at 10:37
  • when you use double quotes to create string instead of new operator it first looks string pool and if it finds string with same string returns referance to it otherwise creates new one – Mustafa ASAN Sep 27 '15 at 10:37
  • @Murinik why mark as duplicate? even my question is different I edit it. –  Sep 27 '15 at 10:41
  • @EthicalThinker the answer and explanation is the same - marking as duplicate means people who ask questions that are semantically the same will be directed to an established answer pool. – ataulm Sep 27 '15 at 10:47
  • Haven't you ever read a book about Java and learned the difference between reference types and primitive types? – Tom Sep 27 '15 at 11:11
  • @Tom I know about primitive types and reference types. –  Sep 27 '15 at 11:14
  • @Tom Nice man, Understood, the == and equals() will make different for References type? –  Sep 27 '15 at 11:19
  • *"the == and equals() will make different for References type?"* -> *"I was reading about == operator in java and found that it used to compare memory reference"* ... what do you think? – Tom Sep 27 '15 at 11:28
  • @Tom I mean, equals for Objects not for int. 2nd compare memory reference mean, == used to compare if objects refers same memory place where literal stored. But I need to search why java used equals instead == also for objects. –  Sep 27 '15 at 13:30
  • *"But I need to search why java used equals instead == also for objects."* Maybe because the language designers thought: why should `==` behave differently depending on the variable type? – Tom Sep 27 '15 at 13:52
  • @Tom it should not and also not behaving differently, But I read that `==` perform _reference equality_. and I'm sure it comparing reference not exact value. –  Sep 27 '15 at 14:14
  • Yes, it shouldn't not behave differently, so are you still confused about *"But I need to search why java used equals instead == also for objects."*? – Tom Sep 27 '15 at 14:18

1 Answers1

1

Strings are a bit special as they use String interning. So yes, behind the screens those two strings have the same memory reference (but do not count on it for string comparison. See this question).

Replace your strings by

Object obj1 = new Object();
Object obj2 = new Object();

and you will get the expected output.

Community
  • 1
  • 1
Robin
  • 36,233
  • 5
  • 47
  • 99
  • ok, this for just String? `int obj1=3; int obj2=3;` this also printing true –  Sep 27 '15 at 10:37
  • 2
    @EthicalThinker That's because those aren't objects, but plain memory values ;-) – watery Sep 27 '15 at 10:41
  • @watery it's mean java will share each memory that repeats even, string int, double long and any other type without new keyword? –  Sep 27 '15 at 10:44
  • @NewBie Of course not! I mean, it's not that simple. Primitive types are treated differently than objects; e.g. when you pass an `int` to a method, the latter gets a *copy* of the value, whereas if you pass an object, the method receives a *reference* to it. But then, I think (I'm unsure though) that `Integer`s (and the others), being immutable, are held in some kind of cache. – watery Sep 27 '15 at 12:02
  • @watery why java introduce equals() method == is not enough also for objects? –  Sep 27 '15 at 13:55
  • @NewBie This is a questions & answers site: if you have more questions do a search, and if you're not satisfied then post a new question :-) – watery Sep 27 '15 at 14:31