public class Demo {
public static void main(String args[]) {
String str=new String("hello");
if(str=="hello") {
System.out.println("true");
}else{
System.out.println("false");
}
}
}
A true
B false
考点:考察求职者对==与的用法
出现频率:★★★★★
【面试题分析】
==判断的是对象引用地址是否相同
String str=newString("hello");
这句话new了一个新的String对象,所以地址与"hello"字符串常量的地址不同,
注意这两句话,
1.新建的对象保存在堆内存中,引用变量str保存在栈内存中并指向堆内存中的对象。
2.而“hello”这种直接保存在字符串常量池中。
3。两个不是同一个地址啊!返回false
所以参考答案是( B)