1
public class MainDemo 
{

    public void comp() 
    {
        String s1 = "abc";
        String s2 = "abc";
        System.out.print(""+s1==s2); // Why return false??? Plz clear my doubt? 
        System.out.println(s1==s2);//And why true for this
    }

    /**
      * @param args
     */
    public static void main(String[] args) 
    {
        // TODO Auto-generated method stub
        MainDemo obj=new MainDemo();
        obj.comp();
    }
}


#####################################################

Why this return false ??

System.out.print(""+s1==s2); // Why return false??? 

Please clear my doubt?


Edited

Can someone Tell me how do I check the instance Value


EDITED 2

System.out.println(s1.hashCode()); // BOTH ARE SAME

System.out.println(s2.hashCode());// BOTH ARE SAME

Then What happened to this?????

Sophia
  • 1,046
  • 1
  • 12
  • 15

7 Answers7

1

""+s1 is a new String and so not the same Object as s2. You should you equals to compare values of Strings in Java. For more information and examples have a look at: How do I compare strings in Java?

Community
  • 1
  • 1
bish
  • 3,381
  • 9
  • 48
  • 69
1

If I closed the s1==s2 in bracket like this (s1==s2) Its return true.....Confuseed

Well, these parentheses are used to specify operator precendence. Same as in mathematics.

System.out.println("" + (s1 == s2));

That will just do

System.out.println("" + true);

What you had before was equivalent to

System.out.println( ( "" + s1) == s2);
Thilo
  • 257,207
  • 101
  • 511
  • 656
1

Comparing String like this is not a good idea, use a1.equals(a2); Well back to answer of your question.

String a1="abc";
String a2="abc";
System.out.println(a1==a2); // true
System.out.println(""+a1==a2); // false
System.out.println(""+(a1==a2)); // true

Look at this ""+a1. If you try ""+a1==a1 it return false, confused? don't be because ""+a1 is just a new String object on heap that points to "abc" in the string pool. while ""+(a1==a2) compares first and than append like this :""+(true)

As I suggested use a1.equals(a2); instead of == with strings

official: here

equals(Object anObject) Compares this string to the specified object.

Malik
  • 23
  • 7
Sarz
  • 1,970
  • 4
  • 23
  • 43
  • This might be the answer. Is there any precedency rules applied???? As its check the bracket first and then ""+ ??? – Sophia May 15 '15 at 05:49
  • Thanks, Yup precedence of operators applied, as compiler resolve the expression (according to precedence and/or right to left), first than it throws into buffer (system.out) or any statement or anything else. – Sarz May 15 '15 at 07:11
0

The == operator checks whether the references to the String objects are equal.

And in String.equals(""); checks contains of both Strings.

Rohit
  • 2,646
  • 6
  • 27
  • 52
0

Please Find my comments in your code.. Also read unicode etc from Core Java by Cay Horstman

public class MainDemo {

    public void comp() {
        String s1 = "abc";//String  s1 "abc"
        String s2 = "abc";//String  s2 "abc"
        System.out.print(""+s1==s2); // Why return false??? Plz clear my    doubt? // its "null+abc"=="abc" // mind the null in first "abc" and tell they equal???
        System.out.println(s1==s2);//And why true for this // because here its "abc"=="abc" no null added to the string
   }

   /**
   * @param args
   */
   public static void main(String[] args) {
    // TODO Auto-generated method stub
    MainDemo obj=new MainDemo();
    obj.comp();

   }

}
Uzair
  • 1,529
  • 14
  • 17
0
String s1 = "abc";
String s2 = "abc";
System.out.print(""+s1==s2);   // comparing a reference to object on heap with the reference to interned string in string pool
System.out.println(s1==s2);    // comparing references to same interned string in the string pool

String s1 and String s2 are being assigned string literals i-e there value is known at compile time. JVM would intern them in the string pool and both s1 and s2 would actually be pointing to same string in the string pool.

when you do (s1==s2); s1 and s2 both reference to the same string in the string pool so they return true. but doing (""+s1==s2); returns false because ""+s1 would be evaluated on runtime. JVM would make a string object on the heap that in turn would point to "abc" in the string pool.

enforce string interning using

System.out.println((""+s1).intern() == s2);    // true because of explicit interning

Generally, its better to use .equals() to compare strings. If you want to directly compare the strings you must know how JVM is handling strings behind the scenes.

read What is Java String interning? for further clarification.

Malik
  • 23
  • 7
-1

Because String s1's length got changed now. Check length of those strings using string length function and see what it returns.

Sagar
  • 642
  • 3
  • 14