Java face question (1)

Source: Internet
Author: User

1.What are the three main features of object-oriented programming, please briefly describe

(1). Inheritance:

Inheritance is a hierarchical model of a junction class and allows and encourages the reuse of classes, which provides a way to articulate commonalities. A new class of objects can be derived from existing classes, a process known as class inheritance. The new class inherits the attributes of the original class, which is called the derived class (subclass) of the original class, and the original class is called the base class of the new class (The parent Class). Derived classes can inherit methods and instance variables from their base classes, and classes can modify or add new methods to make them more suitable for special needs.

(2). Package:

Encapsulation is the process and data is surrounded, access to data only through the defined interface. Object-oriented computing begins with this basic concept that the real world can be portrayed as a series of fully autonomous, encapsulated objects that access other objects through a protected interface.

(3). Polymorphism:

Polymorphism refers to allowing objects of different classes to respond to the same message. Polymorphism consists of parameterized polymorphism and inclusion polymorphism. Polymorphism language has the advantage of flexibility, abstraction, behavior sharing and code sharing, which solves the problem of application function with the same name.

2.Stringthe difference between the StringBuffer and the

The Java platform provides two classes: string and StringBuffer, which can store and manipulate strings, That is, character data that contains more than one character. This string class provides values that cannot be changed strings and the strings provided by this StringBuffer class are modified when you know that character data is going to change, you can use StringBuffer typically, You can use Stringbuffers to dynamically construct character data

3.say Arraylist,vector, LinkedList's storage performance and features

both ArrayList and vectors store data using arrays, which are larger than the actual stored data in order to add and insert elements, both of which allow the element to be indexed directly by ordinal, but the insertion element involves memory operations such as array element movement. So index data fast and insert data slow, vector because of the use of synchronized thinking method (thread safety), usually performance is worse than ArrayList, and LinkedList use a doubly linked list for storage, index data by ordinal need forward or back traversal, But when inserting data, you only need to record items before and after items, so the insertion speed is faster

4.Collectionthe difference between the collections and the

Collection is the ancestor interface of the collection class, and the main interface for inheriting it is set and list. Collections is a helper class for a collection class that provides a series of static methods for searching, sorting, threading, and so on for various collections.

5.HashMapthe difference between the Hashtable and the

HashMap is a lightweight implementation of Hashtable (non-thread-safe implementation), they all complete the map interface, the main difference is that the HASHMAP allows null (NULL) key value (key), due to non-thread-safe, may be more efficient than the Hashtablejava Chinese station Community portal OC) | G| AX, HashMap allows NULL to be used as a Entryde key or value, and Hashtable does not allow HashMap to remove Hashtable thought methods, Change to Containsvalue and ContainsKey because contains thought method is easy to cause misunderstanding Hashtable inherit from dictionary class, and HashMap is an implementation of Java1.2 introduced map interface.

The biggest difference is that Hashtable way of thinking is synchronize, and HashMap is not, when multiple threads access Hashtable, do not need their own way of thinking to achieve synchronization, and HashMap must provide external synchronization.

The Hash/rehash algorithms used by Hashtable and HashMap are probably the same, so there is no big difference in performance.

6.final, finally, finalizethe 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.

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.

7.Overloadand override the difference. Can the overloaded method change the type of the return value?

Overload (Overload):

(1) method overloading is a means of allowing classes to handle different types of data in a uniform manner. Multiple functions with the same name exist at the same time, with different number/types of parameters. Overloaded overloading is a representation of polymorphism in a class.
(2) Java's method overloading is the ability to create multiple methods in a class that have the same name but have different parameters and different definitions. The method is called polymorphism by the number of different arguments passed to them and by the type of parameter to determine which method to use.
(3) When overloading, the method name is the same, but the parameter type and number are different, the return value type can be the same or different. The return type cannot be used as a distinguishing criterion for overloaded functions.

Rewrite ( Override ):

(1) The polymorphism between the parent class and the child class, redefining the function of the parent class. If you define a method in a subclass that has the same name and arguments as its parent class, we say that the method is overridden (overriding). In Java, subclasses can inherit methods from the parent class without having to rewrite the same method. But sometimes subclasses do not want to inherit the parent class's methods, but want to make some changes, which requires a method of rewriting. Method overrides are also called method overrides.
(2) The method in the Kawai class has the same method name, return type, and parameter table as a method in the parent class, and the new method overwrites the original method. If you need a method from the parent class, you can use the Super keyword, which references the parent class of the current class.
(3) The access adornment permission of a subclass function cannot be less than the parent class's

Rules for overriding methods:
1. The parameter list must be exactly the same as the overridden method, otherwise it cannot be called overridden but overloaded.
2. The returned type must always be the same as the return type of the overridden method, otherwise it cannot be called overriding but overloaded.
3. The restrictions on access modifiers must be greater than the access modifiers of the overridden method (Public>protected>default>private)
4. The overriding method must not throw a new check exception or a more generalized check exception than the overridden method declaration. For example
A method of the parent class states that a check exception ioexception, in overriding this method is not able to throw exception, can only throw IOException subclass exception, can throw non-check exception.


Instead of overloading the rules:
1. Must have a different parameter list;
2. You can have different return types, as long as the parameter list is different;
3. Can have different access modifiers;
4. Different exceptions can be thrown;


The difference between overrides and overloads is that:

Overriding polymorphism works, and calling overloaded methods can greatly reduce the amount of code input, and the same method name can have different functions or return values as long as it passes different parameters inside.

With good rewriting and overloading, you can design a clear and concise class that can be said to be overridden and overloaded in a way that is very unusual in the process of writing code.

The overloaded method is to change the type of the return value.

8.ErrorWhat's the difference with 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.

9.Abstract classWhat's the difference with interface?

1.abstract class represents an inheritance relationship in the Java language, and a class can only use one inheritance relationship at a time. However, a class can implement multiple interface.

2. In abstract class, you can have your own data members, or you can have non-ABSTARCT member methods, and in interface, you can only have static data members that cannot be modified (that is, it must be static final, but Data members are not generally defined in interface, and all member methods are abstract.

The 3.abstract class and interface reflect a different design concept. In fact, abstract class represents the "is-a" relationship, interface represents the "like-a" relationship.

4. Classes that implement abstract classes and interfaces must implement all of these methods. Abstract classes can have non-abstract methods. There is no implementation method in the interface.

5. The variable defined in the interface is the public static final type by default, and must be given its initial value, so the implementation class cannot be redefined or changed.

6. Variables in abstract classes are friendly by default, whose values can be redefined in subclasses or re-assigned.

7. The methods in the interface are public,abstract type by default.

.List, Set, Mapwhether to inherit from collection interface

List,set is inherited from Collection,map not.

One by one .SwtichDoes it work on a byte, can it function on a long, or does it work on a string?

Swtich can only be used in byte short int char

.Charcan I store a Chinese character in a type variable?

Can be defined as a Chinese, because in Java encoding in Unicode, a char accounted for 16 bit, so put a Chinese is no problem .

.There are several ways to implement multithreading, what is it? What is the best way to do this? How are there several implementations of synchronization ?

There are two ways to implement multithreading, namely, inheriting the thread class and implementing the Runnable interface.

The implementation of the Runnable interface is good because the implementation class can implement multiple interfaces and only inherit one class.

There are two implementations of synchronization, namely Synchronized,wait and notify

.Javahow many types of streams are there in? The JDK provides some abstract classes for inheritance for each type of stream, stating which classes they are

Byte stream, character stream. The byte stream inherits from the Inputstream/outputstream, and the character stream inherits from the Inputstreamreader/outputsteamwriter.

Java face question (1)

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.