Crazy Java handout Reading Notes (1), java handout Reading Notes

Source: Internet
Author: User

Crazy Java handout Reading Notes (1), java handout Reading Notes

Instructor Li Gang's crazy Java handout (the third edition) is my book of enlightenment. I read it three times before and after, and each time I have a new understanding.

In the next period, we will record some classic knowledge.

1. the execution methods of advanced computer languages are compiled and interpreted. The former runs efficiently but cannot be cross-platform (C, C ++, Object-C ), the background can be cross-platform but inefficient (Ruby and Python ). Java is quite special. compile and generate. class first, and then explain it in JVM.

2. Garbage collection mechanism: relying on the garbage collection algorithm, when to recycle is transparent to Java programmers, so it is necessary to develop a good habit-do not reference unwanted objects. (Reclaim in heap)

3. Java is a strongly typed language. All class variables must be declared and used. variables of the specified type can only accept values that match the type.

4. Java supports two types: 1. Basic Type: boolean type and numerical type. 2. Reference Type: Class, interface, and array.

5. Forced type conversion: When overflow occurs, I always think that 19.745 will change to 19 (actually sweaty). In fact, it is converted to binary before being intercepted.

6. Constant pool: some data that has been identified during the compilation and saved in the. class file. Including constants in classes, methods, and interfaces, as well as string constants.

7. switch statement: the data type of the control expression can only be byte, short, char, int, enumeration, String.

8. The break and continue can be jumped to the specified loop layer through tags.

Public class BreakTest2 {public static void main (String [] args) {outer: for (int I = 0; I <5; I ++) {for (int j = 0; j <3; j ++) {System. out. println ("I value:" + I + "j value:" + j); if (j = 1) {break outer ;}}}}}

9. Stack and stack: 1. WhenMethodDuring execution, each method creates its own memory stack. The variables defined in this method will be put into the stack memory one by one. As the execution of the method ends, the memory stack of this method will also be automatically destroyed. Therefore, all the local variables defined in the method are placed in the stack memory; 2. When we create an object in the program, the object will be saved to the running data zone, for reuse, this operation is the data zone is the heap memory. Objects in heap memory will not be destroyed because the method ends. Even after the method ends, this object may be referenced by another variable, and the object will not be destroyed, the garbage collector of the system recycles a variable only when it is referenced by no reference variable;

10. Arrays: added tool class in java 8. It supports concurrent programming under the java. util package.

// Define an array a int [] a = new int [] {3, 4, 5, 6 }; // define an a2 array int [] a2 = new int [] {3, 4, 5, 6}; // The length of array a and array a2 is equal, each element is equal in sequence and true System is output. out. println ("array a and a2 are equal:" + Arrays. equals (a, a2); // By copying an array a, generate a new B array int [] B = Arrays. copyOf (a, 6); System. out. println ("array a and array B are equal:" + Arrays. equals (a, B); // outputs the elements of array B and outputs [3, 4, 5, 6, 0, 0] System. out. println ("B array element:" + Arrays. toString (B); // assign the 3rd (included) to 5th (excluded) elements of array B to 1 Arrays. fill (B, 2, 4, 1); // outputs the elements of array B, which output [3, 4, 1, 1, 0, 0] System. out. println ("B array element:" + Arrays. toString (B); // sorts array B by Arrays. sort (B); // outputs the elements of array B and outputs [0, 0, 1, 1, 3, 4] System. out. println ("B array element:" + Arrays. toString (B ));}

11. The Java language calls the constructor using the new keyword.

12. this Keyword: 1. Let a method in the class access another method or instance variable in the class. 2. The object represented by this is uncertain, but its class is definite. 3. In the constructor, this indicates the object being initialized by the constructor. 4. If this is returned in a method, you can call the same method multiple times consecutively.

Public class ReturnThis {public int age; public ReturnThis grow () {age ++; // return this returns the object that calls this method return this ;} public static void main (String [] args) {ReturnThis rt = new ReturnThis (); // you can call the same method rt consecutively. grow (). grow (). grow (); System. out. println ("rt's age member variable value is:" + rt. age );}}

13. static: Do not use objects to call static modified member variables. Use classes to call them.

14. Value Transfer: Pass the copy of the actual parameter into the method, and the parameter itself will not be affected.

15. Variable Method: when defining a method, add three points (...) after the type of the last parameter (...). Multiple parameter values are passed in as arrays. Compared with the input array, the variable parameter is more concise, but a method can only have one variable parameter.

Public class Varargs {// defines the public static void test (int a, String... books) {// books is treated as an array for (String tmp: books) {System. out. println (tmp);} // output the System value of integer variable. out. println (a);} public static void main (String [] args) {// call the test method test (5, "Crazy Java handout ", "lightweight Java EE Enterprise Application practices ");}}

16. Recursive Algorithm: A method must be recursive to a known direction when calling itself.

17. local variables: parameters (variables defined in the method signature), method local variables (defined in the method), and code block local variables (defined in the Code block ), A local variable does not belong to any class or instance. It is always stored in the stack memory of its method.

18. encapsulation: The member variables of the class are not directly exposed, but operated and accessed through methods, so that some restrictions can be added to the method.

19.High Cohesion: Try to hide the module's internal data and function implementation details in the module's internal independence, and direct external intervention is not allowed;Low Coupling: Only a small number of methods are exposed for external use.

20. static import: the import package method added by JDK1.5 is used to import the static member import static package in the package...

21. constructor: 1. constructor is an important way to create a Java object, but this object is not completely created by the constructor. 2. the constructor of the subclass must call the constructor of its parent class (the constructor without parameters is called by default when super and this are not used ).

22. method rewriting (overwrite) Principle: 1. Two values are the same: Method Name and parameter list. 2. Two smaller values: Return Value of subclass method <= return value of parent method. 3. A large value: subclass method access permission> = parent method access permission

23. When a program creates a subclass object, the system not only allocates memory for the instance variables defined in the class, but also allocates memory for all instance variables inherited from the parent class.

24. reference variable type: 1. compile-time type: determined by the type used to declare the variable. 2. runtime type: determined by the object actually assigned to the variable. 3. The compilation type and runtime type are different, the so-called polymorphism 4 occurs. The referenced variable can only call the method of its compile-time type at the compilation stage, however, when running, execute the method of its runtime type (you can use forced type conversion to solve the problem ).

25. Upward Transformation: assign a subclass object to the parent class to reference the variable; force type conversion: assign a parent class object to the subclass to reference the variable.

26 polymorphism: 1. variables of the same type present many different behavioral characteristics when calling the same method. 2. member variables do not have polymorphism and always call the value of the parent class.

27. instanceof: determines whether the previous object is a later class, or its subclass or implementation class instance.

String str="str";//trueSystem.out.println(str instanceof Object);Object obj=new Object();//falseSystem.out.println(obj instanceof String);

28. inheritance and combination: 1 represents "is a", and the combination represents "has a" 2. when a subclass object is created, the system allocates memory space for the instance variables defined by its parent class. Therefore, inheritance and combination do not have much difference in system overhead.

29. Initialization block: 1. the initialization block is executed before the constructor (the content of the initialization block will be restored to the constructor after compilation). 2. The static initialization block is executed before the normal initialization block.

30. Packaging class: The packaging class instance can be directly compared with the numerical type

31. toString: When customizing a class, try to override the toString method of the class to facilitate the output of instance values.

32. = and equals: 1. for the reference type, only the two point to the same object, = will be equal to true 2. the String type is as follows. The data determined during compilation is in the constant pool, and the data generated during runtime is in the heap memory.

// S1 directly references "Crazy Java" String s1 = "Crazy Java" in the constant pool; String s2 = "crazy"; String s3 = "Java "; // The String value after s4 can be determined at compilation. // s4 directly references "Crazy Java" String s4 = "crazy" + "Java" in the constant pool "; // The String value after s5 can be determined at compilation. // s5 directly references "Crazy Java" String s5 = "crazy" + "crazy" + "Java" in the constant pool"; // The String value after s6 cannot be determined during compilation. // The String s6 = s2 + s3 in the constant Pool cannot be referenced; // call the constructor using new to create a new String object. // s7 references the newly created String object String s7 = new String ("Crazy Java") in the heap memory "); system. out. println (s1 = s4); // outputs true System. out. println (s1 = s5); // outputs true System. out. println (s1 = s6); // outputs false System. out. println (s1 = s7); // outputs false

33. rewrite equals conditions: 1. self-defense: x. equals (x) = true 2. symmetry: If x. equals (y) = true, then y. equals (x) = true 3. passed: If x. equals (y) = true, y. equals (z) = true, then x. equals (z) = true 4. consistency: As long as x. equals (y) = true and x and y remain unchanged. No matter how many calls are called, The results remain unchanged. for any unsuitable Null x, x. equals (null) = false 6. if equals is the same, the hashcode is the same.

34. A null-type instance can be accessed through the static method and static variables of the category class at the underlying layer.

35. final: 1. the final variable cannot be assigned, but cannot be changed. the final variable must be the specified initial value displayed by the programmer. when final modifies the referenced variable, the referenced address cannot be changed, and the object can be changed. 4. the final modification method cannot be overwritten, but can be reloaded.

36. macro variable: when the final variable is defined, the initial value is specified for the variable and can be determined at compilation, the compiler will directly replace all the variables used in the program with the variable value (entering the constant pool)

37. Immutable class: 1. private final modifies all member variables. 2. Only getter does not have setter.

38. cache pool: the first-in-first-out cache instance is overwritten by equals and hsahcode.

Class CacheImmutale {private static int MAX_SIZE = 10; // use an array to cache the existing instance private static CacheImmutale [] cache = new CacheImmutale [MAX_SIZE]; // record the location of the cache instance in the cache, cache [pos-1] is the latest cached instance private static int pos = 0; private final String name; private CacheImmutale (String name) {this. name = name;} public String getName () {return name;} public static CacheImmutale valueOf (String name) {// traverses cached objects, for (in T I = 0; I <MAX_SIZE; I ++) {// if the same instance already exists, the cached instance if (cache [I]! = Null & cache [I]. getName (). equals (name) {return cache [I] ;}// if the cache pool is full if (pos = MAX_SIZE) {// overwrite the first cached object, put the generated object at the beginning of the cache pool. Cache [0] = new CacheImmutale (name); // set pos to 1 pos = 1;} else {// cache the newly created object, pos plus 1 cache [pos ++] = new CacheImmutale (name);} return cache [pos-1];} public boolean equals (Object obj) {if (this = obj) {return true;} if (obj! = Null & obj. getClass () = CacheImmutale. class) {CacheImmutale ci = (CacheImmutale) obj; return name. equals (ci. getName ();} return false;} public int hashCode () {return name. hashCode () ;}} public class CacheImmutaleTest {public static void main (String [] args) {CacheImmutale c1 = CacheImmutale. valueOf ("hello"); CacheImmutale c2 = CacheImmutale. valueOf ("hello"); // the following code outputs true System. out. println (c1 = c2 );}}

39. default method: 1. the methods added after JDK1.8 have three methods in the interface: abstract, static, and default. The latter two must have two methods. use the instance of the interface to call the default method

40. abstract class: abstract class is an abstract parent class of multiple child classes. It can be used as an intermediate product for system implementation. This intermediate product has implemented some of the functions of the system, however, this product cannot be regarded as a final product and must be further improved,

41. internal class: 1. provide better encapsulation 2. internal class members can directly access the private data of the external class. 3. the anonymous internal class applies to Class 4 that only needs to be used once. the local internal class and the anonymous internal class are not class members. 5. classes that contain internal classes are called external classes.

42. non-static internal class: 1. A reference to an external class is saved in a non-static internal class object. when an external Class Object accesses a non-static internal class member, it may not exist at all. static members cannot be defined in non-static internal classes.

43. static internal class: 1. static internal classes can contain static and non-static members. the static internal class is related to the external class. The static internal class object is parasitic to the class itself of the external class and only holds the class reference of the external class. There is no object reference of the external class.

44. use of internal classes: 1. try to use static internal classes (simple calls) 2. there is no egg for local internal classes. 3. non-static internal class constructor must be called by its external Class Object

Public class SubClass extends Out. in {// display and define the SubClass constructor public SubClass (Out out) {// explicitly call the In constructor Out through the incoming out object. super ("hello ");}}

45. anonymous internal class: 1. must inherit a parent class or implement an interface 2. cannot be an abstract class 3. the constructor cannot be defined. 4. local variables accessed by anonymous internal classes before JDK1.8 must be modified using final. final modification will be automatically added after JDK1.8.

46. Enumeration class: You can use enumeration classes to replace static final constants.

public class Enum {public enum Season{    spring,summer,fall,winter;}public static void main(String[] args) {    Season season=Season.spring;    System.out.println(season);    switch (season){    case spring:        System.out.println("spring");        break;    case summer:        System.out.println("summer");        break;    case fall:        System.out.println("fall");        break;    case winter:        System.out.println("winter");        break;    }    }

47. garbage Collection: 1. only recycles objects, not physical resources. the program cannot accurately control the running of garbage collection. 3. before recycling an object, the finalize method of the object will be called to try to obtain a new reference for the object to be revived 4. there is uncertainty about when the finalize method is called.

Public class FinalizeTest {private static FinalizeTest ft = null; public void info () {System. out. println ("finalize method for testing resource cleanup");} public static void main (String [] args) throws Exception {// create a FinalizeTest object and immediately enter the recoverable state new FinalizeTest (); // notify the System to recycle resources. // System. gc (); // ① // force the garbage collection mechanism to call the finalize () method of the recoverable object // Runtime. getRuntime (). runFinalization (); // ② System. runFinalization (); // ③ ft.info ();} public void finalize () {// Let tf reference the recoverable object to be recycled, you can restore the object to the reachable ft = this ;}}

48. reference Type: 1. strong reference (the most common reference) 2. soft reference (recycled when the system memory space is insufficient) 3. weak references (lower than soft references. When the system garbage collection mechanism runs, it will be recycled no matter the memory is insufficient) 4. virtual Reference (useless)

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.