Thursday, February 4, 2016

[Java]9 questions about initialisation and recover

1 : How Java distinguish the overlaoded methods ?
 By by parameter type and order

2 read and explain why error :
   public static void testLong(long i){
         System.out.print("test long");
   }
   
   public static void testFloat(float f){
          System.out.print("test float"); 
  }
  
   public static void main(String[] args){
             testLong(5); // non error : 5 is int, it can be upcast to long
             testFloat(1.5); // error : 1.5 is double!!! cannot downcast to float


the correct should be : testFloat(1.5f) ---> 1.5f is a float

3 read and explain why error :
   public static class A{
         A(int i){System.out.println("A(int i)");}
   }

    public static void main(String[] args){
     A a = new A(); // when we defined a constructor, the default constructor is not usable any more, we should redefine the constructor A().!!
}

Compilation error!

4 read and explain :
 public class A{
         A(){ System.out.println("A()");}
    
         A(int i){ System.out.println("A int");}
         
         A(int i, int j){ 
             A(); //compilation error : cannot find A() ---> should be this()! 
             A(i);
             System.out.println("A int i int j");
             
         }
         
          public static void main(String []args){
             A a= new A();
            System.out.println("Hello World");
         }
}
we can use this() to call another constructor in a constructor, but : this() can be only call once,  and it should be the first line of the new constructor


5 read and tell the results :
 public class A{
     private int i;
     private String j;
     int getI(){return i;}

     String getJ(){return j;}

     A(int i){i=i;} // attention here! this.i = i , 
 //in this block when we write i, the first java think is the input i int.
     A(String j){this.j=j;}

     public static void main(String []args){
        System.out.println(new A(5).getI());
        System.out.println(new A("hello").getJ());
     }
}

result :  0 hello

6 we got static methods and non-static methods, can we call non static methods int static methods ?
Non! Because static is a class method, non static method is a object method,and the object method can only be called after initializing the object.

7 why static cannot be used for a local variable ?
static value is belong to the class, it's created when do the compilation.  But normal variable is belong to the object, it will be created after initialization.

8 finalize() what, why and how
we uese finalize() to release memory , when you call this, the GC will recover the memory in next GC time, normally we don't call it in the code, the object created by new will be recover by GC automatically, but some memory is tocken not by new, in this case we need finalize() .

9 garbage collection techs :

  1. reference counter : every time we create a reference from the object, we add one for the counter, and when the reference is set to null or destroyed, we minus one.  when we found the object's counter is <1, GC will release the memory.
  2. stop and copy : how to gc : stop running, and copy the survived objects from a heap to another heap.
  3. mark and sweep : mark all alive objects and when finished marking, do the GC.


No comments:

Post a Comment