Java Foundation interview (IV)

Source: Internet
Author: User
Tags throwable stringbuffer

31. String s = new string ("xyz"); how many stringobject have been created? Can I inherit the String class?

Two or one is possible, "XYZ" corresponds to an object, the object is placed in a string constant buffer, and the constant "XYZ" is the one in the buffer, no matter how many times it occurs. NewString creates a new object every time it is written, it creates a new string object using the contents of the constant "XYZ" object. If ' xyz ' had been used before, then "XYZ" would not have been created and would have been taken directly from the buffer, when a stringobject was created, but if "XYZ" had not been used before, then an object was created and put into the buffer, in which case it created two objects. As to whether the string class inherits, the answer is no, because the string default final decoration is not inheritable.

32. The difference between string and StringBuffer

The Java platform provides two classes: string and StringBuffer, which can store and manipulate strings, which are character data that contain multiple characters. This string class provides a string of values that cannot be changed. The string provided by this StringBuffer class can be modified. You can use StringBuffer when you know that character data is going to change. Typically, you can use Stringbuffers to dynamically construct character data.

33. The following statement creates a total of how many 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");

The first statement prints the result of false, the second statement prints a true result, which means that the Javac compilation can be optimized for the expression that adds the string constants directly, without having to wait until the run time to do the addition processing, but to remove the plus sign at compile time. Compiles it directly into a result that these constants are connected to.

The first line of code in the title is optimized by the compiler at compile time, which is equivalent to a string that directly defines an "ABCD", so the above code should only create a string object. Write the following two lines of code,

String s = "a" + "B" + "C" + "D";

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

The result of the final printing should be true.

34, try {} has a return statement, then immediately after this try finally{} code will not be executed, when executed, before return or after?

We know that the statement in the finally{} is bound to execute, then this may be normal to blurt out before return, after return may be out of this method, the ghost knows where to go, but more accurate should be in return in the middle of execution, see the following program code running results:

Public Classtest {

public static void Main (String[]args) {

System.out.println (Newtest (). Test ());;

}

static int Test ()

{

Intx = 1;

Try

{

RETURNX;

}

Finally

{

++x;

}

}

}

---------Execution Results---------

1

The running result is 1, why? The main function calls the sub-function and obtains the result the procedure, like the main function prepares an empty jar, when the child function returns the result, first puts the result in the jar, then returns the program logic to the main function. The so-called return, that is, the sub-function said, I do not run, your main function to continue to run it, there is no result, the result is to say this before put into the jar.

35, final, finally, finalize the difference.

Final is used to declare properties, methods, and classes, respectively, that the property is immutable, that the method is not overridden, and that the class is not inheritable. Internal classes to access local variables, local variables must be defined as final types.

Finally is part of the exception-handling statement structure, which indicates that it is always executed.

Finalize is a method of the object class that, when executed by the garbage collector, calls this method of the reclaimed object, and can override this method to provide garbage collection of other resource recycles, such as closing the file. However, the JVM does not guarantee that this method is always called

36. What are the similarities and differences between abnormal operation and general anomaly?

An exception represents an unhealthy state that may occur during a program's run, and a run-time exception that represents an exception that may be encountered in a typical operation of a virtual machine is a common run error. The Java compiler requires the method to declare a non-runtime exception that might occur, but does not require that a runtime exception that is not caught to be thrown must be declared.

37. What is the difference between error and exception?

Error indicates a serious problem in situations where recovery is not impossible but difficult. For example, memory overflow. It is impossible to expect the program to handle such situations. Exception represents a design or implementation issue. That is, it means that if the program runs normally, it never happens.

38. Simply talk about the simple principle and application of exception handling mechanism in Java.

Exceptions are abnormal conditions or errors that occur when a Java program is run (not compiled), similar to real-life events, where real-life events can contain information about the time, place, people, and plot of an event, which can be represented by an object, and Java uses an object-oriented approach to handling exceptions. It encapsulates each exception that occurs in the program into an object that contains information about the exception.

Java classifies exceptions, different types of exceptions are represented by different Java classes, and the root class for all exceptions is java.lang.throwable,throwable with two sub-classes derived below:

Error and Exception,error represent a serious problem that the application itself cannot overcome and recover, and the program only crashes, for example, a system problem such as memory overflow and thread deadlock.

Exception indicates that the program can also overcome and recover problems, which are divided into system exceptions and common exceptions:

System anomaly is the problem caused by the defects of software itself, which is caused by the poor consideration of software developers, and software users cannot overcome and recover the problem, but in this case the software system can continue to run or let the software hang out, for example, Array script out of bounds (arrayindexoutofboundsexception), null pointer exception (NULLPOINTEREXCEPTION), class conversion exception (classcastexception);

Ordinary exception is the operating environment changes or anomalies caused by the problem, is the user can overcome problems, such as network disconnection, hard disk space is not enough, after such an exception, the program should not die.

Java provides a different solution for system exceptions and common exceptions, and the compiler enforces common exceptions that must try: Catch processing or using the throws declaration continues to be thrown to the upper call method processing, so the common exception is also called the checked exception, and the system exception can be handled or not handled, so the compiler does not enforce with try. catch processing or declaration with throws, so system exceptions are also known as unchecked exceptions.

39. What is the difference between heap and stack in Java?
The heap and stack in the JVM belong to different memory regions and are used for different purposes. Stacks are often used to save method frames and local variables, and objects are always allocated on the heap. Stacks are usually smaller than heaps and are not shared across multiple threads, and the heap is shared by all threads of the entire JVM.

Stacks: Some of the basic types of variables defined in the function and the reference variables of the objects are allocated in the stack memory of the function, and when a variable is defined in a block of code, Java allocates the memory space for the variable in the stack, and when the scope of the variable is exceeded, Java automatically frees the memory space allocated for that variable. The memory space can be used immediately by another.

Heap: Heap memory is used to store the objects and arrays created by new, and the memory allocated in the heap is managed by the automatic garbage collector of the Java virtual machine. After creating an array or an object in the heap, you can also define a special variable in the stack that is equal to the first address of the array or object in the heap memory, and this variable in the stack becomes the reference variable of the array or object. The reference variable in the stack can then be used in the program to access an array or an object in the heap, which is equivalent to a name that is an array or an object.

40. Can I cast int to a variable of type byte? What happens if the value is greater than the range of type byte?
We can do the cast, but the int in Java is 32 bits, and byte is 8 bits, so if you force conversions, the high 24 bits of the int type will be discarded because the byte type range is from-128 to 128.

Java Foundation interview (IV)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.