-5
String a="ABC";
String a2="ABC";

//--------------------------------------

String b=new String("ABC");    
String b2=new String("ABC");

//-----------------------------------------

int c=5;
int c2=5;

//-------------------------------------
int d=new Integer(5);    
int d2=new Integer(5);

//----------------------------------------

a and a2 is pointer,memory same, b and b2 is object not same memory, a!=b, and b!=b2, why? c=d and d=d2, new String is object and new Integer not object ?

Nomesh DeSilva
  • 1,649
  • 4
  • 25
  • 43
  • 3
    Well, you stored `new Integer` in an `int`, so it got immediately unboxed. If you had stored them in `Integer` variables it would have been different. – Louis Wasserman May 27 '15 at 17:12

2 Answers2

3

new String is object and new Integer not object

Because you immediately assigned the Integers to a primitive type, not an object type. If you had written

Integer d=new Integer(5);

Integer d2=new Integer(5);

then d != d2 as expected, not least because the results of any two distinct invocations of new will be != to each other.

Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413
  • You might add what happens due to `Integer a = 1; Integer b = 1; if( a == b )...;` – laune May 27 '15 at 17:32
  • Eh. For myself, I'd prefer what happens there to be a compile error. – Louis Wasserman May 27 '15 at 17:33
  • @laune: As soon as you've used `==` to compare two objects with useful `.equals` implementations, you've done something wrong. – Louis Wasserman May 27 '15 at 17:47
  • Now I understand your first comment. But testing for object identity is still necessary. You'd need another operator if == should cause a compile error. – laune May 27 '15 at 17:53
  • Eh. Objects that care about reference equality should inherit it in their `.equals` method, so you can use `.equals` everywhere. There are extremely rare cases where it's necessary, so I could live with it being a suppressable warning. – Louis Wasserman May 27 '15 at 17:58
  • No - you need the identity check in addition to equality. Independently. – laune May 27 '15 at 19:36
  • Explain where that's actually necessary. – Louis Wasserman May 27 '15 at 19:57
  • The javadoc for IdentityHashMap has a couple of applications. Also, see Drools for the difference between the two forms of equalsBehavior: identity and equality. – laune May 28 '15 at 14:06
2

Case 1

String a="ABC";
String a2="ABC";

This string will added in to String Pool

so when creating new String in String pool jvm do check whether the same string exists in string pool or not?, if exists the new string will refer the same string pool string

So a and a2 point the same memory

Case 2

String b=new String("ABC");    
String b2=new String("ABC");

in this scenario new string object will be added. if you want point the same memory for above statement the you have to use like

String b=new String("ABC").intern();    
String b2=new String("ABC").intern();

So b and b2 are not the same object

Case 3

    int c=5;
    int c2=5;

are primitives

Case 4

   int d=new Integer(5);    
   int d2=new Integer(5);

case 3 and case 4 you are comparing with primitive value with Integer object reference values so it dors not match

So your case also return false

References here

Community
  • 1
  • 1
Sasikumar Murugesan
  • 4,412
  • 10
  • 51
  • 74
  • 2
    Your explanation for case 4 has a problem. There is no cache used if you create the instances using the `new` keyword. So `new Integer(5) == new Integer(5)` is `false`. – Tom May 27 '15 at 20:59