Think in Java Reading Notes

Source: Internet
Author: User
Tags bitwise operators

Chapter 1: Object Overview
1. The five features of Java are also five object-oriented features:
Everything is an object: all objects
A program is a bunch of objects telling each other what to do by sending messages: a program is a group of objects that pass information between each other and tell the other party what to do.
Each object has its own memory made up of other objects: each object uses other objects to form its own memory.
Every object has a type: all objects have types.
All objects of a participant type can receive the same messages: all objects of the same type can receive the same information.

2. In the first chapter, I talk about some OOP concepts. In my opinion, there are also many philosophical things. I may have an epiphany when I read them several times.

 

 

Chapter 2: Object

1. in Java, what we directly manipulate is not the class itself, but an instance of the class, or a reference. Java has no address transfer.
(Chap2, P2)

2. Java stores objects, that is, classes in the "heap", and stores the reference of other data and objects in the "stack" to collect operations, stack is faster than stack.
(Chap2, P3)

3. Because the stack is faster than the heap, Java's primitive type variables are also stored in the stack as a special case, which can improve efficiency. On the other hand, primitive data is not a class, so they do not have a reference.
(Chap2, P4)

4. Java does not allow defining variables with the same name in the same method, for example:
{
Int x = 12;
{
Int x = 16;
}
}
This is allowed in C ++, but a compilation error occurs in Java.
(Chap2, P7)

5. You don't need to worry about clearing reference that is no longer in use. Java's gabage collector will help you do everything.
(Chap2, P8)

6. For a primitive type variable, if this variable is a member of the class, the class will initialize it. If it is not a member of the class, it will not be initialized, it may be an arbitrary value.
(Chap2, P9)

7. javadoc is very powerful, but it requires us to have rich comments and good habits when writing programs.

 

 

Chapter 3: Process Control

1. Almost all operators can only work on primitive. But "=", "=", "! = "Is an exception. They can be applied to all objects. In addition, the string class also supports" + "and" + = ".
(Chap3, P2)

2. The value assignment of the reference variable will cause the reference on the left of the expression to lose the original object. The original object becomes memory garbage without reference.
(Chap3, P3)

3. Books in Java always emphasize that Java does not have address transfer, but I think that reference transfer is address transfer.

4. Integer n1 = new INTEGER (47 );
Integer n2 = new INTEGER (47 );
System. Out. println (n1.equals (N2 ));
The printed result is true, so do not take it for granted. In fact, equals compares the reference. Here there are two references, which are obviously not equal. The reason why true is output is, this is because the equals function has been used in the integer class.
After processing, if it is a self-written class without overloading equals, the print must be false.
(Chap3, P11)

5. Java provides bitwise operators, but I don't think it is necessary to use them.
(Chap3, p15)

6. in Java, the comma operator can only be used in a for loop.
(Chap3, p37)

7. Switch can only use char, byte, short, and Int.
(Chap3, p43)

 

Chapter 4: initialization and cleaning
1. Use primitive-type overload with caution.
(Chap4, P7)

2. the return value cannot be overloaded, because although the method has a return value, the program does not pay attention to the return value, for example:
Definition: int F () {}; string F (){};
When F () is called, the virtual machine does not know which F to call.
(Chap4, P11)

3. A common method in a class cannot call constructor. constructor can call each other, but can only use the this keyword.
(Chap4, P13)

4. In general, you do not need to use finalize () in the class because the virtual opportunity is automatically cleaned up. However, in some special cases, an object is declared, but there is no refrence. For example:
Class Aclass (){....};
...
New Aclass ();
Because there is no refrence, the virtual opportunity can be recycled if it is useless. However, if you do not want it to be recycled, it can be implemented in the finalize function, examples of specific reference books.
(Chap4, p16)

5. Internal variables must be initialized before they are used. The so-called "use" indicates the right side of the current expression and the parameters of the method, rather than the left side of the expression, for example:
String S = "I love Java ";
Int I;
I = S. Length ();
Is correct; and
Int I;
I ++;
Yes. Because I ++ is relative to I = I + 1
However, if it is not an internal variable but a member data of the class, Initialization is not required because the compiler does not know which method will be initialized. For primitive member data, Java automatically assigns the initial value, for example:
Boolean = false
Char = (char) 0 ---- Space
Byte = 0
Int = 0
Long = 0
Float = 0
Double = 0
For the member data of an object, it is equal to null before initialization. Therefore, primitive's member data is used before initialization and will not cause errors, but the object data will encounter exceptions at runtime.
There is a special situation, such:
Int [] A = new int [4];
At this point, it seems that only the size of the array is initialized, but the members of the array are not initialized. But in Java, this is no problem. Java automatically initializes each array member.
(Chap4, P22)

6. When the class is instantiated, the definition of the member data is always executed first (if Initialization is performed during the definition), and then the constructor is executed, whether in the code order, the member data is in the front or the constructor is in the front.
(Chap4, P26)

7. For static member variables, static member variables are always initialized before other member variables. static member variables are only initialized once, only static member variables used are initialized.
Here, "used" is used to check from the main function, if the main function defines static variables or other classes called by the main function define static variables, these static variables will be "used.
(Chap4, P2)

8. An array of the primitive type can be initialized using new, for example, int [] A = new int [] {, 3,4 }, you can also use the method such as int [] A = {1, 2, 3, 4} for initialization. However, if it is not a primitive variable, you can only use new for initialization.
(Chap4, p34)

9. During array initialization, int [] A = {1, 2, 3, 4,}; 4 is followed by a comma, right? A: Yes. The last comma and none can be used.
(Chap4, p35)

 

 

Chapter 5: hidden implementations

1. Not every Java file requires a public class, but a Java file can only have one public class. If there is no public class, the file name can be retrieved at will.
(Chap5, P4)

2. When using import, the referenced class library can be found either in classpaht or in the relative path of the current path added with import, for example, import COM. kingworld. util. If the current path is D:/javawork,
The imported class libraries can be in the D:/javawork/COM/kingworld/util directory.
(Chap5, P7)

3. for Java package, if it is packaged as a jar file, you must put the file in a path that can be found in classpath.
(Chap5, P7)

4. Java accessors include public, protected, private, and package. The accessors here refer to the accessors of class members. The access permissions are: public> package> protected> private.
The access permission of a package is not only for other classes in the same package to access the public, protected, and package members of this class, but also for private members.
(Chap5, P10)

5. The class has no private access permission relative to the member's access permission, because the private class has no value and does not have the protected access permission.

 

Chapter 6: Reuse

1. tostring is a special method. When the compiler needs a string and your class is an object, the compiler automatically calls the tostring method. Of course, make sure that you have this method in your class.
(Chap6, P2)

2. Writing a main function to each class makes debugging much easier.
(Chap6, p6)

3. If there is no default (No parameter) constructor in the base class and a parameter constructor exists, the base class constructor must be called in the subclass constructor, otherwise, an error occurs during compilation. That is, when the subclass is instantiated
The virtual machine automatically calls the default constructor of the base class, unless the non-default constructor of the base class is explicitly called in the constructor of the subclass.
(Chap6, P9)

4. the compiler will forcibly place the call of the constructor of your base class at the top of the constructor of the derived class. That is to say, there cannot be anything before it.
(Chap6, P9)

5. Although the compiler will force you to initialize the base class and require you to complete initialization at the beginning of the constructor, it will not check whether you have initialized the member object.
(Chap6, P11)

6. merging or inheritance? In general, synthesis is used for new classes to use the functions of the old classes, rather than for their interfaces. That is to say, embed the object and use it to implement the functions of the new class, but the user sees the new tired interface instead of the embedded interface.
Object interface.
(Chap6, p16)

7. Generally, the class member data should be defined as private.
(Chap6, p16)

8. Upload (upcasting) is always secure.
(Chap6, p19)

9. The private method implies the meaning of final. Because you cannot access the private method, you cannot rewrite it. You can add a final modifier to a private method, but this does not make any sense.
(Chap6, P24)

The private function mentioned in 10 and 9 cannot be rewritten. This function is private in the base class and private in the derived class. If it is not private in the derived class, it can be rewritten.
(Chap6, P24)

 

Chapter 7: Polymorphism

1. "encapsulation" creates a new data type by combining data features and behaviors. By setting the details to private, "Hide implementation" completes the separation between interfaces and implementations. Polymorphism is from the perspective of "class ".
Process this logical separation.
(Chap7, P2)

2. "Upload" enables the dynamic nature of the class, but it should be noted that only when the base class is public can the extension class be re-written. For example:
Public class test {
Private void F (){
System. Out. println ("Private F ()");
}

Public static void main (string [] ARGs ){
Test po = new derived ();
Po. F ();
}
}

Class derived extends test {
Public void F (){
System. Out. println ("Public F ()");
}
}
In the example, test. f is private, so derived. f is not actually test. f, derived. f is a brand new method, and it cannot even be considered as a heavy load, because derived cannot see F () of the base class at all ().
Therefore, the output should be private F () rather than public F ().
(Chap7, P12)

3. If a class contains one or more abstract methods, the class must be defined as abstract. However, if the class does not have abstract methods, the class can also be defined as abstract.
(Chap7, P13)

4. Calling sequence of constructor:
① Call the constructors of the base class. This is a recursive process, so we will first create the root of the inheritance system, then the next-level derived class, and so on until the last constructor of the inheritance class
② Initialize Member objects in the declared order
③ Execute the body of the constructor of the inherited class.
In fact, it is better to put a vote in this way: in the initialization process of a class, initialize these member variables in the declared order of the member object, and then execute its constructor; if there is a base class, initialize the base class first.
(Chap7, P17)

5. If you want to clean up the memory, you can write a function from the base class, and the function name can be set by yourself. However, you must execute the function from the top layer when calling the function, this is exactly the opposite of the calling sequence of the constructor.
(Chap7, P18)

6. When you start Java, do not think about making the entire program into a class system. The better way is to synthesize it.
(Chap7, P23)

 

 

Chapter 8: interfaces and internal classes

1. the method in the interface is automatically public, that is, if you do not set it, it does not think it is a package like a class, but public. In addition, the methods in the interface cannot be private or protected.
(Chap8, P3)

2. When carrying out inheritance and implementation at the same time, when declaring a class, it should be inherited before implementation.
(Chap8, p6)

3. Using interfaces or abstract classes? If there is no implementation of member data and methods, the interface should be prioritized.
(Chap8, P7)

4. The member variables in the interface are automatically public and final, so you do not need to declare them. This allows you to implement functions similar to the enum in C language. For example:
Public interface months {
Int January = 1, February = 2, March = 3, limit L = 4, may = 5, June = 6, July = 7, August = 8, September = 9, october = 10, November = 11, December = 12;
}
(Chap8, P10)

5. An internal class is a very valuable feature that allows you to logically organize mutually dependent classes and control internal access permissions of the class. But remember, the internal class and synthesis are completely different.
(Chap8, p15)

6. Internal classes can be created in methods, or even in any scope.
(Chap8, P18)

7. The internal class can access any member of the host class.
(Chap8, P24)

8. Nested classes are static internal classes.
(Chap8, P26)

9. Each internal class can inherit an "Implementation" independently ). Therefore, internal classes are not subject to the constraint of "whether the host class has inherited other implementations.
(Chap8, p34)

10. Although the author has mentioned many advantages of internal classes, I still don't think I can understand them very well. Let's wait for a while and think about it.

 

 

Chapter 9: handling errors caused by exceptions

1. If a method throws an exception during declaration, such as public void F () throws simpleexception, exception capture is required during the call.
(Chap9, p6)

2. When an error message is printed, system. Err is better than system. out because the latter may not be redirected.
(Chap9, p6)

3. self-created exception classes can have constructors with parameters or other members.
(Chap9, p6)

4. The printstacktrace method of the throwable class returns the information about how the called method reaches the exception location in order.
(Chap9, P7)

5. exception description (that is, keep up with the throws keyword behind the method and the class name of the exception to be thrown) can give programmers a clear idea of what exceptions this method may throw.
(Chap9, P9)

6. The nullpointerreference virtual opportunity is automatically thrown out without calling it everywhere.
(Chap9, P18)

7. Whether or not an exception is thrown, The Finally block is always executed.
(Chap9, P20)

8. Principle of exceptional use:
① Handle the problem in a proper place. (Avoid capturing exceptions when you do not know how to handle them)
② Solve the problem and call the method that caused the problem again.
③ Correct the problem and then dye the method to continue
④ Use some other numbers that are not allowed to be returned by this method for calculation.
⑤ Finish all the work that can be done in the current allowed environment, and then throw the same exception to a higher level
⑥ Finish all the work that can be done in the current allowed environment, and then throw a different exception to a higher level
7. Stop the program
Streamline (if the exception structure makes things too complicated, it will be very painful and annoying to use)
Producer makes the class libraries and programs safer (both short-term investment in debugging and long-term investment in program robustness)

 

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.