-3

As the java-doc tells Java Operator == tests for reference equality (whether they are the same object). so "==" operator will return true only if two object reference it is comparing represent exactly same object otherwise "==" will return false.

But while running a piece of code all i found is that this statement doesn't satisfy the Output of the code. Here's the code:

public class Test2 {


    public static void main(String[] args)
    {
         String s="Sachin";  
         String t="Sachin";
         System.out.println(s==t); 
    }
} 

And Surprisingly i found a output "true". Please Help me understand why it is so?

Here's a Screenshot to my program output:

https://i.stack.imgur.com/OZ0PW.jpg

Prometheus
  • 35
  • 10
  • 2
    I don't understand, why you ask for the `==` operator, if your provided code does not use that in all, but uses the `equals` operator. – Korashen Feb 02 '18 at 12:48
  • 2
    `s` and `t` refer to the same object due to the interning of the strings; i.e. the compiler is being clever. Lots of duplicates on this. – Bathsheba Feb 02 '18 at 12:49
  • [REMOVED] - After running your code: The result is `false`, your question is not reproducable. – Korashen Feb 02 '18 at 12:49
  • @Bathsheba what do you mean by interning of the strings? – Prometheus Feb 02 '18 at 12:50
  • Well, **sometimes** is `true` and sometimes is `false`... – zlakad Feb 02 '18 at 12:52
  • Yeah please just go ahead and edit the question. Now it doesn't even print true. – förschter Feb 02 '18 at 12:52
  • @Prometheus! Take a look at `String.intern()` method and what it does. This is exaclty what happend to your updated question. – Korashen Feb 02 '18 at 12:52
  • @MuratK. Here's a Screen Shot of my output https://ibb.co/jFVFm6 – Prometheus Feb 02 '18 at 12:55
  • 1
    @zlakad It should always be true. From [the JLS](https://docs.oracle.com/javase/specs/jls/se9/html/jls-15.html#jls-15.28): *Compile-time constant expressions of type String are always "interned"* – Michael Feb 02 '18 at 12:56
  • @Prometheus Yes, you changed your question like 5 times – Murat Karagöz Feb 02 '18 at 12:57
  • @Michael, that is correct, but *only* if the Strings are *Compile-time constants* like you said. I've tried to encourage using `equals` in every case... ;) – zlakad Feb 02 '18 at 13:03
  • @zlakad And both strings here are compile time constants. Your comment makes it sound like the result of `s == t` in this case is non-deterministic, and that's not true. – Michael Feb 02 '18 at 13:05
  • @Michael I agree, but the question was edited several times - so... It's too late to delete the comment right now. No bad feelings, friend. – zlakad Feb 02 '18 at 13:08
  • @Michael, I know I can delete the comment, but since we had this conversation it wouldn't be nice (that is the context of "I'ts too late to delete...") – zlakad Feb 02 '18 at 14:14

1 Answers1

4

You assuption is that

     String s="Sachin";  
     String t="Sachin";

creates two string objects, but this is not true.

Java optimises the usage of string so that is puts literal strings in the string pool so that it assinges an already created string object from that pool if the compiler finds the same string a second time. This is called string interning.

You better try this:

public class Test2 {
    public static void main(String[] args)
    {
         String s="Sachin";  
         String t=new String(s);
         System.out.println(s==t); 
    }
} 
Timothy Truckle
  • 15,071
  • 2
  • 27
  • 51