Java Basics Interview (4)

31, String s = new String ( "xyz"); created a few StringObject? You can inherit String class?

        Two or are likely to, "xyz" corresponds to an object, the object in a string constant buffer, constant "xyz" No matter how many times appears, is that a buffer. NewString each write again, to create a new object, it uses the contents of the constant "xyz" object to create a new String object. If you previously used the 'xyz', this does not create "xyz", and get directly from the buffer, then create a StringObject; but if you have not used "xyz", then the time will create a object and put into a buffer which case it creates two objects. As to whether the String class inheritance, the answer is no, because the String default final modification, is not inherited.

 

32, the difference between String and StringBuffer

        JAVA platform provides two classes: String and the StringBuffer, which can store and manipulate strings, i.e., character data comprising a plurality of characters. This class provides a numeric string String immutable. This class provides the string while StringBuffer may be modified. When you know you want to change the character data when you can use StringBuffer. Typically, you can use StringBuffers to dynamically construct character data.

      

33, the following statement creates a total number of objects: String s = "a" + "b" + "c" + "d";

For the following code:

String s1 = "a";

String s2 = s1 + "b";

String s3 = "a" + "b";

System.out.println(s2 == "ab");

System.out.println(s3 == "ab");

Printing result of the first statement is false, the result of printing the second statement is true, indicating that javac compiler may be optimized with adding string constant expressions necessary to wait again for operation of adding process, but where the plus sign is removed at compile time, the results compiled into directly connected to one of these constants.

After the first line of code is the title optimizing compiler at compile time, the equivalent of a direct definition of "abcd" string, so, the above code should only creates a String object. Write the following two lines of code,

          String s ="a" + "b" +"c" + "d";

          System.out.println(s== "abcd");

The results of the final print be true.

 

34, try {} there is a return statement, then the code will finally followed after the try {} Lane will not be executed, when executed, return before or after?

        We know finally {} statements that will be executed, then this might blurt out normal it is before the return, after the return may be out of this method, and the devil knows where to run, but more accurately should be performed at an intermediate return , look at the program code following result:

public classTest {

    public static void main(String[]args) {

       System.out.println(newTest().test());;

    }

    static int test()

    {

       intx = 1;

       try

       {

          returnx;

       }

       finally

       {

          ++x;

       }

    }

  

}

 

---------Results of the---------

1

The result is a run, why? The main function to call a subroutine and the results obtained in the process, like an empty jar to prepare the main function, when the sub-function that returns a result, first results on the jar, and then the program returns to the main logic function. The so-called return, that is, Functions, I do not run, and you continue to run the main function of it, it was no result at all, the result is put in before saying this jar.

 

35 difference, final, finally, finalize the. 

        final declaration for properties, methods, and classes, attributes can not be changed, respectively, the method can not be overwritten, the class can not be inherited. To access the local inner class variables, local variables must be defined are final.

        finally exception handling part of sentence structure, means always executed.

        finalize is a method of the Object class, at the time of execution of the garbage collector calls this approach is subject to collection, can override this method to provide additional resources when recycling garbage collection, such as closing files. But the JVM is no guarantee that this method is called total

 

36. What are the similarities and differences with the general exception runtime exception?

        Anomalies indicate abnormal status during program execution can occur, runtime exceptions generally indicate abnormal operation of virtual machines that may be encountered, is a common run-time error. When abnormal, but does not require a statement uncaught when running java compiler must request a declaration of non-operational throw exceptions that may occur.

 

37, error and exception What is the difference?

        Restoration of error in the case of a serious problem not impossible but very difficult. For example, memory overflow. Program can not be expected to handle such a situation. exception that a design or implementation issues. In other words, it means that if run properly, the situation never happened.

 

38, to speak briefly on the simple principle and application exception handling mechanism in Java.

        An anomaly is a non-normal or error java runtime (non-compiled) occurred, with real-life events is very similar to real-life events may include the timing of events, places, characters, plot and other information, can is represented by an object, Java object-oriented approach to handle exceptions, it is the occurrence of abnormality of each program are respectively encapsulated into an object represented, the object contains information about the abnormality.

        Java abnormal classify different types of abnormalities are different Java classes, said the root class for all exceptions java.lang.Throwable, Throwable has derived the following two sub-categories:

        Error and Exception, Error means that the application itself can not overcome a serious problem and restore the program only ran the collapse, for example, said memory overflow systems and thread deadlock issues.

        Exception indicates that the program is also able to overcome the problems and recovery, which is divided into ordinary system abnormalities and anomalies:

        Abnormalities are caused by defects in the software itself, that is, software developers consider the problem caused by ill, and restore software users can not overcome this problem, but this problem can also allow the system to continue to run the software or let software hang, e.g., a script array bounds (An ArrayIndexOutOfBoundsException), null pointer exception (a NullPointerException), based translation exception (a ClassCastException);

        The problem is common abnormal operating environment changes or abnormalities caused, the user is able to overcome the problem, for example, network disconnection, enough hard disk space, after such an exception occurs, the program should not die.

java system abnormalities and abnormal general offer different solutions to the compiler enforces the general exception must try..catch or treated with throws statement calling the method continues to throw the upper handle, so common abnormality called checked exceptions, and the system can be abnormal processing may not be processed, so that the compiler does not force or by treatment with try..catch throws declaration, the system abnormality called unchecked exceptions.

 

39, the Java heap and stack What is the difference?
        The JVM heap and stack belong to different memory areas, the use of different purposes. Preservation methods commonly used in the frame stack and local variables, and the object is always allocated on the heap. Heap stack is usually relatively small, it will not be shared among multiple threads, the heap is shared by all threads across the JVM.

        Stack: some variables refer to variables and objects of the type defined in the basic function is a function of stack memory allocated, when a period of a block of code defines variables, Java memory space allocated for the variable in the stack, when after more than variable scope, Java will automatically freed memory space allocated for the variable, the memory can be immediately used it otherwise.

        Heap: heap memory used to store objects and arrays created by the new, the memory allocated on the heap, the automatic garbage collector Java virtual machine to manage. After generating a heap array or object, you can also define a special variable in the stack, the stack so that the value of this variable is equal to the first address of the array or object in the heap memory, stack variables into this a reference to an array or object variable, since you can access the heap array or object using the stack reference variable in the program, reference variables is equivalent to a name from an array or object.

 

40, can cast to byte int type of variable it? If the value is greater than the range of byte type, what phenomenon will occur? 
        We can do a cast, but in Java int is 32 bits, and the 8-bit byte, so if mandatory conversion, int type high-24 will be dropped, because the type of byte range from -128 to 128 .

Guess you like

Origin www.cnblogs.com/mxb0611/p/11910220.html