Know one of String: Compare String

xiaoxiao2021-03-06  79

Java.Lang.object has an equals class for comparing whether the two classes are equal, so there are more simple String, such as: string str1 = "hello quanjizhu"; string str2 = "Hello quanjizhu"; system.out. Println (str1.equals (str2)); At this time, some will be pointed out that in fact, STR1 and STR2 can also be used directly with == to verify the class: system.out.println (str1 == str2); this is because STR1, STR2 is the same instance, and when Java is compiled, the constant "Hello Quanjizhu" will be placed in String pool (I think it should save storage or improve performance), so the result is Str1, STR2 It is point to the same example. The same string str3 = "hello" "quanjizhu"; it will be optimized into string str3 = "hello quanjizhu" during compilation; also points to the same instance. Look at an example string var = "quanjizhu"; string str4 = "Hello" var; system.out.println (str1 == str4) What is the result of STR1 == STR4? The output result is False, which proves that the two are not to point to the same example. For a definition method: str4 = ("Hello" var4) .intern (); inf () method tells the compiler to put this result in String pool Therefore, the output structure will be true; however, when you use a constructor to initialize the String class, it is another case. String str5 = new string ("Hello Quanjizhu"); system.out.println (ST1 == STR5 The result will be FLASE.

转载请注明原文地址:https://www.9cbs.com/read-107387.html

New Post(0)