Hi friends,
I have a doubt (May sound simple) regarding the behaviour of the 'String' class.I have 3 programs.
Let me list one by one.
Program :1 public class Test
{
public static void main(String s[ ])
{
String s1 =new String("Hi");
String s2=new String("Hi");
if(s1==s2)
System.out.println("Both are same");
else
System.out.println("Both are different");
}
}
Program : 2 public class Test
{
public static void main(String s[ ])
{
String s1 =new String();
String s2=new String();
s1 = "Hi";
s2 = "Hi";
if(s1==s2)
System.out.println("Both are same");
else
System.out.println("Both are different");
}
}
Program : 3 public class Test
{
public static void main(String s[ ])
{
String s1 = "Hi";
String s2 = "Hi";
if(s1==s2)
System.out.println("Both are same");
else
System.out.println("Both are different");
}
}
The first one prints "Both are different" , but the next 2 two prints "Both are same", I want the exact explanation of this , why is this happening?
Expecting ur replies.......
Regards,
sheeba