JAVA language hands-on brain problems

1.

Such early and often define the variable
  int value = 100;
the previous example so defined variable
  MyClass obj = new MyClass ();
This variable is defined in two ways is the same as you?
 
Answer: not the same, which opens up space, and the latter means that the construction space for the object obj MyClass class, and call the constructor of MyClass.
 
 
 
2.

 

 

 

Object variable can also use "==" to determine whether the value of the variable is equal to two right?

A first execution result is true, the second execution result is false, there is a variable which represents the object can not use the string variable == Analyzing two values ​​are equal, as they are the addresses of two comparison objects or strings, If you declare two objects or strings then their address is not equal to it will be false.

However, if there are two objects obj1 = obj2, then a determination result after == true, the value is determined to be true if the two strings are equal, or objects that will use equals () method, and equals () method from inheritance, although there is no written succession in the main method, but it inherits the parent class Object class, and is the largest of the parent class, so by default, do not write inheritance.

 

3.

Please summarize, this method is what "distinguishes", you can list a few?

 

 

 

 

 

 Please summarize, this method is what "distinguishes", you can list a few?

A: The method and class names are the same and no return type (not void), and time if not declared constructor, the system will automatically generate a default constructor, inherited from the Object class, and if, declared constructor, so go call the default constructor will throw an error, which is why the second picture compilation process will complain because the construction provides a method, but it calls the default constructor error

 

 

4. Results of the following

 

Thus, the definition of the specified class member 1 performs initialization block or the default value of the class, which need to see a "top surface" performed in the end, 2 constructor executes class. Class initialization block does not receive any parameter as long as an object and a class is created, they will be executed. Therefore, the package is suitable for those "code must be executed when the object is created."

 

 

 

5.TestStaticInitializeBlock.java execution results are as follows

. 1  class Root
 2  {
 . 3      static {
 . 4          System.out.println ( "Root static initialization block" );
 . 5      }
 . 6      {
 . 7          System.out.println ( "normal initialization block the Root" );
 . 8      }
 . 9      public Root ( )
 10      {
 . 11          System.out.println ( "Root no argument constructor" );
 12 is      }
 13 is  }
 14  class Mid the extends Root
 15  {
 16      static {
 . 17         System.out.println ( "Mid static initialization block" );
 18 is      }
 . 19      {
 20 is          System.out.println ( "Mid general initialization block" );
 21 is      }
 22 is      public Mid ()
 23 is      {
 24          System.out.println ( "Mid zero-argument constructor" );
 25      }
 26 is      public Mid (String MSG)
 27      {
 28          // by calling this same class constructor overloads 
29          this ();
 30          System.out.println ( " Mid constructor parameters with which the parameter values: "+ MSG);
 31 is      }
 32  }
 33 is class Leaf the extends Mid
 34 is  {
 35      static {
 36          System.out.println ( "Leaf static initialization block" );
 37 [      }
 38 is      {
 39          System.out.println ( "Leaf normal initialization block" );
 40      }    
 41 is      public Leaf ()
 42      {
 43          // by calling the parent class super has a string constructor parameter 
44 is          super ( "the Java presentation initialization sequence" );
 45          System.out.println ( "Leaf performed constructor" );
 46      }
 47  
48  }
 49  
50 public class TestStaticInitializeBlock
51 {
52     public static void main(String[] args) 
53     {
54         new Leaf();
55         
56 
57     }
58 }

The results are as follows

 

Can be found, for use sequentially, A subclass B, B is a subclass of C, C, if the constructor is executed, is configured to perform the method of A, B is executed in the constructor, the final implementation of the constructor C.

On the other hand, a static configuration block execution order highest priority, then the general configuration code block, then the constructor with no arguments, and finally there is a constructor parameter.

Further, first performs a static block of code for each class, then the last execution of each block non-static class above order.

 

 

6.

Static method only allows access to static data, then, how in a static method access instance members of the class (ie no additional field or method static keyword)?
Please write code to verify your ideas. (Published in the blog) code is as follows

. 1  public  class jingtaihanshu {
 2  int X =. 4 ;
 . 3  static  int Y =. 5 ;
 . 4  public  static  void Method, () {
 . 5      System.out.println ( "instance variable = X" + new new jingtaihanshu () X.); // access class instance variables need to hold instantiated class in a static method 
. 6      System.out.println ( "static variable = Y" + Y); // in a static method can directly access the static class variable 
. 7      
. 8  }
 9  public  static  void main (String [] args) {
 10      jingtaihanshu.Method ();
 . 11      jingtaihanshu ASD =new new jingtaihanshu ();
 12 is      System.out.println ( "new variable x is:" + asd.x);
 13 is  }
 14 }

The results are:

Examples of variables x = 4
Static variable y = 5
new variable x is: 4

It can be seen in the external static method call, you can use the "class name. Method name" approach, you can also use the "object name. Method name" approach. And examples of the latter method only way. In other words, you can call the static method without creating an object

 

7.Integer class boxing and unboxing in the end is how to achieve?

 

 

 1 public class BoxAndUnbox {
 2 
 3     /**
 4      * @param args
 5      */
 6     public static void main(String[] args) {
 7         int value=100;
 8 
 9         Integer obj=value;  //装箱
10 
11         int result=obj*2;     //拆箱
12 
13     }
14 
15 }

 

 

 

Each primitive data type of packaging are the full body of each data type identifier of the first letter capitalized, e.g. int Integer class is a wrapper class, which first define a variable, and then packaging [1] = identifier Identifier 2; then, the original data type identifier unpacking identifier 3 = 2 * 2; decompiled then after discovery, the process of packing is achieved by a method call valueOf wrapper, the unpacking process is the method by calling wrapper intValue achieved.

 

 

 

 

8.

 

 

Two pairs of integers is obviously exactly the same, why the output of a true, one output false?

 

 

 

 Thus valueOf () meet again a new object and then == comparison, is the address compare two objects, so do not equal, so the result of the code is false.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/tkg1314/p/11684898.html