-2

if we made three strings in java through string literal String A = "hel"+"lo"; String B = "lo"; String C = "hel"+B; . A == C be true but output is false.. as both will share same memory in stringpool. i am bit confused why this is happening.

Code that prints false:

public class Main {

    public static void main(String[] args) {
        String A = "hel" + "lo";
        String B = "lo";
        String C = "hel" + B;
        System.out.println(A == C);
    }
}

Code that prints true:

public class Main {
    public static void main(String[] args) {
        String A = "hel" + "lo";
        String B = "lo";
        String C = "hel" + "lo";
        System.out.println(A == C);
    }
}

Why do I get different results as both are the same?

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • `A` `B` and `C` are three different object. – Scary Wombat Oct 19 '21 at 07:38
  • Why would you expect `A` and `C` to share the same `String` object? – luk2302 Oct 19 '21 at 07:38
  • beacuse string created by literal having same content share memory in stringpool – vishal joshi Oct 19 '21 at 07:40
  • 1
    I think this question is **not** about how to compare strings in Java. I think the question is related to how exactly the string pool works when two string literals are appended. – MC Emperor Oct 19 '21 at 07:50
  • 1
    `String C = "hel" + B;` is not a string constant because the string variable `B` is not `final` and therefore doesn't count as string constant. Only if you write `final String B = "lo";` is `B` a string constant and only then is `String C = "hel" + B;` a constant string expression that the compiler evaluates. – Thomas Kläger Oct 19 '21 at 07:55
  • 1
    See [JLS § 4.3.3](https://docs.oracle.com/javase/specs/jls/se17/html/jls-4.html#jls-4.3.3). – MC Emperor Oct 19 '21 at 08:04

2 Answers2

0

The problem in this example

public class Main {
    public static void main(String[] args) {
        String A = "hel" + "lo";
        String B = "lo";
        String C = "hel" + B;
        System.out.println(A == C);
    }
}

is that the variable B is mutable and therefore the expression "hel" + B is not a constant expression.

The compiler translates the line String C = "hel" + B; into something similar to

        String C = "hel".concat(B);

which results in a new string object that is different from the string literal "hello".


If you turn B into a constant variable and write

public class Main {
    public static void main(String[] args) {
        String A = "hel" + "lo";
        final String B = "lo";
        String C = "hel" + B;
        System.out.println(A == C);
    }
}

then the expression "hel" + B is a constant expression and the compiler can compute the value of this expression.

Thomas Kläger
  • 17,754
  • 3
  • 23
  • 34
-1

The Java String class equals() method compares the two given strings based on the content of the string. The == operator compares references not values. Try to understand the below program, you will know the difference.

 String A = "hel" + "lo";
 String B = "lo";
 String C = "hel" + B;
 System.out.println(A.equals(C)); //true 

class Teststringcomparison{  
 public static void main(String args[]){  
   String s1="Sachin";  
   String s2="Sachin";  
   String s3=new String("Sachin");  
   System.out.println(s1==s2);//true (because both refer to same instance)  
   System.out.println(s1==s3);//false(because s3 refers to instance created in nonpool)  
 }  
}  
chanrlc
  • 182
  • 1
  • 10
  • OP is particularly interested in why `C` does not get the same string pool value as `A`. – luk2302 Oct 19 '21 at 07:58
  • Because == operator compares references(memory address) not values, java use string equlas to do the string comparison. – chanrlc Oct 19 '21 at 08:02
  • You are still missing the point, two string variables can still be `==` if they refer to the same string pool entry - OP is wondering why C and A do not behave like that. OP does not want to know that `equals` should be used but why `==` does not work in his case. – luk2302 Oct 19 '21 at 08:03
  • ya i m not asking about equals method i am interested in string pool. luk20302 sir can you please tell me how internally this statement String C = "hel" + B is working so that it is not getting same memory refrence – vishal joshi Oct 19 '21 at 08:09
  • yes, i think it is the OP problem. – chanrlc Oct 19 '21 at 08:10