Think in Java learning note (1)

Source: Internet
Author: User
Chap 20 thousand all objects 1. Data Storage Data has six storage locations: 1) Register registers. 2) stack Stack: Located in the regular memory area. Primitive variables and object pointers (refrence) exist here. 3) Heap: All object data. 4) static storage: 5) fixed storage: 6) Non-memory storage: such as persistent object and streamed object. 2. the primitive type is a simple data type. To speed up the data, it is generally stored in the stack. Note: The default value of Class A {int I; // I is 0. That is, the class data member has the default value A () {Int J; // error. That is, the method data member has no default value.} 3. The code for variable lifetime/scope is correct in C/C ++, but is incorrect in Java: A () {int x = 1; {int x = 1; // error!} Chap 3 Control Process 1. Random Number random class, included in the Java. util package. Its nextint (int I) method returns [0 ~ (I) the random number between them, and the other nextfloat methods are also. Math. Random (): a static method that generates a random number between 0 and 1. The result is a double. 2. About = and! = For non-primitive objects, the default comparison operation is reference. Generally, the equals method can be used to implement the desired operation. However, for custom objects, the equals method must be customized because it compares reference.3 by default. method call: non-static methods cannot be called in static methods. Think about the truth. 4. Short expression T1 & T2 & T3. When T1 = true; t2 = false, T3. You can use this to write concise code. 5. Switch can only be used to select multiple options for int and char. 6. Convert all float and double types to int type, and then take the decimal part directly. For example, converting 5.9 to 5. Chap 4 initialization and cleaning 1. It is rare to use constructor in constructor. Example: Class A {private int I; private string Ss = new string ("null"); A () {This (3, "BBB");} A (int I, string Str) {This. I = I; SS = STR ;}} but note that the constructor cannot be referenced twice consecutively and can only appear in the first row. For example, this (I); this (SS); is incorrect. 2. Static
Strictly speaking, static is not an OOP method because it can be called without class instantiation. It is similar to a global function. If the program appears static multiple times, you have to doubt whether the program needs to be improved. Sometimes, static blocks are used in the program. It mainly supports anonymous internal classes. 3. Understand garbage collection (GC, Garbage Collector) 1) Before collection, execute finalize (). 2) In most cases, programmers do not need to consider the spam issue. 3) The mechanism of garbage removal is complicated. Fortunately, it is performed automatically in the background, but it may affect the system running speed. 4) garbage collection usually occurs when the memory is insufficient, so you do not know when to perform garbage collection. 4. initialization sequence of constructors and member variables public class stacktest {/*** @ Param ARGs */private tag II = new tag (1); stacktest () {tag JJ = new tag (2) ;}private tag KK = new tag (3); public static void main (string [] ARGs) {New stacktest ();}} class tag {tag (int I) {system. out. println ("tag (" + I + ")") ;}} output: Tag (1) Tag (3) Tag (2) The above function clearly indicates: when you create an object, initialize the member variable and then the constructor.

This obviously makes sense, because constructors may reference member variables.

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.