1. Why is the following code not compiled? Where did it go wrong?
Public class test{ publicstaticvoid main (String []args) { Foo obj1=New Foo (); }} class foo{ int value; Public Foo (int initvalue) { value=initvalue; }}
The error is that there are parameters at the time of the constructor, and no parameters of type int were entered when called.
2. According to the output of the following code, self-Summary Java field Initialization law.
Public classTest { Public Static voidMain (string[] args) {Initializeblockclass obj=NewInitializeblockclass (); System.out.println (Obj.field); Obj=NewInitializeblockclass (300); System.out.println (Obj.field); }}classinitializeblockclass{//The following sentence affects the initial value of the Field field before and after the initialization block//public int field=100;{field=200; } Public intfield=100; PublicInitializeblockclass (intvalue) { This. field=value; } PublicInitializeblockclass () {}}
Output: After initialization 200,300; at initialization in the first 100,300.
Rule: 1) executes the default value specified by the class member definition or the initialization block of the class, exactly which one you want to see "in front of"
2) Execute the constructor of the class
Note: The initialization blocks of the class do not receive any parameters, and they are executed whenever an object of the class is created. Therefore, it is appropriate to encapsulate the code that must be executed when the object is created.
3. Run the Teststaticiniitializeblock.java example, observe the output, and summarize the order of execution of the static initialization block.
classroot{Static{System.out.println ("Static initialization block of root"); } {System.out.println ("Root's normal initialization block"); } PublicRoot () {System.out.println ("Root parameterless constructor"); }}classMidextendsroot{Static{System.out.println ("Static initialization block for mid"); } {System.out.println ("Normal initialization block for mid"); } PublicMid () {System.out.println ("No parametric constructor for mid"); } PublicMid (String msg) {//calling overloaded constructors in the same class through this This(); System.out.println ("Mid with parametric constructor, its parameter value:" +msg); }}classLeafextendsmid{Static{System.out.println ("Static initialization block of the leaf"); } {System.out.println ("Plain initialization block of the leaf"); } PublicLeaf () {//A constructor that invokes a string argument in the parent class through Super Super("Java Initialization sequence Demo"); System.out.println ("The constructor that executes the leaf"); }} Public classteststaticinitializeblock{ Public Static voidMain (string[] args) {NewLeaf (); }}
1) static initialization block executes only once
2) When creating objects of subtypes, the execution of static initialization blocks of the parent type is also caused.
4. An interesting question
Static methods allow access to static data, so how to access instance members of a class in a static method (that is, a field or method that does not have a static keyword attached)
Please write code to verify your idea
class test{ staticint num; int qwe; Static void foo () { num=0; Test T=new test (); T.qwe=0; }}
How is the boxed and unboxing of the 5.Interger class implemented?
box, automatically creates the corresponding integer object based on the value, unpacking, automatically converting the wrapper type to the base data type.
6. Two pairs of integers exactly the same, why one output true, one output false?
Public class Test { publicstaticvoid main (string[] args) { Integer i1= ; Integer J1=100; System.out.println (i1= =J1); Integer i2=129; Integer J2=129; System.out.println (i2= =J2);} }
When you create an integer object by using the ValueOf method, if the value is between [-128,127], a reference to the object that already exists in Integercache.cache is returned, otherwise a new integer object is created.
The values for I1 and I2 in the above code are 100, so the objects that already exist are taken directly from the cache, so i1 and I2 point to the same object, while i3 and I4 point to different objects respectively.
The third time to move the brain