String st="GUEST";
if(st!="GUEST")
System.out.println("1");
else
System.out.println("2");会出现结果1,为什么???怎么得到我想要的结果??
if(st!="GUEST")
System.out.println("1");
else
System.out.println("2");会出现结果1,为什么???怎么得到我想要的结果??
if(st!="GUEST")两者所指向的 hashcode不同 所以判定为true 输出 1
equals只比较两者之间的值、类型另外提个小小建议,以后记得加上{}号 以免引起不必要的错误~ 当然你这段代码加不加无所谓!
String st="GUEST";
if(st!="GUEST"){
System.out.println("1");
}else{
System.out.println("2");
}
对于"".equals这种情况比较多。
if(!st.equalsignorecase("GUEST"))//equals也可以只是equalsignorecase是区分大小写的
System.out.println("1");
else
System.out.println("2");
这样就一定是2了