3. Array initialization
1. An array is a fixed-length collection that cannot change its length after it has been initialized.
The array is allocated in the heap, and a reference to the array is stored in the stack.
class arr{ publicstaticvoid main (string[] args) { int [] A = {1, 2, 3, 4}; int [] B = A; for (int i = 0; i < b.length; i++) { B[i]++ ; } for (int i = 0; i < a.length; i++) { System.out.print (a[i]); }}}
Output results: 2345
For other references, when other references to another object are used, they point to the same area in the heap.
2. Using arrays.tostring () to print an array
You can reduce the array traversal by using this method, but only for one-dimensional arrays
Arrays.tostring (a);
3. Variable parameter list
As the name implies, the number of input parameters is variable. Such as:
Private Static void PrintArray (String ... strs)
{ System.out.println (arrays.tostring (STRs));}
This specifies that the passed in parameter can only be a string type, but in practical use, more objects are used,
This brings a benefit: You can use a method to provide method calls to different input parameters, eliminating the overloaded work.
// input New Invokestaticblock (), ' a '); Private Static void PrintArray (Object ... strs) { for (object object:strs) { + "");} }
Output:
1 1 1.11 [email protected] A
4. Inheritance Initialization
The compiled code for each class exists in its own standalone file. The file is loaded only if you need to use the program code. In general, tired code is loaded when it is first used.
classparentclass{protected StaticString B = "B"; Static{System.out.println ("ParentClass.Static.Block"); } protected Static voidoutput (String s) {System.out.println ("Parentclass.output ():" +s); }}classSonclassextendsparentclass{PrivateString c = "C"; Static{System.out.println ("SonClass.Static.Block"); } Public Static voidMain (string[] args) {System.out.println ("Sonclass.main ()"); Sonclass son=NewSonclass (); Son.output (parentclass.b); }}
Output Result:
ParentClass.Static.BlockSonClass.Static.BlockSonClass.main () Parentclass.output (): b
The order of initialization is:
(1) First find the main method, get the corresponding class file, so that the Java virtual machine will know what to load the program;
(2) Initializes the parent class, and, if there is a parent class, continues to initialize the parent class;
(3) First initializes the parent class static data, and then initializes the other member variables, most of which execute the constructor;
(4) The subclass is also the same number sequence, and finally executes the contents of the main function.
Java Learning Note two: Initializing (iii)