java toString() 函数的疑惑

来源:百度知道 编辑:UC知道 时间:2024/06/14 21:35:33
public class test3 {
public static void main(String args[]){
String a="a";
String b[]={"A"};

String temp1=b[0].toString();
String temp=a.toUpperCase();

System.out.println("temp1:"+temp1);
System.out.println("temp:"+temp);

if(temp1==temp)
System.out.println("yes");
else
System.out.println("no");
}
}

为什么输出的都是相同的A 可是用if判断时却不想等了呢??

public class test3 {
public static void main(String args[]){
String a="a";
String b[]={"A"};

String temp1=b[0].toString();
String temp=a.toUpperCase();

System.out.println("temp1:"+temp1);
System.out.println("temp:"+temp);

if(temp1.equals(temp))
System.out.println("yes");
else
System.out.println("no");
}
}
java里判断字符串相等用equal方法,因为==是判断内存位置是否相等,而equal方法则是判断指向空间的值是否相等~~