Java fundamentals must be mastered by question 10 Q:
Key points of knowledge:
Define classes, fields, static adornments, local, member variable initialization,
Inherit, (not) Check for exceptions, read and write streams, copy files.
1. How to define a class, a field, a method, and how to create an object of a class;
Answer: A class is an organizational unit of a Java program, and any Java statement is written in a class file. (So not all, package statements and import statements can be written out, but must be in the class file after compilation)
Class General form: Class classname{}
Define a field: A field in Java typically refers to a member variable (a database is a field)
Access type FieldName;
Method General form:
Access type methodname (parameter-list) {method Body}
Access modifier return type method name parameter list
Four ways to explicitly create an object:
Creating an object with the new statement
Invoking the newinstance () instance method of the Java.lang.Class or Java.lang.reflect.Constructor class by means of reflection
Calling the object's clone () method
The ReadObject () method of the Java.io.ObjectInputStream object is called by using serialization means.
Additional:
( when an object is a function parameter, only the state of the object can be changed in the function, but the object's reference cannot be changed.) )
The parameter passing type in Java is a value pass
2, can the local variables defined in the method not be initialized? Can a member variable defined in a class be uninitialized? If not initialized, what are the initial values for the string and int types variables?
Answer : Local variables must be initialized, defined in the class called member variables, can not be initialized,
To understand the necessity of initialization, you need to understand how the program works in a computer. The variables we declare are remembered by the computer, placed in a special memory, and the values of the variables are recorded in a different place. The meaning of the declarative process is: Tell the computer we created a variable called XXX, and the purpose of the initialization is to tell the computer that the value of the variable we created is yyy. If not initialized, then the computer will not know the value of XXX we created. At this point, either the system gives us a default value, or an error occurs!
Also note that static member variables are not initialized by default and must be manually initialized in either the initialization block or the definition or the constructor.
The byte,char,short,int,long,float,double initialization value of the global variable is 0,boolean default to False, and the object is null. Shaped like:
Note: The variable declared inside the main function is not a global variable, it is a local variable and can only be used in the main method body.
3, What is the difference between method modification and static and non-modification?
a : member variables and member methods that are modified by static are independent of any object of that class. that is , it does not depend on class-specific instances and is shared by all instances of the class. As long as this class is loaded, the Java virtual machine can find them based on the class name in the method area of the run-time data area. Therefore, a static object can be accessed before any of its objects are created, without referencing any objects.
There is no concept of global variables in the Java language.
4, what is inheritance, and what is the order in which constructors are called in inheritance?
A : Code reuse is implemented through inheritance. All classes in Java are obtained by inheriting the Java.lang.Object class directly or indirectly. Inherited classes are called subclasses, and inherited classes are called parent classes. Subclasses cannot inherit member variables and methods in the parent class that have access rights private. Subclasses can override the parent class's methods and name member variables with the same name as the parent class. However, Java does not support multiple inheritance, which is the ability of a class to derive from multiple classes of superclass. in Java, when a subclass is used to create an object, the parent class's parameterless constructor is called first, and then the constructor is called!
Note: the order of execution in inheritance:
1. Implement a static or static block-level code that is common to the parent class
2, the implementation of its own public static properties
3. Implementing the parent Class public property
4. How to perform the construction of the parent class
5. Implementing its own public properties
6. Execute the constructor of the function itself
7. Static method does not execute
Add: when the parent class has more than one constructor, the parameterless constructor is not executed if the subclass uses super (parameter information) to indicate that the constructor that invokes the parent class has arguments
5, what is the override of the method? What is the difference between override (override) and overload?
Answer: Override of Method (override of method)
1, which happens in the inheritance relationship.
2. Overridden methods in subclasses and methods that are overridden in a superclass must have the same return value type, the same argument list
overloads of the method ( overloaded)
- Occurs in the same class
- Overloaded methods and overloaded methods must have the same method name; You can have different return value types, and you must have a different parameter list
The difference between 6,int and integers; what is unpacking and boxing;
A: The difference between an int and an integer is, in large terms, the difference between the basic data type and its wrapper class:
int is the basic type, the value is stored directly, and the integer is the object, pointing to the object with a reference
Data types in 1.Java are divided into basic data types and complex data types
int is the former and the integer is the latter (that is, a class), so the variables of the int class are initially 0 when the class is initialized. The integer variable is initialized to null.
2. When initializing:
int i =1;integer i= new Integer (1);(to look at Integer as a class), but because of the automatic boxing and unpacking:
Auto-unpacking: The Name Incredibles is to re-convert the object to the basic data type:
Auto-Boxing: It can be simply understood that the basic data types are encapsulated as object types to conform to the Java object-oriented;
7, check abnormal and non-check exceptions;
A: Java's detectable anomalies and non-detection anomalies are distinct. A detectable exception is verified by the compiler, and the compiler enforces a processing or claim rule for any method that declares an exception.
Non-detection exceptions do not follow processing or claim rules. When such an exception is generated, it is not necessary to take any appropriate action, and the compiler does not check whether such an exception has been resolved. There are two main classes that define a non-detection exception: RuntimeException and error.
Why does the error subclass belong to a non-detection exception? This is because it is impossible to predict their time of production. If the Java application is out of memory, OutOfMemoryError may occur at any time , and the cause is generally not a special call in the application, but a problem for the JVM itself. In addition, the error class generally represents a serious problem that the application cannot resolve, and therefore treats these classes as non-detection exceptions.
The RuntimeException class is also a non-detection exception, one reason is that runtime exceptions thrown by ordinary JVM operations can occur at any time. Unlike error, this type of exception is typically raised by a specific operation. However, these operations occur frequently in Java applications. For example, if you have to write exception handling code to check for null references each time you use an object, the entire application will quickly become a large try-catch block. Therefore, run-time exceptions are not restricted by compiler check and processing or claim rules.
There is another reason to use the RuntimeException class as an undetected exception: the problem they represent is not necessarily treated as an exception. The Nullpointerex ception can be handled in the Try-catch structure, but it is simpler and more economical to test for null values before using references. Similarly, you can check the value of 0 in a division operation without using ArithmeticException.
8, the difference between Inputstream/outputstream and reader/writer?
A: Essentially, the biggest difference between Wirter/reader and Inputstream/outputstream is encode and decode.
Inputstream/outputstream
The Writer/reader is decode/encode when it is operated. It decode the data based on your system property file.encoding. For example, if you read a line from a file, the string returned with Reader.readline () is decode data. If your file's encoding is not equal to the value of your file.encoding, it will produce a coding error.
9, how to copy a file using Inputstream/outputstream?
Answer: By reading the byte stream, declare the buffer, approximate code:
InputStream is = new FileInputStream (C:\Java\a.txt); File DestFile = new file ("path", file name); OutputStream os = new FileOutputStream (destfile); byte[] buffer = new Byte[400];int Leng th = 0; while (length = is.read (buffer)) >0) { os.write (buffer, 0, length);} Is.close (); Os.close ();
10, how to read the properties configuration file;
Answer: . The main methods in properties are:
(1) load (inputstream instream)
This method can load a property list into the Properties class object from the file input stream corresponding to the. Properties property file.
(2) Store (OutputStream out, String comments)
This method saves the property list of the Properties class object to the output stream.
(3) Getproperty/setproperty
The two methods are to get and set property information, respectively.
Java basic question 10 q