Junior — Middle
How is equality between two values checked using the == operator in programming?
sobes.tech AI
Answer from AI
The == operator in programming is used to check the equality of two values. Depending on the language and data type, it can compare:
- For primitive types (numbers, boolean values) — it directly compares the values.
- For reference types (objects) — it compares references (memory addresses), meaning it checks whether both variables point to the same object.
For example, in Java:
int a = 5;
int b = 5;
System.out.println(a == b); // true, values are compared
String s1 = new String("test");
String s2 = new String("test");
System.out.println(s1 == s2); // false, different objects
System.out.println(s1.equals(s2)); // true, string contents are the same
Thus, == checks value equality for primitives and reference equality for objects.