-1
import java.lang.String;
public class Test {
    public static void main(String[] args) {
        String a1="ka";
        String a2="ka"; 
        System.out.println("a1==a2?  "+(a1==a2));
        String a3="k";
        String a4=new String("k");
        System.out.println("a3==a4?  "+(a3==a4))
        System.out.println("a3==a4?  "+(a3==a4.intern()));
        String a5="k";
        String a6=a4+"a";
        System.out.println("a1==a6?  "+(a1==a6));
    }
}

Output that i got:

a1==a2?  true
a3==a4?  false
a3==a4?  true
a1==a6?  false
  • a1===a2 is true as line 5 will not create new String literal in string pool area.Only reference to previously created string is returned.
  • a3==a4? false as a4 will have refernce to the String object instead of the string in the string in string pool area. My question is if a3 is referencing the string constant instead of the String object, how is it able to use the methods of the String class?
  • a4.intern() will return the reference to the string in the string pool which happens to be same as a3
  • a6=a4+"a" will create a new string "ka". But this actually make use of StringBuilder class and its append method . It is then converted to string using toString(). Does this process store the newly created string "ka" in the string pool area? Since the string is already in the pool the code at line 12 should return the reference to it. So the a1==a6 should be true.rt? I am new to java. Please guide me where i am doing the mistake?
Patrick
  • 1,717
  • 7
  • 21
  • 28
smasher
  • 294
  • 2
  • 17
  • here is the answer of the "weird" behav.... http://stackoverflow.com/a/35899981/982161 – ΦXocę 웃 Пepeúpa ツ Apr 16 '16 at 13:58
  • `a6=a4+"a"` will create a new String `ka` on the heap irrespective of whether `"ka"` exists in the String constants pool. This reference will be used to compare against the `"ka"` in String pool, so you will get false. Note that `"a"` will be added to the String constants pool when you do this. – TheLostMind Apr 16 '16 at 13:59
  • 2
    @smasher Likely because this question gets asked at least once a day. – Carcigenicate Apr 16 '16 at 13:59
  • i know == compares references instead of comparing the content of the String.. – smasher Apr 16 '16 at 14:00
  • @TheLostMind Can you elaborate why the new string will be created in heap rather that in string pool area where string literals are generally stored.. is it due to the + operator that internally make use of StringBuilder class and its methods? – smasher Apr 16 '16 at 14:10
  • @smasher - Yes, only String literals and and Strings which are interned (by calling `intern()`) will be put in the String constants pool. Other strings which are created go into the heap and can be easily GCed – TheLostMind Apr 16 '16 at 14:21
  • @TheLostMind thanks :) – smasher Apr 16 '16 at 14:33
  • @smasher - String literals are just like any other String object. They are created and placed in the String constants pool when the class is initialized. – TheLostMind Apr 16 '16 at 14:34
  • @TheLostMind Ok i got it... Thanks – smasher Apr 16 '16 at 14:41

1 Answers1

-1

You are comparing the Strings wrongly (because you are in fact comparing references)

String Class in java is defined in java.lang package and it is exactly that, a class and not a primitive like int or boolean.

String is immutable and final in Java and in this case JVM uses String Pool to store all the String objects.

What are different ways to create String Object?

We can create String object using new operator like any normal java class or we can use double quotes (literal assignment) to create a String object.


To your Question:

When we create a String using double quotes, JVM looks in the String pool to find if any other String is stored with same value. If found, it just returns the reference to that String object else it creates a new String object with given value and stores it in the String pool.

When we use new operator, JVM creates the String object but don’t store it into the String Pool. We can use intern() method to store the String object into String pool or return the reference if there is already a String with equal value present in the pool.

So when you do

String s1 = "abc";
String s2 = "abc";

those are checked in the StringPool and since s1 already exist there, s2 will take the same reference, hence, s1 ==s2 is true.

but when you do:

String s3 = new String("abc");
String s4 = new String("abc"); 

you are using the new operator, therefore the JVM is not checking if there is an string already in the heap, it will just allocate a new space for s4, so is s3==s4 ??? of course no.

Please take a look at the image below for a more illustrative example.

enter image description here

Community
  • 1
  • 1
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
  • 5
    [copying](http://stackoverflow.com/a/35899981/1864167) [your answer](http://stackoverflow.com/a/36071438/1864167) [across duplicate questions](http://stackoverflow.com/a/35813942/1864167) is [absolutely not done](http://stackoverflow.com/a/36177249/1864167) – Jeroen Vannevel Apr 16 '16 at 14:06
  • voting down all my answer is not a way either... – ΦXocę 웃 Пepeúpa ツ Apr 16 '16 at 14:08
  • I didn't down vote you but are you 100% sure what you said is correct? – user3437460 Apr 16 '16 at 14:11
  • 4
    If you think that your answer to some other *similar* question helps the OP, then drop a comment linking to your answer. Copying and pasting the exact same answer it is not acceptable – TheLostMind Apr 16 '16 at 14:17
  • @JeroenVannevel is this stated anywhere on the website? [The help center](http://stackoverflow.com/help/answering) says nothing about Copy-pasting your own answer. It mentions plagiarism but only to the effect of other people's work. – Norsk Apr 16 '16 at 14:35
  • @ΦXoce웃Пepeúpa We can use intern() method to store the String object into String pool . Do you mean using intern() will move the String object in heap area to String private area? – smasher Apr 16 '16 at 14:40
  • 4
    Sorry but copy-pasting answers should absolutely not be done. It is inacceptable and very poor behavior. If it's a duplicate, close it as such. Do NOT copy-paste answers. – Tunaki Apr 16 '16 at 17:03