Java Side Question Summary (constantly updated)

Source: Internet
Author: User
Tags finally block

Graduation season, for programmers is also a good job search opportunities, a good resume is the key to open the door of the company, but can enter their ideal company, become one of them, lies in the success or failure of the interview. This article will introduce some of the common interview questions raised by companies and do some simple answers. Also for their own new job, do not go to the company to ask questions to find the West East.

One, Java four characteristics: abstraction, encapsulation, inheritance, polymorphism.

(1) Abstract: From the literal meaning can understand, like is a little vague meaning, not sure good meaning. In object-oriented concepts, we know that all objects are depicted by classes, but not all classes are used to depict objects, and if a class does not contain enough information to depict a specific object, such a class is an abstract class. Abstract classes are often used to characterize the abstract concepts we derive in the analysis and design of the problem domain, as an abstraction of a series of concrete concepts that look different, but are essentially the same, and we cannot instantiate them (not get a concrete one) so they are called abstractions. For example: We want to describe the "car", it is an abstraction, it has a mass, volume and other commonalities, but lack of characteristics (not sure what type of car (cars, buses, etc.), they have their own characteristics), we do not have the only one to represent the car (because neither the car nor the bus can represent the vehicle) , an abstract class can be used to describe it, so an abstract class cannot be instantiated. When we use a class to describe the "car", this class can inherit the abstract class that describes the "car", so that the "car" is a "car".

(2) Encapsulation: Hide the properties and implementation details of the object, only expose the interface externally, control the access level of the Read and modified properties in the program. Then why encapsulate it? 1. Hide the implementation details. For example, you buy a car, you just need to know how to drive, do not need to understand its implementation principle. 2. Security. For example, you have privatized the age attribute in a program, and provided external get and set methods, when the outside world uses the Set method to assign a value, you can judge within the set method, control within a reasonable range (0-100 years old), so that the outside world can not be arbitrarily assigned. 3. Code reusability. For example, the various methods encapsulated in the tool class, you can repeat the call anywhere, without having to implement its details in every place. 4. Division of Labor. Encapsulation is divided into attribute encapsulation, method encapsulation, class encapsulation, plug-in encapsulation, module encapsulation, System encapsulation and so on. Facilitate the process of division of labor, non-interference, convenient between the modules of the mutual combination and decomposition, but also conducive to code debugging.

(3) Inheritance: is an object-oriented most significant feature, inheritance is derived from the existing classes of the new class is called a subclass, the subclass inherits the data properties and behavior of the parent class, and can expand the new behavior according to its own requirements, improve the reusability of the code. Simplified understanding: When two classes have the same characteristics (attributes) and behaviors (methods), you can extract the same parts into one class as the parent class, and the other two classes inherit the parent class. After inheritance, subclasses automatically own the properties and methods of the parent class, but it is particularly important to note that the parent class's private properties and construction methods cannot be inherited. In addition, subclasses can write their own unique properties and methods, the purpose is to implement the extension of the function, subclasses can also replicate the parent class method is the method of rewriting.

(4) Polymorphism: The same thing, calls its same method, the parameters are the same, but behaves differently. In addition, there are three prerequisites for implementing polymorphism in Java: inheritance, rewriting, and upward transformation. Inheritance: Subclasses and parent classes that have inheritance relationships must exist in polymorphism. Rewrite: Subclasses redefine some methods in the parent class and call the methods of the subclasses when they are called. Upward transformation: In polymorphic, a reference to a subclass needs to be assigned to the parent class object, so that the reference can have the ability to invoke methods and subclasses of the parent class.

Second, the difference between rewriting and overloading (brief answer).

When a subclass inherits from a parent class, and the method in the child class is exactly the same as the name of the method in the parent class, the number of arguments , and the type, the method in the subclass is called overriding the method in the parent class.

For the same class, if there are two or more methods with the same name in the class, but the method has at least a different number of parameters, type, and order, this time constitutes the method overload.

Third, Java class load order.

a Java file from being loaded into the unloaded life process, a total of 5 stages, the JVM divides the class loading process into:
load-On (validate + prepare + Parse) initialization (pre-use preparation), use, uninstall
(1) Loading
The binary byte stream is obtained by the fully qualified name of a class, and the static storage structure represented by this byte stream is transformed into the runtime data structure of the method area. Finally, a class object representing this class is generated in the Java heap as the access entry for the data in the method area. In general, the binary data of the class is found and loaded.
(2) Links:
Validation: Ensure that the class being loaded is correct;
prepare: Allocates memory for static variables of the class and initializes them to default values;
Parse: Converts a symbolic reference in a class to a direct reference;
(3) initialization of the class
(1) When the class is initialized
1) Create an instance of the class, that is, the new object
2) Access a static variable of a class or interface, or assign a value to the static variable
3) Calling the static method of the class
4) Reflection (Class.forName ("Com.lyj.load"))
5) Initializes a subclass of a class (the parent class of the child class is initialized first)
6) Startup class indicated at the start of the JVM, which is the same class with the same file name and class name
(2) sequence of initialization of classes
1) If this class has not been loaded and linked, then load and link it first
2) If the class has a direct parent class, and the class has not been initialized (note: In a classloader, the class can only be initialized once), then initialize the immediate parent class (not for the interface)
3) If there is an initialization statement (such as a static variable and a static block) in the join class, then the initialization statements are executed sequentially.
4) In general, the order of initialization is: (static variable, static initialization block) –> (variable, initialization block) –> constructor, if there is a parent class, the order is: Parent class static method –> Subclass static method –> Parent class construction Method-- The subclass constructor method.

Four access modifier public,private,protected, and not write (default) when the difference?

Scope current similar Bun class other

Public√√√√

Protected√√√x

Default√√xx

Private√xxx

Defaults to default when members of a class do not write access adornments. The default is equivalent to exposing (public) to other classes in the same package, and for other classes that are not in the same package are equivalent to private (private). A protected (protected) subclass is equivalent to exposing a class that is not a parent-child relationship in the same package as private.

Five when an object is passed as a parameter to a method, this method can change the properties of the object and return the changed result, is it a value pass or a reference pass?

is a value pass. The Java programming language has only value-passing parameters. When an object instance is passed as a parameter to a method, the value of the parameter is a reference to the object. An object's properties can be changed during the call, but the object's reference is never changed. (   only value passing exists in Java, only the value is passed!!! However, we often see that the passing of objects (arrays, classes, interfaces) seems a bit like reference passing, which can change the value of an attribute in an object. But don't be fooled by this illusion, the value of the incoming function is actually a copy of the object reference, that is, the value of the referenced address is passed, so it is passed by value. )

Six, the difference between String and StringBuilder, StringBuffer (simple answer)?

Comparison of the three in execution speed: StringBuilder > StringBuffer > String. The reason is that string is a string constant, and StringBuffer and StringBuilder are string variables.

On-thread security, StringBuilder threads are unsafe, and stringbuffer is thread-safe.

Seven list Some of your common run-time exceptions?

ArithmeticException (Arithmetic exception)

ClassCastException (class conversion exception)

IllegalArgumentException (illegal parameter exception)

Indexoutofboundsexception (The following table out-of-bounds exception)

NullPointerException (null pointer exception)

SecurityException (Security Exception)

Eight final, finally, finalize the difference?

1.final modifier (keyword). A class that is final modified means that no new subclasses can be derived, and cannot be inherited as a parent class. Therefore, a class cannot be declared by both abstract and final. Declaring variables or methods as final ensures that they are not modified during use. A variable declared as final must give the initial value of the variable at the time of declaration, and only read in subsequent references. The method that is declared by final is also used only and cannot be overloaded.

2.inally is to provide a finally block in case of exception handling to perform any cleanup operations. Whether or not an exception is thrown or captured, the finally block is executed. The contents of the try block are executed to the end when there is no exception. The content in a catch block is a jump to a catch block when the content of the try block is declared by a catch exception. The finally block executes the contents of the finally block regardless of whether the exception occurs, so there is code in the code logic that needs to be executed regardless of what happens, and can be placed in the finally block.

3.finalize is the method name. Java technology allows the use of the Finalize () method to do the necessary cleanup before the garbage collector clears objects from memory. This method is called by the garbage collector to this object when it determines that the object is not referenced. It is defined in the object class, so all classes inherit it. Subclasses override the Finalize () method to organize system resources or to perform other cleanup work. The Finalize () method is called on the object before the object is deleted by the garbage collector.

I hope that you will meet the face of the question to comment out, later I will present here. Learn from each other and progress with each other.

Java Side Question Summary (constantly updated)

Related Article

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.