public static void main(String[] args) {
// TODO Auto-generated method stub
// Primitive Data Types:
int no1 = 10;
int no2 = 10;
System.out.println(no1 == no2); // == operator comparing the value.
//Output: "true"
// Non Primitive Data Types---> "String"
// with new keyword using string
String s1 = new String("hello");
String s2 = new String("hello");
System.out.println(s1 == s2); // == Operator comparing the object.
//Output: "False"
System.out.println(s1.equals(s2)); // equals() method is used to compare the value.
//Output: "False"
// Without new keyword using string
String a1 = "welcome";
String a2 = "welcome";
System.out.println(a1 == a2); // == operator comparing the value because its having same value for both variables on same location.
System.out.println(a1.equals(a2)); // equals() method is used to compare the value.
//StringBuffer
StringBuffer sb1 = new StringBuffer("java");
StringBuffer sb2 = new StringBuffer("java");
System.out.println(sb1 == sb2);
// StringBuffer objects are created on the heap, and even if they have the same content,new StringBuffer() creates distinct objects in memory
//Output: "False"
System.out.println(sb1.equals(sb2));
//The equals() method in StringBuffer (and StringBuilder) is inherited from Object and by default performs a reference comparison
//Output: "False"
//StringBuilder
StringBuilder sg1 = new StringBuilder("sql");
StringBuilder sg2 = new StringBuilder("sql");
System.out.println(sg1 == sg2);
//StringBuilder objects are created on the heap, and even if they have the same content,new StringBuilder() creates distinct objects in memor
//Output: "False"
System.out.println(sg1.equals(sg2));
//The equals() method in StringBuilder is inherited from Object and by default performs a reference comparison
//Output: "False"
}
}
Top comments (0)