Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/396.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 字符串str1=“hello”之间有什么区别;字符串str2=新字符串(“hello”);在爪哇?_Java_String - Fatal编程技术网

Java 字符串str1=“hello”之间有什么区别;字符串str2=新字符串(“hello”);在爪哇?

Java 字符串str1=“hello”之间有什么区别;字符串str2=新字符串(“hello”);在爪哇?,java,string,Java,String,字符串str1=hello;字符串str2=新字符串hello;在爪哇 我知道str2是一个对象,但是str1呢 我的意思是,例如: 如果两者都是对象,为什么 if(str1.toString() == str2.toString()) 不会产生真正的布尔值?对象在任何情况下都是字符串,顺便说一句 if(str1.toString().equals(str2.toString())) 甚至更好 if(str1.equals(str2)) 是比较字符串的正确方法它们都是对象,请参阅所有相关问

字符串str1=hello;字符串str2=新字符串hello;在爪哇

我知道str2是一个对象,但是str1呢

我的意思是,例如:

如果两者都是对象,为什么

if(str1.toString() == str2.toString())

不会产生真正的布尔值?

对象在任何情况下都是字符串,顺便说一句

if(str1.toString().equals(str2.toString()))
甚至更好

if(str1.equals(str2))

是比较字符串的正确方法

它们都是对象,请参阅所有相关问题和答案这两个都是对象。前者使用文字字符串,后者从文字字符串创建一个新字符串。@ZouZou生成一个新哈希