java interview Basics (d)

1, Q: java three characteristics have nothing to talk about, respectively.

  A: Inheritance: Following Cheng Houzai class automatically has the properties and methods of the parent class, but special attention is private property and the parent class constructor not be inherited. The benefits of code reuse can reduce the amount of code

       Package: a package also called information hiding, is the use of abstract data types and data based on the operation data packaged together, it constitutes a separate entity indivisible, data to be protected within the abstract data type, as much as possible to hide interior details, leaving only some of the external interface of the contact with the outside. Other parts of the system to communicate with the outside only through the interaction data is wrapped in the authorized operator to this abstract data types. In other words, users do not need to know the implementation details of the object inside the method, but you can access the object based on the external interfaces (object name and parameters) provided by the object. The advantage is to achieve a specific function code is packaged into a separate entity, each programmer can call when needed, in order to achieve a professional division, as well as the hidden information, implementation details.

     Polymorphism: polymorphism of three elements: 1. Polymorphic method is polymorphic, the polymorphism is not a property (the property is not polymorphic) have more than two states exist three necessary conditions: inherited, rewriting method, references to parent child class object. 3. After the parent class reference to sub-class object, method calls with reference to a subclass overrides the superclass, then multi-state arises.

    Abstract: In the object-oriented concept, all objects are described by type, but conversely, not all of the classes are used to describe an object, if a class does not contain enough information to describe a specific object, such a class is an abstract class. In addition to the abstract class does not instantiate objects, other functional classes still exist, member variables, methods and constructors of members to access the same manner as an ordinary class. Since the abstract class can not be instantiated objects, abstract class must be inherited, can be used. Also for this reason, usually decide whether or not an abstract design class at the design stage. Parent class contains a set of common subclass, but because of the parent class itself is abstract, so you can not use these methods. In Java abstract class represents is an inheritance, a class can only inherit an abstract class, but a class can implement multiple interfaces. Restrictions abstract class: 1. abstract class can not be instantiated (beginners easy to make the mistake), if it is instantiated, it will error, the compiler can not pass. Only the abstract class non-abstract subclasses can create objects. 2. abstract class does not necessarily contain abstract methods, but there are abstract methods of the class must be abstract class. 3. The method of abstract class merely declared abstract does not have a body, is not given method implementation method is specific functions. 4. The method of construction, a class method (a modified static method) can not be declared abstract methods. The subclass of abstract class must implement the abstract methods give a specific abstract class, unless the subclasses are abstract.

2, Q: briefly about the java heap and stack

   A: JAVA program is running, the space is divided in the memory 5 for storing data. They are: 1: register. 2: Local area method. 3: Method area. 4: Stack. 5: heap.

    Basic data types, local variables are stored in the stack memory, run disappeared. And an array of new instance of the object created, are stored in the heap memory, used up by the garbage collection mechanism from time to time automatically eliminated.

    Stack:

The basic types of variables defined in the function, a reference object are variable in the stack memory allocation function.
Features stack memory, a data is finished, the variable will be released immediately, save memory space.
Data in the stack memory, no default value is initialized, need to manually set.  

    stack:

Heap memory used to store new objects and arrays created.
All entities have the memory address of the value of heap memory.
Heap memory is used to encapsulate the data entities, these data have default initialization values.
When the heap entity is no longer pointing, JVM to start garbage collection mechanism to automatically clear, and this is one of C ++ JAVA superior performance (C ++ programmers need to be manually cleared).

    You can refer to: https://blog.csdn.net/jasonwang18/article/details/70578647 .

    To sum up can be: the difference between heap and stack can be used as a metaphor of view: Using the stack as we go to a restaurant for dinner, just a la carte (issued application), pay, and eat (use), fed on go, without regard to vegetable, vegetables and other preparation and washing dishes, scrubbing pots, etc. off the work, his advantage is fast, but a small degree of freedom. Heap is like a DIY like to eat the food, too much trouble, but more in line with their own tastes, and the large degree of freedom.

 

3, Q: explained the difference between final, finally, finalize the?

   A: In java, final can be used to modify the class, method and variable (member variables or local variables). When modified with the final class, indicating that the class can not be inherited by other classes. When we need to make a class never be inherited, this time can be modified with final, but be warned:

final class method will implicitly define all members of the final method.

    finally, as part of exception handling, it can only be in try / catch statement, and comes with a block of statements, the statements represent the final will be executed (with or without throwing an exception), often used in the release of resources needed case. When confronted exit, will exit the program, it will not be executed finally in the bottom of the statement. Otherwise it will finally executed, even when there is no catch, finally will come to execute the code of the program in return try.

    , Which means that each object has such a method finalize () is defined in java.lang.Object years. This method gc start, the object is recovered when they were called. In fact, most of the objects can be recycled gc (out of all new objects, gc can get, we will not use means other than to create new objects in general), so generally you do not need to finalize the programmer to achieve. 

Under special circumstances, require the programmer to achieve finalize, when the object is recovered free up some resources, such as: a socket link, created when the object is initialized, valid throughout the life cycle, you need to realize finalize, close the link. 
  Note also that the use finalize a thing, calling super.finalize ();

  After finalize an object () method is called only once and finalize () is called gc will not mean an immediate recovery of the object, it is possible to call finalize (), the object and do not need to be recovered, and then to the real to be recovered when, as a previous call once, it does not call finalize (), cause problems. Therefore, it is recommended not to use finalize () method, which with the destructor is not the same.

 4, Q: use Java to write a bubble sort. (In front of what is said in the python bubble sort, the first is Bubble Sort: https://www.cnblogs.com/cxiaocai/p/11040749.html )

   A:

Package Penalty for COM; 

public  class Blog {
 //     Principle bubble sort algorithm is as follows:
 //     comparing adjacent elements. If the first is greater than the second, the two of them exchanged.
//     for each to do the same work for neighbors, from the beginning of the first pair to the end of the last pair. At this point, it should be the last element is the largest number.
//     Repeat the above steps for all elements, except the last one.
//     continue to repeat the above steps for each time fewer and fewer elements, a pair of numbers until there is no need to compare. 
    public  static  void main (String [] args) {
         int [] ARR = {-97, -8,. 9, -5, 87,. 1, -87, 98 };
         for ( int I = 0; I <arr.length ; I ++ ) {
             int K = 0 ;
             for ( int j = i + 1; j < arr.length; j++) {
                if (arr[i] > arr[j]) {
                    k = arr[i];
                    arr[i] = arr[j];
                    arr[j] = k;
                }
            }
        }
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
        }
    }
}

5, Q: How do decided to use HashMap or TreeMap?

   A: TreeMap sort of key natural increase in the order, not the order of HashMap, HashMap fast speed they are non-thread-safe, you need to thread-safe, it is recommended to use HashTable.

Guess you like

Origin www.cnblogs.com/cxiaocai/p/11099714.html