When we do
String a=new String("mac");
String b=new String("mac");
if(b == a)
{
System.out.println("condition 1 is true");
}
if(b.equals(a))
{
System.out.println("condition 2 is true");
}
condition 1 fails and condition 2 is true since b and a are two different objects
But when we do
String a="mac";
String b="mac";
if(b == a)
{
System.out.println("condition 1 is true");
}
if(b.equals(a))
{
System.out.println("condition 2 is true");
}
Both the conditions are true. Why didn't java create a new object for second case. If java creates a new object only when we use new()
then if we give different values to both the strings then what happens internally in java?