+ 3

What will be the output for following java code snippet :

public static void main(String args [ ]) { String str= new String("Alan"); String str1="Alan"; if(str==str1) { System.out.println("True"); } else System.out.println("False"); }

28th Feb 2018, 5:19 PM
ashish jain
ashish jain - avatar
5 Answers
+ 4
False.. Don't use == to compare strings.. Use equals.() instead if(str.equals(str1))
28th Feb 2018, 5:21 PM
LordHill
LordHill - avatar
+ 7
well you will understand this by this post go through this https://www.sololearn.com/Discuss/647021/?ref=app
28th Feb 2018, 5:23 PM
GAWEN STEASY
GAWEN STEASY - avatar
+ 3
It is not comparing the strings there it is actually comparing the address of the strings where they are stored and hence that is why I have not used .equals in my if statement..
28th Feb 2018, 5:28 PM
ashish jain
ashish jain - avatar
0
so you don't have a question about the code u sent. right?
28th Feb 2018, 6:55 PM
Farshaad Heydari
Farshaad Heydari - avatar
0
1
10th Jan 2021, 10:12 AM
Prince Sey
Prince Sey - avatar