System.out.println(s1 == "hello"),“hello”是啥?

System.out.println(s1 ==

问题描述:

         String s1 ="hello";
        System.out.println(s1 == "hello");//true

今天看字符串常量池知识,看的差不多了,突然来个这,整蒙了。

大佬们,println里面的这个“hello”是啥? 引用吗?

出现这种问题,需要补充哪方面知识?诸位赐教

 

 

System.out.println(s1 == "hello");这里面的"hello"是一个字符串常量,不是引用

s1 == "hello"判断s1是否与这个常量相等。

System.out.println(s1 == "hello");

改为

System.out.println(s1.equals("hello")); 

//字符串比较用equals方法

System.out.println(s1 == "hello");//true
题主 "hello" 就是一个字符串常量

题主 字符串比较不能直接用== 因为这是比较地址,应该用equals()方法比较,这样才是比较字符串里面内容是否一样

常量池里的“hello”字符串本身

还是println里面的“hellp”这个整体,是一个指向在字符串常量池的hello对象的一个引用?

就是new String("hello")

这是变量的另一种形式  字面量

您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~

如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~

ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632