public class stringComp { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub String str1=new String("woko"); String str2="woko"; String str3="woko"; String str4=new String("woko"); if(str2==str3){ System.out.println("str2=str3"); } if(str2.equals(str3)){ System.out.println("str2 is equals with str3"); } if(str1==str4){ System.out.println("str1=str4"); } if(str1.equals(str4)){ System.out.println("str1 is equals with str4"); } if(str1==str2){ System.out.println("str1= str2"); } if(str1.equals(str2)){ System.out.println("str1 is equals with str2"); } } } 输出 : str2=str3 str2 is equals with str3 str1 is equals with str4 str1 is equals with str2