public class name
{
public static void main(String args[])
{
String s="true";
Boolean b=new Boolean("true");
if(s.equals(b))
System.out.println("yes");
else
System.out.println("no");
}
}
请问:这段代码的输出结果是什么,为什么,希望您在给出结果的时候附上您的理解,您是如何想的,您是如何理解的,谢谢!!
{
public static void main(String args[])
{
String s="true";
Boolean b=new Boolean("true");
if(s.equals(b))
System.out.println("yes");
else
System.out.println("no");
}
}
请问:这段代码的输出结果是什么,为什么,希望您在给出结果的时候附上您的理解,您是如何想的,您是如何理解的,谢谢!!
s是String类型的,调用String类中的equals()public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof String) {
String anotherString = (String)anObject;
int n = count;
if (n == anotherString.count) {
char v1[] = value;
char v2[] = anotherString.value;
int i = offset;
int j = anotherString.offset;
while (n-- != 0) {
if (v1[i++] != v2[j++])
return false;
}
return true;
}
}
return false;
}先执行到if (this == anObject), 跳过if,因为s和b地址不同
再执行到if (anObject instanceof String),跳过if,因为b不是String类型的对象
最后return false,返回false,打印no
{
public static void main(String args[])
{
String s="true";
Boolean b=new Boolean("true");
String str=new String("true");
if(s.equals(b))
System.out.println("yes");
else
System.out.println("no");
if(s.equals(str))
System.out.println("yes");
else
System.out.println("no");
if(s==str)
System.out.println("yes");
else
System.out.println("no");
}
}
输出结果:no
yes
no