Strings with the same content created using double quotes all point to the same reference. What comes out of new String is a new object. This is why you should try to avoid new String <pre>
public class StringEqualsTest{
public static void main(String[] args) {
String s1="Gavin";
String s2=new String("Gavin");
System.out.println("Gavin"==s1);
System.out.println("Gavin"==s2);
}
}
There are many introductions to Java's == and equals() on the Internet. Just browse a few articles and you will be able to understand this problem. This problem is very simple on the surface, but it will become more in-depth as you go on.
==Compares literal values Strings are reference types, and established strings are immutable in memory. s refers to the memory address of the "" string, and the same address will naturally compare the same
Strings with the same content created using double quotes all point to the same reference. What comes out of new String is a new object. This is why you should try to avoid new String
<pre>
true🎜false🎜
$java StringEqualsTest$java StringEqualsTest
true
false
String is not a basic data type, so using == is the memory address for comparison.
There are many introductions to Java's == and equals() on the Internet. Just browse a few articles and you will be able to understand this problem. This problem is very simple on the surface, but it will become more in-depth as you go on.
The original poster can first understand the reference comparison and value comparison
Isn’t == in Java only able to determine numeric types?
Answer: No,
==
can determine basic data types (numeric types) and objects.==Compares literal values
Strings are reference types, and established strings are immutable in memory. s refers to the memory address of the "" string, and the same address will naturally compare the same