Summary of some small things I will always forget (Android or Java), androidjava

Source: Internet
Author: User

Summary of some small things I will always forget (Android or Java), androidjava

Too much knowledge is pursued during project development, such as multi-threaded download and network requests. Some very small knowledge is often ignored, sometimes I will open up a project dedicated for verification to verify the idea and the actual situation. Next, I will verify some of it and see if you will ignore it?

1. Is arrayList. add (null) allowed? 2. Add a String object to ArrayList, change the String object, and add another object. Is the result that two identical objects are added or different?
ArrayList<String> strList = new ArrayList<String>();String str = "abc";strList.add(str);str = "123";strList.add(str);strList.add(null);System.out.println("strList = " + strList);
// Output result strList = [abc, 123, null]

So
1. ArrayList can be used to add null objects;
2. Add a String to the ArrayList and change the value before adding it. Two different strings are obtained.

If you use ArrayList to store objects, remember to leave it empty before using it !~
If you need to repeat the String type, define a String and overwrite the value without interruption !~

3. What if the foreach object is empty?
ArrayList<String> strings = null;for (String str : strings) {}
// Result Exception in thread "main" java. lang. NullPointerException

So
3. The foreach object cannot be empty; otherwise, a null pointer is reported.

It should be noted that if foreach is used for data transmission in the future, it is best to judge whether the data is empty first !~

4. If the initialization of String is null, what is printed?
String str = null;System.out.println("str = " + str);
// Output result str = null

So
4. If String is null, it is also null.

If you do not want this result, remember to leave it blank !~

5. How does Android get the width and height of TextView during initialization?
tv.measure(0, 0);int width = tv.getMeasuredWidth();int height = tv.getMeasuredHeight();

During initializationtv.getHeight()It is because the View has to waitonLayout()Can be used only after the endgetWidth()|getHeight()Obtain the width and height. SimilarlyonMeasure()You can only getgetMeasuredWidth(), First sentencetv.measure(0, 0);It is equivalent to a forced call.onMeasure()So we can get the width and height in two or three sentences (pay attention to the difference between dp and px ).

6. Exploring the attributes of sub-classes that cover the parent class

You need to "cook a chestnut" first:

Chestnut 1:
// Father. javapublic class Father {public String name = "Father";} // Son. javapublic class Son extends Father {public String name = "Son"; // The subclass overrides the attribute} // Main. javapublic static void main (String [] args) {// test 1 Father son = new Son (); // polymorphism System. out. println ("son name is" + son. name); // test 2 Son son1 = new Son (); System. out. println ("son1 name is" + son1.name );}

Output result:

son name is Fatherson1 name is Son

[Description] the property does not have polymorphism.

Chestnut 2:
// Father. javapublic class Father {public String name = "Father"; // The parent class adds a method public void printName () {System. out. println ("Father: name is" + name) ;}// Son. javapublic class Son extends Father {public String name = "Son"; // The subclass only overwrites attributes.} // Main. javapublic static void main (String [] args) {// test 1 Father son = new Son (); son. printName (); // print the call method // test 2 Son son1 = new Son (); son1.printName (); // print the call method}

Output result:

Father: name is FatherFather: name is Father

The results are the same.

[Description] subclass inherits the method of the parent class by default. If you do not override this method, the variable is used as the variable of the parent class.

We need to cook the third chestnut for further exploration:

Chestnut 3:
// Father. javapublic class Father {public String name = "Father"; public void printName () {System. out. println ("Father: name is" + name) ;}// Son. javapublic class Son extends Father {public String name = "Son"; // The subclass overrides the printName method public void printName () {System. out. println ("Son: name is" + name) ;}// Main. javapublic static void main (String [] args) {// test 1 Father son = new Son (); System. out. println ("--- son name is" + son. name); son. printName (); // print the call method // test 2 Son son1 = new Son (); System. out. println ("--- son1 name is" + son1.name); son1.printName (); // print the call method}

Output result:

--- son name is FatherSon: name is Son--- son1 name is SonSon: name is Son

[Description] subclass inherits the method of the parent class. If you override this method, it uses its own variable, even if it is defined by polymorphism.

Region to be updated ......

...... ** Too many minutes ** try to squeeze toothpaste ~ * Zookeeper has been completed before * zookeeper has been released. too many bytes * too many bytes */

If you have any questions, suggestions, or forgotten knowledge, please leave a message and let me know !~

Copyright Disclaimer: This article is an original article by the blogger. For more information, see the original article address and author (Alibaba ).

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.