+ 10
Why output is false?
public class Program { public static void main(String[] args) { String s1="hello"; String s2=new String ("hello"); if(s1==s2) System.out.println ("true"); else System.out.println("false"); } }
6 Answers
+ 8
Thank you Serena and othersđ
+ 4
cause they are different objects
i updated your code please check the link to see
https://code.sololearn.com/c3gQ5wPQ9wja/?ref=app
+ 2
On that code, you are actually comparing two different variables that is why you get that 'false'. Instead use the equals method. like the syntax below
if(s1.equals(s2))
{
System.out.println("true");
}
else
{
System.out.println("false");
}
+ 2
Nice and clear Serena Yvonne thanks!
+ 1
great explanation @Serena Yvonne đ