Avatar ·

Difference between == and === in Java

Hello everyone! I can't figure out the comparison operators in Java. I know that in JavaScript there is strict comparison using ===, but in Java such an operator seems to be absent. When trying to write ===, the compiler throws an error.

I tried using == to compare strings, but I get unexpected results. For example, here's this code:

String a = new String("test");\nString b = new String("test");\nSystem.out.println(a == b); // false\nSystem.out.println(a.equals(b)); // true

Why does == return false if the strings are the same? I expected == to compare the content, like in JavaScript. I read the documentation — in Java, == compares object references.