PHP中文网2017-04-17 17:05:58
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);
}
}
$java StringEqualsTest
$java StringEqualsTest
true
false
黄舟2017-04-17 17:05:58
String is not a basic data type, so using == is the memory address for comparison.
ringa_lee2017-04-17 17:05:58
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.
天蓬老师2017-04-17 17:05:58
The original poster can first understand the reference comparison and value comparison
阿神2017-04-17 17:05:58
Isn’t == in Java only able to determine numeric types?
Answer: No, ==
can determine basic data types (numeric types) and objects.
怪我咯2017-04-17 17:05:58
==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