33. How about equal operator?

With equal operator, you compare two object through it’s reference variables.

Example:

public class ComparisonExample {
public static void main(String[] args) {
// Example with primitives
int a = 5;
int b = 5;
System.out.println(“Primitive comparison (a == b): ” + (a == b)); // true

// Example with objects
String str1 = new String(“hello”);
String str2 = new String(“hello”);

// Comparison using ‘==’
System.out.println(“String comparison (str1 == str2): ” + (str1 == str2)); // false

// Comparison using ‘equals()’
System.out.println(“String comparison (str1.equals(str2)): ” + str1.equals(str2)); // true
}
}

 

Leave a Reply

Your email address will not be published. Required fields are marked *