For == operation on variables of String for two different objects s and p created using new gives result as false (line 1) which I understand but why does line 3 and 4 ( line number commented ) gives true as Output?
I am aware of the fact that == is meant for reference comparison and that's where i have the doubt if it's meant for reference comparison then why line 4 gives true as j is an integer and there is no immutability concept as for String ( String s ) and everytime a new object has to be created ?
class World
{
public static void main(String[] args)
{
String s=new String("B");
String p=new String("B");
System.out.println(s==p); //false line 1
Test t1= new Test("A",4);
Test t2= new Test("A",4);
System.out.println(t1==t2); //false line 2
System.out.println(t1.s==t2.s); //true line 3
System.out.println(t1.j==t2.j); //true line 4
}
}
class Test
{
String s;
int j;
Test(String s, int j)
{
this.s=s;
this.j=j;
}
}