JAVA Basics (Interview required)

Source: Internet
Author: User
Tags modifiers thread class throw exception

1. What are the three main features of object-oriented programming?

(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. The difference between String and StringBuffer

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 storage performance and characteristics

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 the index data is fast and the data is inserted slowly. Vector because of the use of synchronized thinking method (thread safety), usually performance is worse than ArrayList, and LinkedList using a doubly linked list for storage, index data by ordinal need to 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. The difference between Collection and collections

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. The difference between HashMap and Hashtable

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, 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.

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, the difference between overload and override. 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.

Override (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. 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.

9. What is the difference between abstract class and 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.

10, List, Set, whether the map inherits from collection interface

List,set is inherited from Collection,map not.

11, whether the Swtich can function on a byte, whether it can function on a long, whether it can function on a string?

Swtich can only be used in byte short int char

12. Can I store a Chinese character in char type?

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.

13, multi-threaded there are several ways to achieve, what is it? What is the best way to do this? Synchronization has several implementation methods, what is it?

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

14. Are there several types of streams in Java? 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.

15. What is the difference between a character stream and a byte stream in Java I/O?

Byte stream in the operation of the time itself is not used in the buffer (memory), and the file itself is directly manipulated, and the character stream is used in the operation of the buffer
Byte stream when manipulating a file, the file can output even if the resource is not closed (the Close method), but if the character stream does not use the Close method, nothing is output, the character stream is buffered, and the flush method can be used to force the flush buffer. Then you can output the content without close.
When you save files on all your hard disks or transfer them in a byte-by-byte manner, including the picture is done in bytes, and the characters are only formed in memory, so the use of bytes is the most.

16. The life cycle of the servlet

1, initialization phase
Call the init () method
2, responding to customer request phase
Call the service () method
3, termination phase
Call the Destroy () method

17, the HTTP protocol is a long connection, or a short connection?

The HTTP protocol is a stateless connection, so it is a short connection.

18. What are the basic data types of Java? is string not a basic type?

Boolean,int,byte,char,short,long,float,double,void,string is not a basic type.

19. What are the different redirection methods in JSP?

<jsp:forward page= "url"/>

<jsp:include file= "url"/>

The former redirects the current page to the page of the specified URL, which is the page that contains the specified URL in the current page.

20. What are the built-in objects of JSP? What are the roles?

Request object Type Javax.servlet.ServletRequest Scope request
Response Response Object Type Javax.servlet.SrvletResponse scope Page
PageContext page Context object type Javax.servlet.jsp.PageContext Scope page
Session object type Javax.servlet.http.HttpSession scope session
Application Application Object type Javax.servlet.ServletContext scope application
Out output Object type Javax.servlet.jsp.JspWriter scope Page
Config configuration object Type Javax.servlet.ServletConfig scope Page
Page object Type Javax.lang.Object Scope page
Exception exception Object type javax.lang.Throwable Scope page

1. The Request object represents requests from the client, such as the information we fill out in the form form, and is the most commonly used object
Common methods are: GetParameter, getparameternames, and Getparametervalues call these methods to get the values of the parameters contained in the Request object.

2. The Response object represents the response to the client, which means that the data sent to the client can be organized through the response object. However, due to the organization of the lower level, it is not recommended for ordinary readers, need to send text to the client directly using

3, PageContext object literal translation can be called "page Context" object, which represents a number of properties of the current page run
Common methods are: Findattribute, GetAttribute, Getattributesscope and Getattributenamesinscope
In general, the PageContext object is not much used, only in the case of the situation of the project is more complex, will use the page properties to assist processing.

4, Session object on behalf of the server and client sessions, when the need to keep the customer information in different JSP pages to use, such as online shopping, customer track tracking and so on. The "session" object is based on a cookie and should be used with care to determine if the client has opened the cookie. Common methods include GetID, GetValue, GetValueNames and Putvalue.

Profile
HTTP is a stateless (stateless) protocol;
The Web server has no historical memory for each client request;
Session is used to save client state information;
Written by the Web server;
stored in the client;
Each access to the client passes the last session record to the Web Server;
The Web server reads the client-submitted session to get the status information for the client

5, Application object is responsible for providing some global information when the application runs in the server, commonly used methods are getmimetype and Getrealpath, etc.

6. The Out object represents the object that sends the data to the client, and unlike the "response" object, the content sent through the "out" object will be the content that the browser needs to display, is the text level, and can be written directly to the client by the "out" object to generate the HTML file dynamically by the program. Common methods include clear, Clearbuffer, flush, getbuffersize, and getremaining, in addition to Pirnt and println, because a buffer is contained inside the "Out" object. So you need some way to manipulate the buffers.

7, the Config object provides some configuration information, the commonly used methods have Getinitparameter and getinitparameternames, in order to obtain the servlet initialization parameters.

8. The Page object represents a running class object produced by a JSP file and is not recommended for general readers.

9. The exception object represents the exception object that is generated when the JSP file is run, which cannot be used directly in a generic JSP file, but only in a JSP file that uses "<%@ page iserrorpage=" true "%>".

JAVA Basics (Interview required)

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.