-3

Why the Integer objects do not behave the way String objects behave?

I read that the reason was performance but can not understand how it would perform better?

Look at the code below for example :

public class MyClass{
    public static void main(String[] args){
        String one = "myString";
        String two = "myString";
        System.out.println(one == two); // true

        String oneLong = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus in leo at massa vehicula rhoncus quis eu mauris. Pellentesque non nulla convallis, tempus augue sed, sollicitudin risus. Aenean posuere nulla ipsum, at faucibus massa dignissim non. Duis felis felis, iaculis eu posuere id, elementum id nulla. Fusce tristique arcu vitae consectetur vehicula. Mauris tincidunt nunc placerat tortor rhoncus, eget venenatis felis dapibus. Sed scelerisque ligula congue ligula elementum hendrerit. Proin et mauris vestibulum, rutrum ante ut, sollicitudin massa. Fusce tempus mattis eleifend. Phasellus ut ante turpis. Suspendisse eu leo nec elit ornare rhoncus sed nec ex. In at tellus mi.";
        String twoLong = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus in leo at massa vehicula rhoncus quis eu mauris. Pellentesque non nulla convallis, tempus augue sed, sollicitudin risus. Aenean posuere nulla ipsum, at faucibus massa dignissim non. Duis felis felis, iaculis eu posuere id, elementum id nulla. Fusce tristique arcu vitae consectetur vehicula. Mauris tincidunt nunc placerat tortor rhoncus, eget venenatis felis dapibus. Sed scelerisque ligula congue ligula elementum hendrerit. Proin et mauris vestibulum, rutrum ante ut, sollicitudin massa. Fusce tempus mattis eleifend. Phasellus ut ante turpis. Suspendisse eu leo nec elit ornare rhoncus sed nec ex. In at tellus mi.";
        System.out.println(oneLong == twoLong); // true

        Integer first = 1;
        Integer second = 1;
        System.out.println(first == second); // true

        Integer third = 500;
        Integer fourth = 500;
        System.out.println(third == fourth); // false
    }
}

Here are the questions I found but no response was given there :

Why Integer In Java is Immutable

Is Integer Immutable?

trincot
  • 317,000
  • 35
  • 244
  • 286
Yassin Hajaj
  • 21,337
  • 9
  • 51
  • 89
  • Take a look at `IntegerCache`. – Tunaki Oct 07 '15 at 07:08
  • 2
    You seem to be comparing object references and not the object content – MadProgrammer Oct 07 '15 at 07:08
  • 3
    Please lookup what "immutable" _really_ means. – Tom Oct 07 '15 at 07:11
  • 1
    Integer objects are completely immutable, not "up to a point". How many instances of the objects are created is something different - and in any case, Strings can also have multiple instances with the same content. – Erwin Bolwidt Oct 07 '15 at 07:12
  • Why the downvotes? I edited it but the question still has no answer... – Yassin Hajaj Oct 07 '15 at 07:16
  • 1
    Your original question did sound like you didn´t knew what immutable actually means. For an answer you could check [this](http://stackoverflow.com/questions/3451145/when-are-java-strings-interned) for String interning. The reason why the integer class doesn´t do it that way is simply because it just stores the values for optimazition on a micro level. also [this](http://stackoverflow.com/questions/10149959/using-operator-in-java-to-compare-wrapper-objects) for Integer interning – SomeJavaGuy Oct 07 '15 at 07:22
  • To be honest, this question is a much better duplicate candidate: [Why does 128==128 return false but 127==127 return true in this code?](http://stackoverflow.com/questions/1700081/why-does-128-128-return-false-but-127-127-return-true-in-this-code) – Tom Oct 07 '15 at 07:38
  • *"Why the downvotes?"* First, maybe because the misuse of "immutable"? Or maybe because you already knew the range for Integers where the cache is applied (even if you didn't knew it was a question about caching), so a simple search for ["127 128"](http://stackoverflow.com/search?q=127+128) would have told you what you wanted to know. – Tom Oct 07 '15 at 07:44

1 Answers1

1

Generally is a good idea to mantain an object as immutable as possible because an immutable object can be shared without problems in a multithreading enviroment.

Creating n copies of the same Integer is possible, ma that copies are immutable. Note that also for String is possible to create n different copies of the same String. To do that you need to explicitly use the new keyword.

Two objects are the same if compared with the operator == returns true. Two objects have the same content if compared with the method equals returns true.

obj1 == obj2       // if true obj1 and obj2 are the same
obj1.equals(obj2)  // if true obj1 and obj2 have the same content 
                   // (can be the same or not)

In your example you have two Integer with the same content (so equals returns true) but different memory locations (so == returns false)

Davide Lorenzo MARINO
  • 26,420
  • 4
  • 39
  • 56