0

I have been asked this question in an interview, I said both would return false. but when I crossed checked it one was true other was false

can anyone explain it?

        String s1 = "H";
        String s2 = "e";
        String s3 = s1+s2;
        String s4 = "He";
        System.out.println(s3 == s4);//false

        String s5="h";
        String s6="h";
        System.out.println(s5 == s6);//true
Cœur
  • 37,241
  • 25
  • 195
  • 267

6 Answers6

0

String comparision needs to be done using

string.equals function == compares the references.

s5 == s6 returns true due to "h" being a constant

nandeesh
  • 24,740
  • 6
  • 69
  • 79
0

I suspect the answer is that a string pool is being used, as strings are often re-used they are sometimes allocated the same piece of memory. So, assigning s5 and s6 the same value on initialization will allocate them the same address, whereas assigning a string to be the concatenation of two other strings will always generate a new address.

Zack Newsham
  • 2,810
  • 1
  • 23
  • 43
0
String s3 = s1+s2;

That string realized at run time and created a reference of that in heap. when we concat strings with (+) operator JVM returns new StringBuilder(string...).toString()

Where as in second case

String s5="h";
String s6="h";

s5 and s6 both are resolved at compile time and pointing to same reference((Constant pool)).

Helpful :https://stackoverflow.com/a/19419194/1927832

Community
  • 1
  • 1
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
  • why does it reference same memory location in case of 2 one???can you explain brief about string pool what here you are referring to constant pool – Ratika Gupta Apr 17 '14 at 07:12
  • Ratika, Here you go http://stackoverflow.com/questions/17489250/how-can-a-string-be-initialized-using/17489410#17489410 – Suresh Atta Apr 17 '14 at 07:53
0

Use the String.equals(String other) function to compare strings, not the == operator.

The function checks the actual contents of the string, the == operator checks whether the references to the objects are equal. Note that string constants are usually "interned" such that two constants with the same value can actually be compared with ==, but it's better not to rely on that.

Java String.equals versus ==

Community
  • 1
  • 1
Tony Meng
  • 353
  • 4
  • 12
0

The "==" operator returns true if the object addresses are the same. At compile time all identical literal Strings are reduced to the same String object. Therefore:

String s1 = "A";
String s2 = "A"; // s1 == s2: points to the same address as s1

String s3 = "AA"
String s4 = s1+s2; // s3 != s4: the literal "AA" is a different object

String.equals() compares the String values, not the addresses.

-1

you can't use == on strings, you need to use the String.equals function. you can use it like that:

        String s1 = "H";
        String s2 = "e";
        String s3 = s1+s2;
        String s4 = "He";
        System.out.println(s3.equals(s4));//false

        String s5="h";
        String s6="h";
        System.out.println(s5.equals(s6));//true
Bary12
  • 1,060
  • 9
  • 23