0

I am trying to verify if the references are equal with below code.

package com.test.oracleinterviewquestions;

public class StringTest {

  public static void main(String args[]){
    StringTest obj=new StringTest();
    obj.testString();
  }

  public void testString(){
    String s1=new String("you cant change me!");
    String s2=new String("you cant change me!");
    System.out.println("s1==s2: is "+s1==s2 );

    String s3="you cant change me!";
    System.out.println("s1==s3: is "+s1==s3);

    String s4="you cant change me!";
    System.out.println("s3==s4: is "+s3==s4);
    System.out.println("---------------------");
   }
}

Lets refer to the values as "yccm" in short for for "you cant change me!". As S1 is created, a entry will be created in heap with rerefence s1 and another one entry in scp but with out reference.

Now, when i create S3, it will point to the existing value in SCP which got created for s1, in scp ,also, when i define S4, it will also point to existing YCCM value in SCP. But, when i print S3==S4, its returning false. please let me know whats the behavior

enter image description here

talex
  • 17,973
  • 3
  • 29
  • 66
Rajaji
  • 133
  • 2
  • 14

6 Answers6

4

a+b==c gets parsed as (a+b)==c, not a + (b==c). I.e, you need parentheses around the comparisons.

Ture Pålsson
  • 6,088
  • 2
  • 12
  • 15
  • public void testString(){ String s1=new String("you cant change me!"); String s2=new String("you cant change me!"); System.out.println("-->" + s1==s2 ); System.out.println("---------------------"); final String s3="you cant change me!"; System.out.println("2nd "+ (s1==s3)); System.out.println("---------------------"); String s4="you cant change me!"; System.out.println("3rd "+ (s3==s4)); System.out.println("---------------------");} – Rajaji Jan 17 '19 at 10:44
0

please update your method as below:

public void testString(){
String s1=new String("you cant change me!");
String s2=new String("you cant change me!");
System.out.println("s1==s2: is "+(s1==s2) );

String s3="you cant change me!";
System.out.println("s1==s3: is "+(s1==s3));

String s4="you cant change me!";
System.out.println("s3==s4: is "+(s3==s4));
System.out.println("---------------------");
}

You will get output as below:

s1==s2: is false
s1==s3: is false
s3==s4: is true
---------------------

NOTE: While concatenation use braces

TheSprinter
  • 1,523
  • 17
  • 30
0

Whenever you play around with String, refrain adding any other hardcoded String in sysout, so as to avoid unwanted concatenation. OR use braces while concatenating String variables. In your case (s3==s4) will yield desired result.

Nisarg Patil
  • 1,509
  • 1
  • 16
  • 27
0

Java Operator precedence must be taken into account.

The expresion "s1==s2: is "+s1==s2 will be interpreted as ("s1==s2: is "+s1)==s2, this is, "s1==s2: is you cant change me!" == "you cant change me!", which is false, as it was pointed in comments.


EDIT

Interestingly enough, the expresion "s1==s2: is you cant change me!" == "you cant change me!" is evaluated as false not (only) because the values, but because they point two distinct object into SCP.

alseether
  • 1,889
  • 2
  • 24
  • 39
0

Both the Strings are not equal because of new operator

String s1=new String("you cant change me!"); String s2=new String("you cant change me!");

it would be equal it was String s1="you cant change me!"; String s2="you cant change me!";

Asit would refer to the same object from the string pool

0

Your logic is correct but here the trick is

String s3 = "you cant change me!"
String s4 = "you cant change me!";
System.out.println("s3==s4: is "+s3==s4);

this first appending string s3==s4: is to s3 and then it is comparing.

In simpler words

System.out.println("s3==s4: is "+s3==s4); 

is same as

String s5 = "s3==s4: is "+s3;
System.out.println(s5==s4)

if you print only

System.out.println(s3==s4); result will be true

Hope I am correct.

Doctor Who
  • 747
  • 1
  • 5
  • 28