Communiquez avec les autres et partagez vos connaissances professionnelles

Inscrivez-vous ou connectez-vous pour rejoindre votre communauté professionnelle.

Suivre

Tell me the output is true or false? By using dry run.

String s1 = "abc"; String s2 = "abc"; System.out.println("s1 == s2 is:" + s1 == s2);

user-image
Question ajoutée par Mohd shahnawaz khan , Associate Project , Cognizant Technology Solution
Date de publication: 2013/09/25
Asif Kamran Malick
par Asif Kamran Malick , Senior Systems Engineer , Infosys Ltd

Question: String s1 = "abc"; 

String s2 = "abc"; 

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

 

Answer: false

 

Explanation: This simply requires knowledge of operator precedence.

+ has a higher predence than ==

So we deal with the + operator first.

The solution is arrived at by the following way:

 

Step1).First comes the + operator

"s1 == s2 is:" + s1

The concatenation of the string  "s1 == s2 is:" and the value of s1 yields :

s1 == s2 is:abc, which is again a string.

 

Step2).Next comes the == operator

The == operator has now two operands.

One operand is the string that we got in Step1, which is s1 == s2 is:abc and the other operand is the value of s2 which is abc.

 

So, the == operator now compares the value of the two operands.

i.e. compares the strings  s1 == s2 is:abc and abc

 

These are two diffrent string values.

So the boolean value returned is false.

Naveen Kumar Sharma Naveen
par Naveen Kumar Sharma Naveen , IT - Trainer , A Plus Education Services (PurpleLeap)

 

Hi Mr. Khan,

 

The output of your mentioned code is:

 

s1 == s2 is:true

 

Now the question is how and why?

 

In Java we take care of String values in a very different way because in Java String values are the part of an Object of type “String” and this object resides in a memory which is called String pool (part of heap memory; dedicated for String objects).

 

In your code, you mentioned…

 

1.       String s1 = “abc”;

2.       String s2 = “abc”;

 

3.       s1 == s2

 

Here “==” (equal operator) is checking the reference of these two objects, whether they are pointing to the same object or not. As we all know that in Java reference variables are doing great job in the absences of pointers.

 

In first line of code one is creating an object of class “java.lang.String” where the reference variable is “s1” and “abc” is the value of that object.

 

In2nd line of code, one is again trying to follow the same procedure however this time Java will check the “String pool memory” for an object who has the same value; “abc” and then java will not create any object but yes it will allow the s2 ref variable to point to the same object which is being pointed by the ref variable s1.

 

Let’s come to last line of the code, here == operator is now generating “true” as an output because both ref variables are pointing to the same object as I explained above.

 

You can find the same output by using “public boolean equals(String obj)” method of java.lang.String class. But it will compare the contents or values of two objects. Actually this method is the overridden version of equals() method of class java.lang.Object class.

 

Regards:

Naveen Kr. Sharma

Daanish Rumani
par Daanish Rumani , Product Manager , Publicis Sapient

Output

s1 == s2 is:false

 

Reason

s1 and s2 are two different strings with the same value. Thus s1 == s2 returns false and only s1.equals(s2) would return true.

Ahmed Hamdy
par Ahmed Hamdy , محاضر فى معهد ضباط الصف المعلمين بالقوات المسلحة المصرية , معهد ضباط الصف المعلمين

خطأ طبعا هو احنا بنقارن s1مع s2 ليه هو المفرض تكون .equal

Ishagh Bah
par Ishagh Bah , Developer and system expert , Orbit

It is false since you are comparing

s1 == s2 is:abc with abc

try

String s1 = "abc"; String s2 = "abc";

System.out.println("s1 == s2 is:");

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

or

String s1 = "abc"; String s2 = "abc"; System.out.println("s1 == s2 is:" + s1.equals(s2));

Walid Mohamed Shekedef
par Walid Mohamed Shekedef , Java Developer

it wll print false at the consol, because this boolean expression does'nt compare the tow strings , but it check if the both refere to the same location at the memory or not, but the comparison will by using .equals or equalsignorecase method

Both Strings pointing from string constant pool in the heap memory.Due to that same address is pointed to both strings.Hence the boolean function true

 

Shahbaz Awan
par Shahbaz Awan , M2M IoT Software Architect , Wadi Makkah

false

Eslam Mohamed
par Eslam Mohamed , Technical Consultant , Meridium

In Java Object orinted programming we have special case to deal with String class 

- Object declaration may be done with one of the following

String name="abc" ;   Vs String name=new String("abc");

the first way check first in literal pool place in memory if there is an old reference to this value(abc) case sensitive if exist it will reference to the same place if not it will reference to new place.

the second way with new keyword will register and reference new place in memory ignored literal pool values.

 

when using == with String we check for reference equality 

when use .equals we check for values equality

 

So your result will be s1 == s2 is: true ==> according to s1 and s2 refernce on the same place in the memory