Advertisement
// Java - Checking equality of references of String objects creating with new keyword.
class StringEqualsOrNot
{
public static void main(String[] ar)
{
String str1 = new String("Hello");
String str2 = new String("Hello");
System.out.println("First String is : " + str1);
System.out.println("Second String is : " + str2);
//Comparing first String with itself
boolean b=(str1==str1);
System.out.println("First String == First String is " + b);
//Comparing second String with itself
b=(str2==str2);
System.out.println("Second String == Second String is " + b);
//Comparing first String with second String
b=(str1==str2);
System.out.println("First String == Second String is " + b);
}
}
First String is : Hello
Second String is : Hello
First String == First String is true
Second String == Second String is true
First String == Second String is false
// Java - Checking equality of references of String objects creating without new keyword.
class StringEqualsOrNot
{
public static void main(String[] ar)
{
String str1 = "Bonjour";
String str2 = "Bonjour";
System.out.println("First String is : " + str1);
System.out.println("Second String is : " + str2);
//Comparing first String with itself
boolean b=(str1==str1);
System.out.println("First String == First String is " + b);
//Comparing second String with itself
b=(str2==str2);
System.out.println("Second String == Second String is " + b);
//Comparing first String with second String
b=(str1==str2);
System.out.println("First String == Second String is " + b);
}
}
First String is : Bonjour
Second String is : Bonjour
First String == First String is true
Second String == Second String is true
First String == Second String is true
Advertisement
// Java - Difference between equals() method and == operator for String objects.
class StringEqualsOrNot
{
public static void main(String[] ar)
{
String str1= new String("Hello");
String str2= new String("Hello");
System.out.println("First String is " + str1);
System.out.println("Second String is " + str2);
//Comparing reference of first String with second String
boolean b=(str1==str2);
System.out.println("First String == Second String is " + b);
//Comparing value of first String with second String
System.out.println("Are values in two String object equal ? " + str1.equals(str2));
}
}
First String is Hello
Second String is Hello
First String == Second String is false
Are values in two String object equal ? true
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement