What is the meaning in following java code: String s1="hello"; String s2="hello";boolean c=s1.equals(s2); System.out.println(c); | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

What is the meaning in following java code: String s1="hello"; String s2="hello";boolean c=s1.equals(s2); System.out.println(c);

1st Dec 2016, 8:03 PM
Sagar Maharana
Sagar Maharana - avatar
4 Answers
+ 2
== it compares objects references( reference means memory location), the both objects are in different location in memory so when u compare these objects with ==, it returns false, because these sind different objects, altough these contain same string :), i hope, i can help u.
2nd Dec 2016, 1:53 PM
Ozan Karataş
Ozan Karataş - avatar
+ 2
s1 == s2 is a true or false evaluation. It's like asking "is the reference at s1 equal to the reference at s2?" . The answer would be false, since they occupy different addresses in memory. If you want to see if two strings hold the same values use s1.equals(s2) as your evaluation.
17th Jan 2017, 2:36 AM
Gray
Gray - avatar
+ 1
what does mean by following code: System.out.println(s1==s2):
2nd Dec 2016, 8:06 AM
Sagar Maharana
Sagar Maharana - avatar
0
hi, there are two objects,(because string is object). U wanna compare the two objects, so u need compare,both of the objects are same so, it should be returned treu :)i hope, i can help u
1st Dec 2016, 8:30 PM
Ozan Karataş
Ozan Karataş - avatar