Basic Java Interview

Source: Internet
Author: User

1. How to correctly exit a multilayer nested loop
    • Use labels and break;

    • Adding identifiers through the outer loop

2. The role of the inner class

An inner class can have multiple instances, each with its own state information, and independent of information from other peripheral objects. In a single perimeter class, you can have multiple internal classes implement the same interface in different ways. or inherit the same class. The moment when an inner class object is created does not depend on the creation of an external class object. The inner class does not have a confusing "is-a" relationship, it is like a separate entity.

3. Differences between Final,finalize and finally

Final is a modifier that can modify variables, methods, and classes.

The Finalize method is a method that is called before the object is recycled, giving the object its own last chance of resurrection, but when it is called Finalize is not guaranteed.

Finally is a keyword that is used in conjunction with try and catch for exception handling. The finally block is bound to be executed regardless of whether an exception occurred in the try block.

4. What are the uses of final?

1. A class that is final modified cannot be inherited
2. The final modified method cannot be overridden
3. A final modified variable cannot be changed. If the reference is decorated, then the reference is immutable and the reference to the content is mutable.
4. The final modified method, the JVM will attempt to be inline to improve operational efficiency
5. The final modified constants are stored in the constant pool during the compilation phase.

It is better to answer the two reordering rules that the compiler adheres to the final domain:

1. Writing to a final field within the constructor, and then assigning a reference to the constructed object to a reference variable, cannot be reordered between the two operations.
2. First read a reference to an object that contains a final field, and the final field is then first read, and cannot be reordered between the two operations

5. What are the uses of static?

Static variables, static methods, static blocks, static internal classes, static guide packages

6. What is the difference between a deep copy and a shallow copy?

Shallow copy: All variables of the copied object contain the same value as the original object, and all references to other objects still point to the original object. In other words, a shallow copy simply duplicates the value, not the object it refers to.

Deep copy: All variables of the copied object contain the same values as the original object, while those referencing the other object will point to the new object being copied, not the original referenced object. In other words, a deep copy copies the objects referenced by the object to be copied again.

7, String,stringbuffer and StringBuilder differences

Strings are string constants, final adornments; StringBuffer string variable (thread safe); StringBuilder string variable (thread unsafe).

The main differences between string and StringBuffer are performance:

The string is an immutable object, and each operation on the string type is equivalent to producing a new string object, and then pointing to the new string object. So try not to do a lot of concatenation of the string, otherwise there will be a lot of temporary objects, causing the GC to start to work, Affect system performance.

StringBuffer is an operation on the object itself, not a new object, so in the case of a lot of stitching we recommend using StringBuffer.

However, it is important to note that the JVM now performs some optimizations on string concatenation:
String s=“This is only ”+”simple”+”test”will be directly optimized by the virtual machine String s=“This is only simple test” , there is no stitching process at this time.

StringBuffer is a thread-safe mutable string whose internal implementation is a mutable array. The StringBuilder is new to JDK 1.5 and is functionally similar to StringBuffer, but is non-thread-safe. Therefore, using StringBuilder can achieve better performance without the multi-threading problem.

Basic Java Interview

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.