Java: Why can String equality be proven with ==? -
I came to know that it was used to test strings similar to Satan, instead of ==
Code> String.equals () , because each string was the reference of its own object.
But if I do something like this
System.out.println ("hello" == "hello");
It is not that it is still a bad thing - you will still be able to test the contextual equality in terms of value equality.
public class test {public static zero main (string [] args) {string x = "hello"; String y = new string (x); System.out.println (x == y); // print false}}
If you are seeing the test of "test" now it is because you really have equal references. The most common reason to see it Probably due to the interval of string literals, but this is always in Java:
public square test {public static zero main (string [] args) {string x = "hello"; String y = "hello" + "lo"; // Consolidated on compile-time system. Outprintlines (x == y); // Prints is true}}
This is guaranteed by the specialty of the Java language:
Each string is literally a reference (§4.3) An example (§4.3.1, § 12.5) The square string (§4.3.3) is a constant value of string objects. String Literals- or more commonly, strings that are the value of continuous expression (§15.28) - "Internet" -additionally, using the example string. Intern, share a unique example.
Comments
Post a Comment