Java String ==, equals() – Understanding – 15 Programs

These are the easiest 15 programs list for better understanding on Java String – ==, equals() method.  Please practice each one.  Observe the output.  Get your learnings!

Scenario #1:
String s1 = new String(“ABC”);
String s2 = “ABC”;
if(s1==s2)
{
System.out.println(“s1 s2 are ==”);
}
else
{
System.out.println(“s1 s2 are not ==”);
}
//With equals() method
String s1 = new String(“ABC”);
String s2 = “ABC”;
if(s1.equals(s2))
{
System.out.println(“s1 s2 are equals”);
}
else
{
System.out.println(“s1 s2 are not equal”);
}

Scenario #2:
String s1 = new String(“ABC”);
String s2 = s1;
if(s1==s2)
{
System.out.println(“s1 s2 are ==”);
}
else
{
System.out.println(“s1 s2 are not ==”);
}

//With equals() method
String s1 = new String(“ABC”);
String s2 = s1;
if(s1.equals(s2))
{
System.out.println(“s1 s2 are equal”);
}
else
{
System.out.println(“s1 s2 are not equal”);
}
Scenario #3:
String s1 = new String(“ABC”);
String s2 = s1;
s2=”ABC”;
if(s1==s2)
{
System.out.println(“s1 s2 are ==”);
}
else
{
System.out.println(“s1 s2 are not ==”);
}

//With equals() method
String s1 = new String(“ABC”);
String s2 = s1;
s2=”ABC”;
if(s1.equals(s2))
{
System.out.println(“s1 s2 are equal”);
}
else
{
System.out.println(“s1 s2 are not equal”);
}

Scenario #4:
String s1 = new String(“ABC”);
String s2 = s1;
s1=”PQR”;
if(s1==s2)
{
System.out.println(“s1 s2 are ==”);
}
else
{
System.out.println(“s1 s2 are not ==”);
}
//With equals() method
String s1 = new String(“ABC”);
String s2 = s1;
s1=”PQR”;
if(s1.equals(s2))
{
System.out.println(“s1 s2  equals”);
}
else
{
System.out.println(“s1 s2  not equals”);
}

Scenario #5:
String s1 = new String(“ABC”);
String s2 = s1;
s1=”PQR”;
s2=”PQR”;
/*Here, s1 will be created in String Constant Pool.  It will hold the value “PQR”.  When we create s2 with same value, no new
* memory reference will be created in String Constant Pool.  Instead, the same s1 will be referred, as it is already present.
*/

if(s1==s2)
{
System.out.println(“s1 s2 are ==”);
}
else
{
System.out.println(“s1 s2 are not ==”);
}

//With equals() method
String s1 = new String(“ABC”);
String s2 = s1;
s1=”PQR”;
s2=”PQR”;
/*Here, s1 will be created in String Constant Pool.  It will hold
* the value “PQR”.  When we create s2 with same value, no new
* memory reference will be created in String Constant Pool.
* Instead, the same s1 will be referred, as it is already
* present.
*/

if(s1.equals(s2))
{
System.out.println(“s1 s2  equals”);
}
else
{
System.out.println(“s1 s2  not equals”);
}

Scenario #6:
String s1 = new String(“ABC”);
String s2 = s1;
s1= new String(“PQR”);
s2= new String(“PQR”);
/*Here, s1 will be created in Heap Memory.  It will hold
* the value “PQR”.  When we create s2 with same value, new
* memory reference will be created again in Heap Memory.
* Hence, when we compare s1 and s2 with == operator, the memory
* references will be compared.
*/
if(s1==s2)
{
System.out.println(“s1 s2 are ==”);
}
else
{
System.out.println(“s1 s2 are not ==”);
}

//With equals() method
String s1 = new String(“ABC”);
String s2 = s1;
s1= new String(“PQR”);
s2= new String(“PQR”);

if(s1.equals(s2))
{
System.out.println(“s1 s2  equals”);
}
else
{
System.out.println(“s1 s2  not equals”);
}

Scenario #7:
String s1 = new String(“ABC”);
String s2 = s1;
s1= new String(“PQR”);
s2= new String(“PQR”);
String s3 = new String(s1);
if(s1==s3)
{
System.out.println(“s1 s3 are ==”);
}
else
{
System.out.println(“s1 s3 are not ==”);
}

//With equals() method
String s1 = new String(“ABC”);
String s2 = s1;
s1= new String(“PQR”);
s2= new String(“PQR”);
String s3 = new String(s1);
if(s1.equals(s3))
{
System.out.println(“s1 s3  equals”);
}
else
{
System.out.println(“s1 s3  not equals”);
}

Scenario #8:
String s1 = new String(“ABC”);
String s2 = “ABC”;
if(s1.equals(s2))
{
System.out.println(“s1 s2 are ==”);
}
else
{
System.out.println(“s1 s2 are not ==”);
}

//With equals() method
String s1 = new String(“ABC”);
String s2 = s1;
s1= new String(“PQR”);
s2= new String(“PQR”);
String s3 = new String(s1);
if(s1.equals(s3))
{
System.out.println(“s1 s3  equals”);
}
else
{
System.out.println(“s1 s3  not equals”);
}

Scenario #9:
String s1 = new String(“ABC”);
String s2 = “ABC”;
s1=null;
if(s1==s2))
{
System.out.println(“s1 s2 are ==”);
}
else
{
System.out.println(“s1 s2 are not ==”);
}

//With equals() method
String s1 = new String(“ABC”);
String s2 = “ABC”;
s1=null;
if(s1.equals(s2))
{
System.out.println(“s1 s3  equals”);
}
else
{
System.out.println(“s1 s3  not equals”);
}

Scenario #10:
String s1 = new String(“ABC”);
String s2 = “ABCD”;
s1+=”D”;
// Here, s1 is referred from Heap Memory.    When we append, s1
//, a new memory reference will be created in Heap Memory for //”D”,  along with existing memory for s1
if(s1==s2)
{
System.out.println(“s1 s2 are ==”);
}
else
{
System.out.println(“s1 s2 are not ==”);
}

//With equals() method
String s1 = new String(“ABC”);
String s2 = “ABCD”;
s1+=”D”;
// Here, s1 is referred from Heap Memory.    When we append, s1
//, a new memory reference will be created in Heap Memory for //”D”,  along with existing memory for s1
if(s1.equals(s2))
{
System.out.println(“s1 s2  equals”);
}
else
{
System.out.println(“s1 s2  not equals”);
}
Scenario #11:
String s1 = “ABC”;
String s2 = “ABCD”;
s1+=”D”;
if(s1==s2)
{
System.out.println(“s1 s2 are ==”);
}
else
{
System.out.println(“s1 s2 are not ==”);
}

//With equals() method
String s1 = “ABC”;
String s2 = “ABCD”;
s1+=”D”;
if(s1.equals(s2))
{
System.out.println(“s1 s2  equals”);
}
else
{
System.out.println(“s1 s2  not equals”);
}

Scenario #12:
String s1 = “ABC”;
String s2 = “ABCD”;
s1+=”D”;
// Whenever we append String literals through references(s1), a new String literal
// will be created along with existing one.  Here, s1 memory //will be maintained and along with this, separate memory will //be created for “D”.
if(s1==s2)
{
System.out.println(“s1 s2 are ==”);
}
else
{
System.out.println(“s1 s2 are not ==”);
}

//With equals() method
String s1 = “ABC”;
String s2 = “ABCD”;
s1+=”D”;
if(s1.equals(s2))
{
System.out.println(“s1 s2  equals”);
}
else
{
System.out.println(“s1 s2  not equals”);
}
Scenario #13:
String s1 = “ABC”+”ABC”;
String s2 = “ABCABC”;
// Here, s1 will be allotted memory, holding values “ABCABC”.

if(s1==(s2))
{
System.out.println(“s1 s2 are ==”);
}
else
{
System.out.println(“s1 s2 are not ==”);
}

String s1 = “ABC”+”ABC”;
String s2 = “ABCABC”;
// Here, s1 will be allotted memory, holding values “ABCABC”.
if(s1.equals(s2))
{
System.out.println(“s1 s2  equals”);
}
else
{
System.out.println(“s1 s2  not equals”);
}

Scenario #14:
String s1 = “ABCABC”;
String s2 = s1+s1;
//String s3=”ABCABCABCABC”;

if(s1==s2)
{
System.out.println(“s1 s2 are ==”);
}
else
{
System.out.println(“s1 s2 are not ==”);
}

//With equals() method
String s1 = “ABCABC”;
String s2 = s1+s1;
if(s1.equals(s2))
{
System.out.println(“s1 s2  equals”);
}
else
{
System.out.println(“s1 s2  not equals”);
}

Scenario #15:
String s1 = “ABCABC”;
String s2 = s1+s1;
String s3=”ABCABCABCABC”;
System.out.println(“s2 “+ s2);
System.out.println(“s3 “+ s3);
if(s2==s3)
{
System.out.println(“s1 s2 are ==”);
}
else
{
System.out.println(“s1 s2 are not ==”);
}