I feel that I still have a poor understanding of the object-oriented thinking of Java I just launched this semester. In the afternoon, I did not intend to go over Java programming ideas. bruce Eckel wrote everything is an object in chapter 2. This sentence is very simple, but he does not know when he can really understand and use it well!
Haha, I will not talk much nonsense. I will post my own Java jobs. At this stage, I will talk about conceptual things. Most of the answers below are the original words in the book, A few are organized by your own understanding! There must be many mistakes or misunderstandings. I hope you can point them out. Thank you!
Description: The textbook uses Java 2 programming, edited by Chen Guojun Chen Lei Chen xiyong Liu Yang, and published by Tsinghua University Press. This book is very poorly written (the original words of the teacher). The teacher often mentions errors during lectures, but he said this is actually good, this saves him the trouble of finding the opposite textbook to explain the problem!
1. What is the difference between public and private members of a class?
A: public members are modified using public. They can be accessed by methods of any object.
Private Members are modified by private. They only allow access by methods of their own classes.
2. What is a method overload?
A: In an object-oriented programming language, a method with the same method name has different functions if the number of parameters is different or the number of parameters is the same but the type is different.
3. What is the role of a class constructor? If a class does not declare a constructor, can this program be correctly executed? Why?
A: ① The main function of the constructor is to initialize class objects.
② Yes, Because Java will automatically call the default constructor
4. What are the features of the constructor?
Answer: (1) the method name and Class Name of the constructor are the same.
(2) The constructor does not return values, nor can it write void.
(3) The main function of constructor is to initialize class objects.
(4) The constructor generally does not call the constructor explicitly, but uses new to call the constructor.
When creating a class object, the system automatically calls the Class Construction Method to initialize the new object.
5. Can I call another constructor in one constructor? If yes, how can I call it?
A: Yes.
When calling another constructor in a constructor, you must use the this keyword to call the constructor. Otherwise, an error occurs during compilation. The this keyword must be written in the first row of the constructor.
6. What are the differences between static variables and instance variables?
A: There is only one static variable in the memory. the Java Virtual Machine allocates memory for the static variable during class loading. The static variable is located in the method area and is shared by all instances of the class. Static variables can be accessed directly through the class name. the life cycle of a static variable depends on the life cycle of the class, and the instance variable depends on the instance of the class. Each time an instance is created, the Java Virtual Machine allocates memory for the instance variable once. The instance variable is located in the heap area, and its life cycle depends on the life cycle of the instance.
7. What are the differences between static methods and instance methods?
A: ① The static method is resident memory, and the instance method is not. Therefore, the static method is highly efficient but occupies the memory.
② The static method allocates memory on the stack, and the instance method is on the stack.
③ Instance methods can be called only when an instance is created, but not static methods.
8. Why is it illegal to call a non-static member in a static method?
A: Because a static method belongs to the entire class, it cannot manipulate or process members of an object, but can only process members of the entire class, that is, static methods can only access static member variables or call static member methods.
9. What is the difference between equality of objects and references pointing to them?
A: equality of objects means that the values of objects are equal, but two objects occupy different memory spaces. The reference to them is equal to the memory addresses of the two objects.
10. What is a static initiator? What is its role? Who will execute the static initiator? What is the difference between it and the constructor?
A: The static initiator is a statement group enclosed by a pair of curly braces "{}" modified by the keyword static.
It is used for initialization.
Static initiators are called and executed by the system when the class is added to the memory.
Differences: (1) the static initiator is used to initialize static data members of the class. The constructor is used to initialize the newly created object.
(2) The static initiator is not a method, and there is no method name, return value, or parameter table.
(3) The static initializer is called and executed by the system when its class is loaded into the memory, and the constructor is automatically executed when the system generates a new object using the new operator.
See yizero by yizero.com