search

Home  >  Q&A  >  body text

java中==不是只能判断数值类型吗?为什么可以判断空字符串,输出为true?

黄舟黄舟2781 days ago661

reply all(6)I'll reply

  • PHP中文网

    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
    true
    false
    $java StringEqualsTest

    true🎜false🎜

    reply
    0
  • 黄舟

    黄舟2017-04-17 17:05:58

    String is not a basic data type, so using == is the memory address for comparison.

    reply
    0
  • ringa_lee

    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.

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-17 17:05:58

    The original poster can first understand the reference comparison and value comparison

    reply
    0
  • 阿神

    阿神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.

    reply
    0
  • 怪我咯

    怪我咯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

    reply
    0
  • Cancelreply