Junior — Senior
Comparison of string literal references and objects in Java
livecode
Task condition
This example demonstrates how string variable comparisons using the == operator work when strings are obtained as literals and when they are created through the new String(...) constructor. It shows under what circumstances two string variables point to the same memory area and when they are in different memory locations.
public class Test1 {
public static void main(String[] args) {
var x = "a";
var y = "a";
var z = new String("a");
System.out.println("a == b: " + (x == y));
System.out.println("a == c: " + (x == z));
}
}