Java Interview Basics Part collection

Source: Internet
Author: User
Tags event listener server memory

Write in front: This article for Java is already very good big hand, there is no need to read, because for you, this TM is simply too simple ....

We have all experienced the interview, do you really understand the interview? For the interview I just want to say that the purpose of the interview is not to let the examiner know how you are, but to let yourself know how you are. That means what you do and what you can do! For example, if you go to an interview with a Java engineer, you will have at least the function of the job, not just the face test!

The following is more than just an interview question to let you know what you are.


Basic part

1. Three main features of object-oriented

Inheritance, encapsulation, polymorphism

What is inheritance?

① inheritance is one of the important reasons that object-oriented programming can improve the efficiency of software development.

② inheritance is transitive, just as the grandson of reality not only looks like his father but also his grandfather.

The properties and methods inherited by ③ are implicit, which is invisible in this class.

④ A class can have only one parent class, that is, a class can only be single-inheritance.

⑤ an interface can have more than one parent class, that is, an interface can be multiple inheritance.

In the actual project development, one class inherits from another class, then the former is the subclass of the latter, and vice versa.

What is encapsulation?

The object data and the instructions that manipulate the object are part of the object itself, enabling you to hide the data as externally as possible.

In actual project development, the use of the most encapsulated entity classes, often with JavaBean (the class must be concrete and public, and with parameterless constructors) together.

So what do the entity classes have?

A: Private member variables, parameterless constructors, parameterized constructors, setter and getters methods, overriding the ToString method, overriding Hashcode, and the Equals method.

What is polymorphic?

① polymorphism is that objects have many forms: reference polymorphism and Method polymorphism.

② Reference polymorphism: A reference to a parent class can point to an object of this class, a reference to a parent class that can point to a child class.

③ method Polymorphism: When creating this class object, the method called is the method of this class; When you create a subclass object, the method that is called is the method that the subclass overrides or the inherited method.

④ There are conditions for polymorphism: inheritance, rewriting.

The role of ⑤ polymorphism is to eliminate the coupling relationship between types.

In the actual project development, Class A inherits Class B, if the class B method is not rewritten in Class A, the output is still the information in the class B method (b b=new A ()), and if the class B method is overridden in Class A, the output is the information in the Class A method (b b=new A ()).

2.Java Collection Frame Tree

Collection interface: It is a root interface of the Java collection framework and the parent interface of the list, set, and queue interfaces. It also defines the methods that can be used to manipulate list, set, and queue-additions and deletions.

Map interface: It provides a mapping relationship where elements are stored as key-value pairs (key-value). The map interface has an important implementation class HashMap.

① the key-value pair in the map interface exists as an object instance of the entry type.

② in the map interface, the key value (key value) is not repeatable, the value can be repeated, that is, there is a many-to-one relationship.

③ provides a collection of key values, a collection of value values, and methods for entry collections, respectively, in the map interface.

The ④MAP supports generics, and both the key and value values can be null.

⑤ the Entry object in HashMap is unordered, and this attribute is the opposite of the list interface.

⑥ has a mapping that has only one key value of NULL in HashMap. (Note: The key value cannot be duplicated.)

List interface: The list interface has an important implementation class ArrayList (array sequence).

①list is a set of elements that are ordered and can be duplicated.

②list can precisely control where each element is inserted, or delete the position of an element.

Comparator interface: A temporary comparison rule. If a class is to implement this interface, it is necessary to implement its compare () method.

Comparable interface: The default comparison rule. When this interface is implemented, it means that instances of this class can be compared in size and can be naturally sorted. If a class is to implement this interface, it is necessary to implement its CompareTo () method.

Io stream in 3.Java

BYTE stream:

Read and write one byte at a byte

FileInputStream in=new FileInputStream ("source file");

FileOutputStream out=new fileoutputstream ("target file");

......

In.close ();

Out.close ();

The first thing to remember is that once the IO stream is used, it is common sense to always remember to close it.

Efficient reading of bytes using buffers

Bufferedinputstream in=new Bufferedinputstream (New FileInputStream ("source file");

Bufferedoutputstream out=new Bufferedoutputstream (new FileOutputStream ("target document");

......

In.close ();

Out.close ();

Character Stream:

InputStreamReader isr=new InputStreamReader (New FileInputStream ("Source file path"), "set Encoding");

OutputStreamWriter osw=new OutputStreamWriter (New FileOutputStream ("Destination file path"), "set Encoding");

......

Osw.close ();

Isr.close ();

You can also write this:

FileReader fr=new FileReader ("source file path");

FileWriter fw=new FileWriter ("Destination file path");

......

Fr.close ();

Fw.close ();

Efficient reading of characters using buffers

BufferedReader br=new BufferedReader (New Filereade ("Source file path");

PrintWriter pw=new printwriter ("Destination file path");

......

Br.close ();

Pw.close ();

Serialization and deserialization:

Serialization of objects

ObjectOutputStream oos=new ObjectOutputStream (new FileOutputStream (file));

The file here refers to the string file= "the path of the files in the project";

Deserialization of an object

ObjectInputStream ois=new ObjectInputStream (new FileInputStream (file));

4.Java Socket Communication (multi-threaded)

Ideas:

① first creates a server-side socket, specifies and listens for a port, and then loops through the connection to start waiting for the client ....

② creates a client socket, specifies the server address and port, then gets the output stream, sends the request to the server side, and closes the socket output stream.

After the client's request is received by the ③ server, a new thread is created and started.

④ creates a threading class, performs a thread operation, gets the input stream, and the server reads the client user details to close the resource.

⑤ executes the thread operation, gets the output stream, responds to the client request, and the client receives a response to the server, shutting down the resource.

Simply put, it's the equivalent of me talking to you (client → server side), you receive what I say (server → threading Class), brain after thinking (threading Class), make an answer to my words (threading class → Client).

5. Relational database

The three paradigms of the database:

The ① field is not divided.

The ② has a primary key, and the non-primary key field relies on the primary key.

③ Non-primary key fields cannot depend on each other.

T-sql:

In the entire database, query operations occupy 80% of the added and deleted changes, and when it comes to queries, T-SQL statements naturally not less. Shown above.

Increase:

① inserting a single line

INSERT into < table name > (column name) values (column values)

② Adding existing table data to a table that already exists

INSERT into < existing new table > (column name) Select < source table column name > from < source table name >

③ Create a new table directly with the existing table data and populate

Select < new table column name > into < new table name > from < source table name >

By deleting:

① Delete a row that satisfies a condition

Delete from < table name > where < delete condition >

② Delete an entire table

TRUNCATE TABLE < name >

Note: Delete all rows of the table, but the structure, columns, constraints, indexes, etc. of the table are not deleted;

Change:

① Update

Update < table name > set < column name = update value > where < update condition >

Sub-query:

SELECT * from t1 WHERE column1 = (select Column1 from T2);

which

①select * from T1 ... Called an out-of-query.

②select Column1 from T2 is called a subquery.

So, let's just say that subqueries are nested inside of queries. In fact, it is also possible to nest one or more sub-queries inside a subquery. Note here that the subquery must appear between the parentheses Oh.

Javaweb

Javaspring Summary

Jsp

Idea: When a user uses the client browser to send a request to the server to access the Index.jsp page, when the server receives the customer's request, it begins to determine if the resource accessed by the user is the first access?

① If this is the first request, then the Tomcat JSP engine will convert the JSP file into a servlet (a servlet is essentially a Java class), since it is a Java class that needs to be compiled to generate the corresponding bytecode file. Then execute Jspinit this initialization method. Then generate the bytecode file for the JSP page and go to ③.

② directly accesses the bytecode file of the generated JSP page and goes to ③.

③ finally parses the Jspservice method in the Java class, which is designed to handle the user's request.

JSP nine large built-in objects:

Out object: It is an instance of the JspWriter class and is an object commonly used to output content to the client.

Request object: It represents the client's requests for information that is encapsulated in the request object. It is also an instance of the HttpServletRequest class and has a request domain, which is always valid until the client's request is completed.

Common methods:

Request.setcharacterencoding ("UTF-8");//Set the code to solve the Chinese garbled problem.

Request.setattribute ("", "");//The former is the property that needs to be set, the latter is the desired value.

Request.getattribute ();

Request.getrequestdispatcher ("index.jsp"). Forward (request, response);//requests are forwarded to the Index.jsp page, and the request object is always valid.

Response object: It contains information about the response to a customer request and is also an instance of the HttpServletResponse class. Like request, it has the scope of the page, that is, when accessing a page, the page's response object is only valid for this visit, and the execution is not valid.

Common methods:

Response.setcontenttype ("text/html", "charset=" Utf-8 ");//sets the Mimi type of the response.

Response.sendredirect ("index.jsp");//Request redirection, if there are multiple redirects, then the object data is not saved, that is, null.

Differential analysis of request forwarding and request redirection:

    • Request redirection: Client behavior, Response.sendredirect (), is essentially equivalent to two requests, the previous request object is not saved, and the address bar's URL address changes.

    • Request Forwarding: Server behavior, Request.getrequestdispatcher (). Forward (req, resp); is a request, the request object will be saved after forwarding, the Address bar URL address will not change.

Session object: When we first visit the page, the session object is automatically loaded and created. It is an instance object of the HttpSession class.

What is a session? Why do you use a Session object?

    • Session: When a client opens a browser to connect to the server to start, the client closes the browser server to end this process, which we call a session.

    • In the actual situation, the customer in the process of accessing the server, will not be able to switch between several pages of the server, when it is necessary for the service side to know that the current operation of the customer is not operating the previous pages of the customer, that is, it must be clear whether the same person in action. So the concept of Session object is introduced.

Application object: It realizes the sharing of data between users, can hold global variables, it is an instance of the ServletContext class. Its lifecycle starts at the start of the server and terminates at the server's shutdown.

Note:

① the same property of the Application object can be manipulated in the connection between the user's back-and-forth connection or a different user.

② the operation of application object properties anywhere will affect the access of other users to this.

Instructions and actions for JSP:

① Three compile Directives: page, include, Taglib.

② Seven action commands: Jsp:forward, Jsp:param, Jsp:include, Jsp:plugin, Jsp:usebean, Jsp:setproperty, Jsp:getpproperty.

JSP directives:

The include directive differs from the Include Action directive:

The life cycle of the session

The "activity" idea of the session life cycle:

① a new page that is opened by a hyperlink in a session belongs to the same session.

② only the current session page does not close all, reopen the new browser window to access the same project resource when it belongs to the same session.

③ unless all pages of this session are closed and then re-accessed by a JSP or the servlet will create a new session.

Note:

Note that the original session is still there, but the old SessionID still exists on the service side, but no more clients will carry it and then hand it over to the server check.

Note:

① when the Web container is started, the filter is loaded and started.

② instantiation is only instantiated once, meaning that it is used only once in the Web container.

Ideas:

① first determines if the servlet instance exists and jumps into step ③ if it exists.

② initialization phase, the Init initialization method is done after the instance is created.

The ③ response phase, in response to client requests, invokes the service () method. The service () method selectively executes the doget () method or the Dopost () method based on the submission method.

④ Terminate phase, call the Destroy () method to destroy, the server shuts down.

Web Listener

A Web listener is a special class that has a servlet specification definition that listens to client requests and service-side operations, while also listening, including objects such as ServletContext, HttpSession, ServletRequest, and so on.

Classification of listeners

Event Listener for HttpSession

Event Listener for ServletContext

Event Listener for ServletRequest

Note:

These three listening events, respectively, implement the corresponding interface, that is, the diagram of 1. N. Now that the interface is implemented, it is necessary to implement its corresponding method. The main use is also.

Event listeners bound to the state of an object in a httpsession domain

Note:

Why do you use the session passivation?

First of all, under normal circumstances, our session is stored in the server session, but if we have a lot of users, the session is still stored in the server, then the server overhead is very large, will directly affect the use of our web application. Therefore, it is necessary to use the session passivation mechanism, which is the essence of the server is not used by the session object is temporarily serialized into the system files, when it is to be used and then deserialized into the server memory.

Model1 and Model2

Note:

Model1 is one of the earliest development patterns that can embody the layered thought, and simply, Model1 is a kind of jsp+javabean development mode.

Cons: maintainability and scalability are relatively poor.

Ideas:

① first submits a request to the servlet by a JSP page.

② then instantiates the object of a model layer in our control layer servlet or invokes some of the functionality of the model layer.

③ is then accessed by the model layer to read our database layer.

④ when the results are read, the results are returned to our control layer.

After the ⑤ control layer has obtained this result, the user will be presented with different JSP pages based on this result.

There is a need for Java teaching video friends, can add me 756576218 Meng new can also

Java Interview Basics Part collection

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.