Technical questions related to interviewing---Java basics

Source: Internet
Author: User

Recently in preparation for the fall school recruit, some common technical issues to make a summary! Hope to help everyone!

1. What is the difference between object-oriented and process-oriented?

Object-oriented is the decomposition of the transaction constituting the problem into individual objects, the object is not to complete a step, but to describe something in the whole problem-solving steps of the behavior. (Description of behavior)

Process-oriented is to analyze the steps required to solve the problem, and then use the function to implement these steps step-by-step, using a one-time call. (Description of steps)

Four basic features of 2.Java (abstraction, encapsulation, inheritance, polymorphism)

Abstraction is the extraction of common and essential features from many things, while discarding their non-essential features.

encapsulation, which is to hide the properties and implementation details of the object, only exposes the interface, controls the access level of the Read and modify properties of the program, and combines the data and the behavior (or function) of the abstraction to form an organic whole, that is, to combine the data with the source code of the operational data to form a "class", Where the data and functions are members of the class.

Inheritance is the derivation of new classes from existing classes, which can absorb data properties and behaviors of existing classes, and can extend new capabilities.

Polymorphic definition: means that objects of different classes are allowed to respond to the same message. That is, the same message can be used in a variety of different ways depending on the sending object. (Sending a message is a function call)

The technique of implementing polymorphism is called dynamic binding, which is the actual type of the referenced object that is judged during execution, and its corresponding method is called according to its actual type.

There are two forms of polymorphism: overloading (with the same method name, different parameters), and overwriting (which occurs in a subclass to override the behavior of the parent class method)

The difference between 3.Overload and override

Overload: Overloading, Method overloading in Java refers to the ability to define multiple functions that have the same method name and different parameters in a class (or in an interface). When overloaded, the method name is the same, but the parameter type and number are different, and the return value type can be the same or different.

The return type cannot be used as a distinguishing criterion for overloaded functions.

Override: Overrides (Overrides), the polymorphism between the parent class and the child class, redefining the function of the parent class. Typically occurs in subclasses that extend the behavior of subclasses (subclasses do not want to inherit the parent class's methods intact, overriding the parent class method to implement their own unique behavior).

4. Whether the constructor constructor can be override

Constructors in Java cannot be inherited by subclasses, so of course it cannot be overridden, but constructors of classes can be overload, and we will see a class with several different constructors.

5. Access control Public,protected,private, and the default difference

Default: The other classes in the same package are equivalent to public. Other classes that are not in the same package are equivalent to private.

Protected: The class itself, any class in the same package, subclasses are accessible

6. Can I inherit the String class

No, the string class is final and cannot be inherited.

 Public Final class implements java.io.Serializable, Comparable<string>, charsequence

7. The difference between string and StringBuffer, StringBuilder

string constant, length immutable.

StringBuffer: String variable (Synchronized, thread safe). If you want to modify the string content frequently, it is best to use stringbuffer for efficiency reasons, and if you want to turn to string type, you can call the ToString () method of StringBuffer.

StringBuilder: String variable (non-thread safe). Internally, the StringBuilder object is treated as a variable-length array that contains a sequence of characters.

The main performance difference between string types and StringBuffer: string is an immutable object, so each time a string is changed, a new string object is generated and the pointer is pointed to the new string object. Therefore, it is best not to use string to change the content of the strings, because each generation of the object will have an impact on the system performance, especially when there is no reference object in memory, the JVM's GC will start to work, and will be degraded.

When using the StringBuffer class , the StringBuffer object itself is manipulated each time, instead of generating new objects and changing the object references. So in most cases it is recommended to use StringBuffer, especially if the string object is often changed .

StringBuilder is generally used inside the method to complete a similar "+" function, because the thread is unsafe, so after use can be discarded. StringBuffer are mainly used in global variables.

Performance: Stringbuilder>stringbuffer >string

8. Relationship between the hashcode and the Equals method

In general, when overriding the Equals method, rewrite the Hashcode method in order to follow a convention: two equal (equal) objects in the sense of equals should have equal hash code (but not say hash code equals equals). These conventions are the needs of some practice, the Java language specification does not require that equals and hashcode two methods must have what relationship or meet what conditions.

9. The difference between abstract classes and interfaces

Abstract classes exist for inheritance, and if you define an abstract class and do not inherit it, you create this abstract class in vain, because you cannot use it to do anything. For a parent class, if one of its methods is implemented in the parent class without any meaning, it must be implemented differently according to the actual needs of the subclass, then the method can be declared as an abstract method, and this class becomes the abstract class.

A class that contains an abstract method is called an abstract class, but it does not mean that there can be only abstract methods in an abstract class, which, like normal classes, can also have member variables and ordinary member methods. Note that there are three main differences between abstract and ordinary classes:

1) The abstract method must be public or protected (because if you are private, you cannot inherit from the quilt class, the subclass cannot implement the method), and by default it is public.

2) Abstract classes cannot be used to create objects;

3) If a class inherits from an abstract class, the child class must implement the abstract method of the parent class. If the subclass does not implement an abstract method of the parent class, the subclass must also be defined as an abstract class.

In other respects, there is no difference between an abstract class and an ordinary class.

An interface refers to a method or function that is called by someone else, and it is an abstraction of the behavior.

The interface can contain variables and methods. Note, however, that the variables in the interface are implicitly specified as public static final variables (and can only be public static final variables, which are reported as compilation errors with private adornments), and methods are implicitly specified as public Abstract method and can only be public abstract method (with other keywords, such as private, protected, static, final and so on will be reported compile errors), and the interface of all methods can not have a concrete implementation, that is to say, The methods in the interface must all be abstract methods.

From here we can see the difference between interface and abstract class, interface is an extremely abstract type, it is more "abstract" than abstract class, and generally does not define variables in the interface.

1) Abstract classes can provide implementation details of member methods, and only public abstract methods exist in interfaces;

2) member variables in an abstract class can be of various types, whereas member variables in an interface are only public static final types;

3) The interface cannot contain static code blocks and static methods, while abstract classes can have static code blocks and static methods;

4) A class can inherit only one abstract class, while a class may implement multiple interfaces.

5) Abstract class is an abstraction of a thing, that is, an abstraction of a class, and an interface is an abstraction of a behavior.

6) abstract class as a parent class of many subclasses, it is a template-like design. And the interface is a kind of behavior specification, it is a kind of radiant design.

10. Automatic box packing and unpacking

Simply put, boxing is the automatic conversion of the base data type to the wrapper type; unpacking is the automatic conversion of the wrapper type to the base data type.

Technical questions related to interviewing---Java basics

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.