Java basic knowledge trap (5)

Source: Internet
Author: User

Java basic knowledge trap (5)
For more information about static variable initialization, arrays, =, and equals, see the following code: public class Test {private final int age; private String name; public Test () {age = 30;} public Test (String name) {this. name = name ;}} I think many of the above Code know that the compilation will report an error because no age copy operation is performed in the meal substitute constructor. The final state variable can be assigned another two places in the final State program. One is the post-declaration value, and the other is the assignment value in the constructor. The code above constructs a parameter and changes it to the following: public Test (String name) {age = 30; this. name = name. Now we can add the modifier of this age variable to static. See the code: public class Test {private static final int age; private String name; public Test () {age = 30 ;} public Test (String name) {age = 30; this. name = name ;}let's talk about how this compilation works! This is also not acceptable. When the age variable becomes static, it no longer belongs to any object and belongs to the Test class, if you still assign values to modify the original value when constructing the object, it will definitely not work. It only assigns values when declaring the object. The code is modified as follows: public class Test {private static final int age = 30; private String name; public Test () {} public Test (String name) {this. name = name ;}} continue to read the following code to see if the code can be compiled? Public class Test {public static final StringBuffer sb = new StringBuffer (); public static void main (String [] args) {sb. append ("www. luoliang. me ") ;}} this is relatively simple. Because sb is a class constant, its reference address cannot be modified because it is a class, however, the content of the address can be modified. Continue to read the following code. Is the compilation successful? If it fails, what is the problem? Interface I {} public class demo {public static void main (String [] args) {I [] iArray = new I [2]; I I = new I ();}} as you can see from the code above, the interface cannot be instantiated, so new I () is definitely not. As for the new array to instantiate I, can this be done? First, we will parse it. Because the elements in the array are objects, the object can be null, in this case, new I [2] indicates an array of I objects with a length of 2. The elements of the array must inherit interface I, for example: class Test implements I {} iArray [0] = new Test (); now let's take a look at this = problem with equals. It is much better on the Internet. Now I will review it myself, see the following code: public class Test {public static void main (String [] args) {String str1 = new String ("luoliang. me "); String str2 = new String (" luoliang. me "); System. out. println (str1 = str2); System. out. println (str1.equals (Str2) ;}} what is output? This is simple and many people know it. If it is a basic type =, it compares its value, and if it is a reference type, it compares its address. So what about the equals method? All objects are subclasses of objects. Let's look at the objects in the jdk source code. We can see: public boolean equals (Object obj) {return (this = obj);} they are directly compared addresses, which are =. So what about the String? Let's take a look at the equals method of the String class: public boolean equals (Object anObject) {if (this = anObject) {return true;} if (anObject instanceof String) {String anotherString = (String) anObject; int n = count; if (n = anotherString. count) {char v1 [] = value; char v2 [] = anotherString. value; int I = offset; int j = anotherString. offset; while (n --! = 0) {if (v1 [I ++]! = V2 [j ++]) return false;} return true;} return false;} You can see that the source code first compares whether it is the same string. If yes, true is returned, if not, compare the characters of each of them. Now we know the answer. The output is as follows: falsetrue. Next let's look at the following code: class Person {public String name; public Person (String name) {this. name = name;} public String getName () {return this. name ;}} now, if two Person objects are compared, if the names are the same, it is determined that they are the same Person. How can this problem be solved? First, let's analyze: Determine whether the same person or not. This Object inherits the base class Object and has its own equals method. Then rewrite Override according to the String class method, see the following: class Person {public String name; public Person (String name) {this. name = name;} public String getName () {return this. name ;}@ Override public boolean equals (Object anObject) {if (this = anObject) {return true;} if (anObject instanceof Person) {Person person = (Person) anObject; if (person. name. equals (this. name )) {Return true ;}} return false ;}} the call is as follows: Person p1 = new Person ("luoliang"); Person p2 = new Person ("luoliang"); System. out. println (p1 = p2); System. out. println (p1.equals (p2); output: falsetrue is here this time. Keep recording!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.