Java BASICS (2)
+ = Implicit forced type conversion.
X + = y; equivalent to x = (Data Type of x) (x + y); function overload?
The function name is the same, and the parameter list is different. It is not related to the return value, that is, what is the function overload encapsulation?
Hiding the attributes and implementation details of an object only provides public access.
Classes and methods are also the embodiment of encapsulation.
What is inheritance?
Concept: extract the same content from multiple classes, define them to a single class (parent class), and inherit the parent class when defining other classes (Child classes.
Benefits: 1. Improved code reusability and reduced code repetition rate. 2. Improved code maintainability. 3. Made the relationship between classes and the premise of polymorphism.
Inheritance features: Classes in Java can only be individually inherited, but can be inherited at multiple layers.
Note:
All constructors in the subclass will access the constructor with null parameters in the parent class by default, because the first line of each subclass constructor has the default statement super ();
If there is no constructor with null parameters in the parent class, you must use the super statement in the constructor of the Child class to specify the constructor in the parent class to be accessed.
If the subclass constructor uses this to specify to call the subclass's own constructor, The called constructor will also access the constructor in the parent class.
What is an anonymous object? What are application scenarios?
Application Scenario: A: Call method. However, only one call is allowed. B: The anonymous object is used for passing actual parameters.
What is the difference between method rewriting and method overloading? Can I change the type of the returned value through overload? (* Interview questions)
Method Rewriting: The subclass contains the same method declaration as the parent class.
Method overload: methods with the same method name and different parameter lists appear in the same class.
It has nothing to do with the return value.
Overload can change the type of the returned value because it has nothing to do with the returned value. What is the static keyword? What are the features? When will it be used?
Static Keyword: Indicates static content. It can modify the member variables and member methods in the class.
Static features: loaded with the class, prioritized with the object, shared by all objects, can be called through the class name. Static content. A: The local code is fast: it is the code included in {} in the method. The function is to limit the lifecycle of a variable and improve efficiency.
B: Construct the code quickly: in the class, out of the method. Code included in. The function is to extract the same content from all constructor methods,
It is defined in the constructor code block. In the future, the constructor will automatically call the constructor code block when calling the constructor. Constructing code is faster than constructing a method.
C: Fast static code: inside the class and out of the method. Code included in. Only static modification is added.
The function is to load the Code only once as the class is loaded. How does one instantiate a class? (* Interview questions)
Student s = new Student (); steps in the memory.
1. Load the sudent. class file into the memory (class Loader)
2. Apply for a space with s variable in the stack memory
3. Apply for space for Student object in heap memory
4. initialize the member variables in the class by default.
5. Display and initialize the member variables in the class.
6. If a constructed code block exists, the constructed code block is executed first. If no code block exists, the code block is omitted.
7. Execute the constructor to initialize the object data through the constructor.
8. After the data in the heap memory is initialized, what are the features of copying the memory value to the constructor execution in the s quantum parent class? Why?
The execution of subclass constructor will first execute the constructor of the parent class.
Because the child classes may directly access the data of the parent class, the data of the parent class takes precedence over the Child class data for initialization. What are the final keywords, what can be modified, and what are their features?
Final Keyword: indicates the final meaning. It can modify classes, methods, and variables.
Modifier class: the class cannot be inherited.
Modification Method: The method cannot be overwritten.
Modifier variable: the variable is a constant. What is polymorphism and what is the premise?
Polymorphism: multiple states of objects at different times. It is a phenomenon in which the state of the compilation period is inconsistent with that of the running period.
Member variables: Compile to the left, and run to the left.
Member method: Compile to the left and run to the right. Common member methods can be rewritten, but variables cannot.
Static Method: compile to the left and run to the left.
What is an abstract class? What are the characteristics and benefits of abstract classes?
The same method has the same method declaration, but the method body is different. Only the method declared by the method is extracted, which is called the abstract method and the class of the abstract method is called the abstract class.
Features:
A: The class or method must be modified in abstract.
B: When a specific class inherits an abstract class, it is either an abstract class or all abstract methods in the abstract class.
C: The abstract class cannot be instantiated. To use it, you must use it in a multi-state manner.
D: Member features:
A: member variables
It can be a variable or a constant.
B: Constructor
Constructor is available, but cannot be instantiated.
It is used to initialize the sub-class to access the parent class data.
C: Member Method
Abstract METHODS only define method declarations, but not method implementations.
Benefits:
A: specify that subclass must implement certain functions.
B: Improves code reusability.
Several questions about abstract classes:
A: The abstract class cannot be instantiated. What is the purpose of the constructor.
It is used to initialize the sub-class to access the parent class data.
B: What is the significance of abstract classes without abstract methods?
Restrict the creation of objects.
C: Which keywords cannot coexist with abstract?
Final: Conflict
Private: Conflict
Static: meaningless
What is an interface? What are the features of interfaces?
If all methods in an abstract class are abstract methods, java provides a more abstract expression for this type of abstract class: interface.
Features:
A: All methods are abstract methods.
B: class implementation Interface
It is either an abstract class or an abstract method in the interface.
C: The interface cannot be instantiated. To use it, use polymorphism.
D: Member features
A: member variables
Only constants and static constants exist. Default modifier: public static final
B: Constructor
No constructor. By default, the initialization of subclass data adopts the constructor of the Ojbect class.
C: Member Method
All are abstract, with the default modifier: What is the difference between the public abstract class and the interface? (* Interview questions)
A: Member differences
Abstract class:
Member variable: it can be a variable or a constant.
Constructor: Yes.
Member Methods: Yes, can be abstract or non-abstract.
Interface:
Member variable: can only be a constant. Default modifier: publci static final
Constructor: No. By default, the initialization of subclass data adopts the constructor of the Ojbect class.
Member method: can only be abstract. The default modifier is public abstract.
B: differences between classes and interfaces
Classes and classes:
Inheritance relationship, single inheritance.
Class and interface:
Implementation relationship, single implementation, multiple implementation.
Interfaces and interfaces:
Inheritance relationships: single inheritance and multi-inheritance.
C: Differences in Design Concepts
Abstract classes are inherited to reflect the relationship between is. Abstract classes generally define the common functions of the entire inheritance structure.
The interface is implemented as follows: the relationship between like. Interfaces generally define extended functions of the entire inheritance structure. Internal class access features:
A: Internal classes can directly access members of external classes, including private ones.
B: To access members of an internal class, the external class must create an object. Why must I add final to the local variables accessed by internal classes? (* Interview questions)
A: Prevent data changes after use.
B: extend the lifecycle of a variable. What is an anonymous internal class? What is the essence?
An anonymous internal class is an internal class without a name.
Format:
New Class Name or Interface Name (){
Rewrite method;
};
Essence: anonymous objects that inherit classes or implement interfaces. = And equals? (* Interview questions)
A: =
A: The basic type compares the value of the basic type.
B: The reference type compares the address values of the reference type.
B: equals ()
Only reference types can be compared.
The default compare address value. What are the differences between String, StringBuffer, and StringBuilder? (* Interview questions)
String: The character length is fixed.
StringBuffer/StringBuilder: The character length is variable.
StringBuffer: secure, but less efficient.
StringBuilder: insecure, but more efficient.
What is the basic data packaging class compatible with StringBuffer and StringBuilder?
Corresponding type
Byte Byte
Short Short
Int Integer
Long Long
Float Float
Double Double
Char Character
What is the difference between a boolean set and an array? (* Interview questions)
Set:
The length can be changed.
Only object types can be stored, and reference types can be stored.
You can store any type of objects.
Array:
Fixed Length.
You can store basic types or reference data types.
Only elements of the same type can be stored. What is generic? What is the purpose? Where can I use it? What are the advantages and disadvantages of generics?
Generic is a special data type that can be specified only when an object is created or a method is called.
Optimized Program Design and solved the problem of yellow warning lines.
Advances the exception during the running period to the Compilation Time.
This avoids forced type conversion.
Disadvantage: The data types are unified and different data types cannot be stored. How does HashSet ensure the uniqueness of elements?
The underlying data structure is a hash table (hash table ). Specifically, an array composed of one-way linked list elements.
It depends on two methods: hashCode () and equals.
Execution sequence:
First, judge whether the hashCode () is the same,
If the same
Inherit and execute the equals () method to see its return value:
True: duplicate elements are not stored.
False: The elements are retained and stored.
If different
Storage.
Remember:
When we see a set of HashXxx structures, we need to know that the elements stored in the Set need to override the hashCode () and equals () methods.
In addition, it is automatically generated. What is the underlying data structure of TreeSet? How can we ensure the uniqueness of elements?
The underlying data structure is a binary tree.
Determines whether the returned value is 0.
How can we ensure the sorting of elements?
A: Compared with natural sorting Elements
Implement the Comparable interface for the class to which the stored elements belong.
B: The comparator sorting set is comparable.
When creating a collection object, let the constructor receive a subclass object of the Comparator interface.
Differences between natural sorting and comparator sorting:
TreeSet constructor does not transmit anything. By default, it is in the order of Comparable (interface, compareTo (T) (ClassCastException is reported if Comparable is not implemented)
If TreeSet is passed into the Comparator (interface compare (T1, T2), what is the underlying data structure of the ComparatorLinkedHashSet? How can we ensure the uniqueness of elements?
The underlying layer consists of a linked list and a hash table.
Ordered by the linked list.
The hash table must be unique. What are variable parameters?
When writing a method, you do not know the specific number of formal parameters.
Java provides Variable Parameter usage.
Note:
A: The variable is actually an array of the data type.
B: If there are multiple formal parameters, the variable parameter can only be the last one.
C: The data types must be consistent. What is the difference between Hashtable and HashMap? (Interview questions)
A: The HashMap thread is insecure and efficient. Allow null keys and null values.
B: Hashtable thread security and low efficiency. The null key and null value are not allowed. What is the difference between throws and throws? (* Interview questions)
Throws
Location: After method (), it is followed by the class name. It can be followed by multiple exception class names separated by commas (,).
Throws an exception and sends it to the caller for processing.
If RuntimeException and its subclass are followed, this method does not need to be processed.
If exceptions and their subclasses are followed, you must write code for processing or throw them when calling them.
Throw
Location: name of the object to be followed in the method. Only one exception object can be followed.
Indicates that an exception is thrown and is processed by the method body statement.
If throw throws RuntimeException and its subclass In the method, the Declaration can have no throws.
If throws throw an Exception and its subclass in the method, throws must be declared. What are the differences between final, finally, and finalize? (* Interview questions)
Final: The final meaning, used to modify classes, variables, and methods. When modifying a class, the class is the final class and cannot be inherited.
When modifying a variable, the variable is a constant and cannot be changed. When modifying a method, the method cannot be overwritten.
Finally: A part of exception handling. The code in it will always be executed (premise: the jvm has not exited) and is generally used to release resources.
Finalize: A method of the object class for garbage processing. What is recursion? What do you need to pay attention to when using recursion?
Recursion is the phenomenon of calling the method itself in the method definition.
A: recursion must have an exit; otherwise, it is dead recursion.
B: The number of recursion times cannot be too many; otherwise, the memory overflows.
C: constructor methods cannot be used recursively. What are the basic IO streams? (* Interview questions)
Byte stream: InputStream, OutputStream, FileInputStream, FileOutputStream,
Streams: Writer, Reader, FileWriter, FileReader,
Efficient byte stream: BufferedInputStream, BufferedOutputStream
Efficient response streams: BufferedWriter and BufferedReader
At the beginning, only byte streams were used. However, because of the appearance of Chinese characters or other characters, they were expressed in two bytes.
If you use Word throttling, you can read and write the data of character files, but it is troublesome. To simplify this operation, the producer stream is provided. What is the difference between flush () and close? (* Interview questions)
Flush (): refresh the buffer, and the stream object can still be used.
Close (): the stream resource is released, but the buffer is refreshed first. After the operation is completed, the stream object cannot be used again. How many implementation solutions are available for multithreading? What are the differences? What should I do? (* Interview questions)
Multithreading can be implemented in two ways:
1. inherit the Thread class
The custom class inherits the Thread class, re-writes the run () method in the class, creates a custom class object in the test class, and calls the start () method.
2. Implement the Runnable interface
The custom class implements the Runnable interface, overwrites the run () method, and creates a custom object in the test class,
Create a Thread object and pass the custom object as the construction parameter. Call the start () method of the Thread class. Thread lifecycle? (* Interview questions)
New: Create a thread object
Ready: qualified for executing the cup, no execution right, ready to execute at any time
Run: qualified for execution, execution right, execution of code in run ()
Congestion: When a thread runs to a condition that meets our definition, it stops and waits for wake-up.
Death: run () is over multithreading. Why is there a security problem? How can this problem be solved? (* Interview questions)
If the following conditions are met, security issues may occur:
A: A multi-threaded program.
B: Shared data.
C: There are multiple statement operations for shared data.
As long as we change the operations that share data in a multi-threaded environment into a single thread, there is no problem.
Java provides the synchronization technology for this situation:
A: Synchronous Code Block
B: Synchronization Method
C: What are the Lock objects for Lock Synchronization After JDK 5? (* Interview questions)
Code block: Any object
Method: this
Static Method: What is the difference between class name. classsleep () and wait? (* Interview questions)
Sleep (): the lock object must be released at a specified time.
Wait (): You can specify the time or not. Release the Lock Object. What are the three elements of network communication? (* Interview questions)
A: IP Address
The unique identifier of the computer in the network.
The following is used: "dot decimal"
B: Port
Application tag.
C: Protocol
Communication rules. What is reflection? How can I retrieve bytecode files through reflection? What are the benefits of reflection? (* Interview questions)
In the running state, the constructor, member variables, and member methods are used through the class Object (Class Object. Reflection.
Three methods:
A. Use the getClass method of the Object class.
B. The static class attribute of any data type can be obtained.
C. Use the static method forName (String className) of the Class to obtain
Benefit: as long as there is a class or a class object, you can get all the attributes and methods of this class or object. Including private.