KillerProgrammer
Programmer
In the following code , why case 1 and case 3 returns "Not Equals" and case 2 returns "Equals" . The reference variable in case 1 is not the same from the string "String" in the JVM String Pool ? If not , case 2 should return "Not equals" , don't ? Same for case 3 that , in my way , should be identic to the case 1
public class Test
{
public static void main(String[] args)
{
//case 1
if(" String ".trim() == "String"
System.out.println("Equals"
else
System.out.println("Not Equals"
//case 2
if("String".substring(0) == "String"
System.out.println("Equals"
else
System.out.println("Not Equals"
//case 3
if(" String ".substring(1,7) == "String"
System.out.println("Equals"
else
System.out.println("Not Equals"
}
}
TIA
Thor
public class Test
{
public static void main(String[] args)
{
//case 1
if(" String ".trim() == "String"
System.out.println("Equals"
else
System.out.println("Not Equals"
//case 2
if("String".substring(0) == "String"
System.out.println("Equals"
else
System.out.println("Not Equals"
//case 3
if(" String ".substring(1,7) == "String"
System.out.println("Equals"
else
System.out.println("Not Equals"
}
}
TIA
Thor