java中==不是只能判断数值类型吗?为什么可以判断空字符串,输出为true?
黄舟
黄舟 2017-04-17 17:03:58
0
6
699
黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

reply all(6)
洪涛

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
true
false
$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.

Peter_Zhu

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template