| After adjusting for half a day, it is still not ideal. If it is adjusted before tomorrow, it will change as soon as it is released.
6. Knowledge about variable initialization in Java:6-1. The initialization sequence is divided into three steps:
1. initialize static variables and static blocks during class loading, first parent class and then subclass
2. When a new object is generated during running, allocate space for the object and initialize the instance variable. The first parent class and then subclass
3. when calling a constructor, first execute the constructor of the parent class, and then execute the constructor of the Child class. The specific process is when the constructor of the Child class is called, in the first line, the constructor of the parent class is called (explicit or implicit)
6-2. Initialization of various types of variables:
Application Type: NULL
Basic Type: Boolean: false
CHAR:/u0000
Byte: 0
Short: 0
INT: 0
Long: 0
Float: 0.0
Double: 0.0
6-3. array Initialization
When we generate an array of storage objects, we actually generate an array that stores references. After the array is created, each reference is automatically set to
. This value is expressed by the keyword null. When Java sees the null value, it regards this reference as "not pointing to any object ". Before using any reference,
You must first assign an object to it. If you use a reference and its value is null, an error will occur during the execution period.
Array initialization starts when space is allocated. Initialization rules are applied. The basic types are initialized according to the 6-2 rules. All application types are initialized to null.
Instance 1:
Int Index = 1;
Int [] Foo = new int [3];
Int bar = Foo [Index];
Int Baz = BAR + index;
What is the result?
A. Baz has the value of 0
B. Baz has the value of 1
C. Baz has the value of 2
D. An exception is thrown
E. The Code will not compile
Instance 2:
1. String Foo = "blue ";
2. boolean [] bar = new Boolean [1];
3. If (BAR [0]) {
4. Foo = "green ";
5 .}
What is the result?
A. Foo has the value of ""
B. Foo has the value of null
C. Foo has the value of "Blue"
D. Foo has the value of "green"
E. An exception is thrown
F. The Code will not compile
6-4. All instance variables in Java are initialized by default. All method variables are initialized by the method itself, and the variables in the method must be initialized before they can be applied.
Example:
Class Parent {
// Static variables
Public static string p_staticfield = "parent class -- static variable ";
// Variable
Public String p_field = "parent class -- variable ";
// Static initialization Block
Static {
System. Out. println (p_staticfield );
System. Out. println ("parent class -- Static initialization block ");
}
// Initialize the block
{
System. Out. println (p_field );
System. Out. println ("parent class -- initialization block ");
}
// Constructor
Public parent (){
System. Out. println ("parent class -- constructor ");
}
}
Public class subclass extends parent {
// Static variables
Public static string s_staticfield = "subclass -- static variable ";
// Variable
Public String s_field = "subclass -- variable ";
// Static initialization Block
Static {
System. Out. println (s_staticfield );
System. Out. println ("subclass -- Static initialization block ");
}
// Initialize the block
{
System. Out. println (s_field );
System. Out. println ("subclass -- initialization block ");
}
// Constructor
Public subclass (){
System. Out. println ("subclass -- constructor ");
}
// Program entry
Public static void main (string [] ARGs ){
New subclass ();
}
}
VII. constructor in Java
1. constructor cannot be inherited
2. Each class has at least one constructor, which is not defined by itself. The Compiler also assigns a default constructor without parameters.
3. the constructor of the subclass will certainly call the constructor of the parent class. The constructor called by Super () or explicitly or implicitly must exist;
If no explicit call is made, the compiler automatically adds the implicit call of super () to the first line of the constructor of the subclass, in this case, the parent class must have constructors without parameters (if the parent class is set
If the constructor has parameters, an error will be reported during compilation)
Example:
Class super1 {
Public int I = 0;
Public super1 (string text ){
I = 1;
}
}
Public class sub1 extends super1 {
Public sub1 (string text ){
// Super (text );
I = 2;
// Implicit superconstruct super1 () is undefined. Another structure must be explicitly cited
}
Public static void main (string ARGs []) {
Sub1 sub2 = new sub1 ("hello ");
System. Out. println (sub2. I );
}
}
8. Exception Handling in Java
1. exception in Java is a sub-run exception or non-runtime exception. The runtime exception is captured and processed by the runtime system (compilation is normal). Non-runtime exceptions must be handled (throw or captured)
2. Must catch be followed after try {} in the exception mechanism?
* Not necessarily, but it must be followed by finally. That is, catch and finally.
* Must catch be followed after try {} in the exception mechanism?
* Not necessarily, but it must be followed by finally. That is, catch and finally.
* Try {
*} Finally {}
* This is okay, and it doesn't make sense, because it ensures that the code in finally will be executed even if an exception occurs.
* Sometimes, this is very useful.
* For example, it can be used to release some resources occupied by the caller and then let the caller handle exceptions.
3. Finally in the exception will be executed. Even if a return statement exists in a method, it will be returned only after Exception Processing.
4. if an exception is thrown, the Child class can be first added to the parent class. If the child class is captured, the parent class will not be captured;
However, the parent class cannot be used as a subclass, which may cause compilation errors.
5. After exception handling, the program continues to execute
Instance:
/*
* If an exception is thrown during non-running, the catch block is used to capture and process the exception.
*/
Import java. Io. ioexception;
Public class exceptiontest {
// Public static void methoda (){
Public static void methoda () throws ioexception {
// Throw new nullpointerexception ();
// Try {
Throw new ioexception ();
// System. Out. println ("method exit ");
//} Catch (ioexception e ){}
// Finally {}
}
Public static void main (string [] ARGs ){
Try {
Methoda ();
// Throw new ioexception ();
} Catch (ioexception e) {system. Out. println ("caught1 ioexception ");
} Catch (nullpointerexception e ){
System. Out. println ("caught1 nullpointerexception ");
} Catch (exception e ){
System. Out. println ("caught exception ");
}
System. Out. println ("main exit ");
}
}
What is the output?
Ans:
9. bitwise AND logical operations
Bitwise operators (&, |) must be calculated on both sides.
If the left side of the operator (&, |) is true, it is not on the right side of the calculation.
Instance:
1. Public class test {
2. Private Static Int J = 0;
4. Private Static Boolean methodb (int K ){
5. J + = K;
6. Return true;
7 .}
9. Public static void methoda (int I ){
10. boolean B:
11. B = I <10 | methodb (4 );
12. B = I <10 | methodb (8 );
15. Public static void main (string ARGs []) {
16. methoda (0 );
17. system. Out. println (j );
18 .}
19}
What is the result?
A. The program prints "0"
B. The program prints "4"
C. The program prints "8"
D. The program prints "12"
E. The Code does not complete
|