Java Face question-intermediate (bottom)

Source: Internet
Author: User
Tags finally block message queue modifier response code semaphore

Differences between inner classes and static inner classes

Inner class:

1. Variables and methods within the inner class cannot be declared static.

2. Internal class instantiation: B is the inner class of a, instantiating b:a.b B = new A (). New B ().

3. Internal classes can refer to static or non-static properties and methods of external classes.

Static Inner class:

1. Static internal class properties and methods can be declared as static or non-static.

2. Instantiate the static inner class: B is the static inner class of a, a.b B = new a.b ().

3. Static inner classes can only reference static properties and methods of external classes.

Inner classes--Inner class

Static nested classes--statically nested class

In fact, people do not call static internal classes, just called habits, from the literal is easy to understand.

Internal classes rely on the existence of external classes, while static nested classes can be completely independent, and it's good to understand that.

Variables and methods in non-static inner classes cannot be declared as static reasons

Static types of properties and methods that are present in memory when the class is loaded. Using the static properties and methods of a class, the class must be loaded into the virtual machine. However, non-static inner classes are not loaded with external classes, but only after the instantiation of the class.

We envision a scenario in which the outer class is not instantiated, and if the inner class is not loaded, if the static member or method of the inner class is called, the inner class is not loaded yet, but attempting to create a static member of the inner class in memory creates a conflict. Therefore, non-static inner classes cannot have static member variables or static methods.

The difference between String,stringbuilder,stringbuffer

string literal constant

StringBuffer string variable (thread safe)

StringBuilder string variable (non-thread safe)

Performance typically StringBuilder > StringBuffer > String.

The string is an immutable object, and each time a change to the string type is equivalent to generating a new string object and then pointing the pointer to a new string object, so the performance is the worst and the string is not used to change the content frequently.

StringBuffer is a string variable that, when manipulated, does not generate a new object, but instead makes a direct change to the object, so the performance is good.

StringBuilder, like StringBuffer, is a string variable, but he does not have a synchronized keyword and is not guaranteed to be thread-safe, so performance is best. In the case of single threaded, we recommend the use of StringBuilder.

Generally speaking:

String: A case that applies to a small number of string operations.

StringBuilder: Applies to a single-threaded case where a large number of operations are performed in a character buffer.

StringBuffer: For multi-threaded operation in a character buffer.

Here are some questions:

What is the output of the following code?

String a = "HelloWorld"; String b = "Hello" + "world"; System.out.println ((A = = b));

The output is: True.

The reason is that string is directly added to the strings and is optimized at compile time. That is, Hello+world is optimized for HelloWorld at compile time, so they point to the same object at run time. We can also infer that for the addition of direct strings, string is not necessarily slower than the remaining two.

What is the output of the following code?

String a = "HelloWorld"; String b = "Hello";       String C = b + "World";       System.out.println ((A = = c));

The output is: False.

The reason is that C does not add two strings directly, contains a string reference, and does not do the compile-time optimization. So a, C eventually generated two objects, then his efficiency is low.

Mutual conversions between collections and arrays

Array Variable collection:

Usually we will answer the following code:

List<String> list = Arrays.asList(array);

But this is not a good answer, the combination of the list is arrays inside a static inner class, the class did not implement the Add, remove method, so there is a problem in use.

Can do this:

String array[]= {"hello","world","java","zhiyin"};
List<String> list = new ArrayList<String>(Arrays.asList(array));

Group of Set variables:

String[] array=list.toArray(new String[list.size()]);

What are the aspects of object-oriented features?

    • Abstract: abstraction is the process of constructing classes by summarizing the common features of a class of objects, including both data abstraction and behavioral abstraction. Abstractions focus only on what properties and behaviors the object has, and do not care what the details of those behaviors are.

    • Inheritance : inheritance is the process of creating a new class from an existing class to get inherited information. The class that provides the inherited information is called the parent class, and the class that obtains the inherited information is called a subclass. Inheritance allows for a certain degree of continuity in the changing software system, while inheritance is also an important means to encapsulate the variable factors in the program.

    • Encapsulation: encapsulation is generally considered to be the binding of data and the method of manipulating data, and access to data can only be achieved through defined interfaces. The essence of object-oriented is to portray the real world as a series of completely autonomous and closed objects. The method we write in the class is a encapsulation of the implementation details, and we write a class that encapsulates the data and data operations. It can be said that encapsulation is to hide all the hidden things, only to the outside world to provide the simplest programming interface.

    • Polymorphism : polymorphism means that objects of different subtypes are allowed to respond differently to the same message. The simple thing is to invoke the same method with the same object reference but do something different. Polymorphism is divided into compile-time polymorphism and run-time polymorphism. The method overloads implement compile-time polymorphism, and the method overrides implement runtime polymorphism.

A summary explanation of several states of the thread

    1. Ready (Runnable): The thread is ready to run, not necessarily immediately start execution.

    2. running (Running): The code of the thread that the process is executing.

    3. Waiting (Waiting): The thread is in a blocked state, waiting for the external processing to end.

    4. Sleep (sleeping): The thread is forced to sleep.

    5. I/O blocking (Blocked on I/O): waits for I/O operation to complete.

    6. Synchronous Blocking (Blocked on synchronization): waits for a lock to be acquired.

    7. Death (Dead): the thread completed execution.

What are the basic interfaces within the Java collection class?

    • Collection: represents a set of objects, each of which is its child element.

    • Set: A collection that does not contain duplicate elements.

    • List: A sequence of collection, and can contain repeating elements.

    • Map: You can map a key (key) to an object with a value, and the key cannot be duplicated.

The difference between iterator and Listiterator

    • The iterator can be used to traverse the set and list collection, but Listiterator can only traverse the list.

    • Iterator to a collection can only be forward traversal, listiterator can either forward traversal or back traversal.

    • Listiterator implements the iterator interface and includes other functions such as adding, replacing elements, getting the index of the previous and subsequent elements, and so on.

The difference between enumeration and iterator

    • The collection classes in Java provide a way to return iterator, the main difference between iterators and enumeration (enumerations) is that iterator can delete elements, but enumration cannot.

    • When you use iterator to traverse a collection, you use the iterator remove () method to remove elements from the collection, and the Remove () method of the collection throws the Concurrentmodificationexception exception.

    • The functionality of the enumeration interface and the functionality of the iterator interface are duplicated. Additionally, the Iterator interface adds an optional remove operation and uses a shorter method name. The new implementation should prioritize the use of the iterator interface instead of the enumeration interface.

    • The enumeration is twice times faster than iterator and consumes less memory at the same time. However, iterator is far more secure than enumeration because other threads are not able to modify objects in the collection that is being traversed by iterator.

Explain the HTTP response structure

The composition of the request message:

    • The request method.

    • The URI of the requested resource.

    • The protocol version.

    • The optional Request header field.

    • The content entity.

The composition of the response message:

    • The protocol version.

    • Status code.

    • The reason phrase used to interpret the status code.

    • The optional response header field.

    • The entity body.

Response:

    • Response code: The response of the client is successful. such as: 404 (Cannot find the requested resource), 500 (server internal error), 200 (successful response), etc.

    • Message header: A combination of server and client communication that tells the client how to perform certain operations.

    • Response body: The delivery server responds to what the client wants to display, either by downloading the file or by displaying the interface.

Final, finalize, and finally respective roles

Final is a modifier that can modify variables, methods, and classes. If the final modifier variable, it means that the value of the variable cannot be changed after initialization.

The Finalize method is a method that is called before the object is recycled, giving the object its own last chance of resurrection, but when it is called Finalize is not guaranteed.

Finally is a keyword that is used in conjunction with try and catch for exception handling. The finally block is bound to be executed regardless of whether an exception occurred in the try block.

Tell me about some of the ways you know how to communicate between processes

    • Piping pipe: Pipe is a half-duplex communication mode, the data can only flow in one direction, and can only be used between the processes that have affinity. A process's affinity usually refers to a parent-child process relationship.

    • Advanced Pipeline Popen: When another program is started as a new process in the current program process, it is a sub-process of the current program, which we become the advanced pipeline method.

    • The famous pipe named pipe: The famous pipe is also a half-duplex communication mode, but it allows the communication between unrelated processes.

    • Message Queuing MessageQueue: Message Queuing is a linked list of messages, stored in the kernel and identified by message queue identifiers. Message Queuing overcomes the disadvantages of less signal transmission information, only unformatted byte stream, and limited buffer size.

    • Shared storage sharedmemory: Shared memory is the mapping of memory that can be accessed by other processes, which is created by a process, but can be accessed by multiple processes. Shared memory is the fastest IPC approach and is specifically designed for low-efficiency operation of other interprocess communication modes. It is often used with other communication mechanisms, such as signal two, to achieve synchronization and communication between processes.

    • Semaphore Semaphore: A semaphore is a counter that can be used to control access to shared resources by multiple processes. It is often used as a locking mechanism to prevent a process from accessing the shared resource while other processes are accessing the resource. Therefore, it is primarily used as a means of synchronization between processes and between different threads within the same process.

    • Socket sockets: Socket sockets are also an inter-process communication mechanism, unlike other communication mechanisms, which can be used for different and inter-process communication.

    • Signal sinal: A signal is a more complex form of communication that notifies the receiving process that an event has occurred.

Java Face question-intermediate (bottom)

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.