Comparison of java String

Look at the piece of code:

 1 public class StringCom {
 2 
 3     public static void main(String[] args) {
 4         String a = "hello";
 5         String b = "hello";
 6         String c = new String("hello");
 7         char d[] = {'h','e','l','l','o'};
 8         
 9         System.out.println(a==b);  //true
10 
11         System.out.println(a==c);  //false
12  
13         System.out.println(a==d);  // Incompatible operand types String and char[]
14 
15         System.out.println(a.equals(d)); //false , 提示:Unlikely argument type for equals(): char[] seems to be unrelated to String
16         
17         System.out.println(a.equals(new String("hello")));  //true
18 
19     }
20 
21 }

Encounter a problem, just five above comparison, ask which will output false, as for the first row 13, directly in the Eclipse error "Incompatible operand types String and char [  ]", it means that it will not export false? Not run on the error.

 

There is a discussion see: https://www.nowcoder.com/questionTerminal/e426ba1e900c4a7ea000e9a029653aae?from=relative 

There are about '==' discussions and 'equals', some reply I see ignorant force, and recommended a blog: https://www.cnblogs.com/zhxhdean/archive/2011/03/25/1995431. html

There are known to almost one answer: https://www.zhihu.com/question/26872848

 

Guess you like

Origin www.cnblogs.com/Guhongying/p/11145111.html