Chapter 4 : Control Structures 1 : Selection


Although it need not be, the expression is usually an identifier. Whether it is an identifier
or an expression, the value of the identifier or the expression can only be of type int, byte, short,
or char

 

 

public class StringComapre {
    
    public static void main(String[] args) {    
    
    String    str1 = new String("Hello");
    String    str2 = "Hello";
    
    if (str1 == str2)
        System.out.println("str1 equal str2");
    else
        System.out.println("str1 != str2");
    
    ////////////////////////////////////////////////////////
    
    String    str3 = "Hello";
    String    str4 = new String("Hello");
    
    if (str3 == str4)
        System.out.println("str3 equal str4");
    else
        System.out.println("str3 != str4");
    
    ///////////////////////////////////////////////////////
    
    String    str5 = new String("Hello");
    String    str6 = new String("Hello");
    
    if (str5 == str6)
        System.out.println("str5 equal str6");
    else
        System.out.println("str5 != str6");
    
    }
}

  这段代码有疑问,为什么str1 和 str2不相等?

posted @ 2014-11-17 12:48  hare101  阅读(157)  评论(0编辑  收藏  举报