1. Java Basics

1. What are the characteristics of object-oriented 

    ① Abstraction:
    Abstraction is to ignore those aspects of a topic that are not related to the current goal in order to pay fuller attention to the aspects related to the current goal. Abstraction does not intend to understand the whole problem, but only to select some of them, leaving some details for now. Abstraction includes two aspects, one is process abstraction and the other is data abstraction.
    ② Inheritance:
    Inheritance is a hierarchical model of connecting classes, and allows and encourages the reuse of classes, it provides a way to express commonality clearly. A new class of objects can be derived from an existing class, a process called class inheritance. The new class inherits the characteristics of the original class, the new class is called the derived class (subclass) of the original class, and the original class is called the base class (parent class) of the new class. A derived class can inherit methods and instance variables from its base class, and the class can modify or add new methods to better suit specific needs.
    ③ Encapsulation:
    Encapsulation is to surround the process and data, and access to the data can only be done through a defined interface. Object-oriented computing begins with the fundamental concept that the real world can be represented as a series of fully autonomous, encapsulated objects that access other objects through a protected interface.
    ④ Polymorphism:
    Polymorphism refers to allowing objects of different classes to respond to the same message. Polymorphism includes parametric polymorphism and inclusion polymorphism. Polymorphic language has the advantages of flexibility, abstraction, behavior sharing, and code sharing, and it solves the problem of the same name of application functions.

    2. Is String the most basic data type?
    Basic data types include byte, int, char, long, float, double, boolean and short.
    The java.lang.String class is final, so it cannot be inherited or modified. In order to improve efficiency and save space, we should use StringBuffer class

    3. What is the difference between int and Integer?
    Java provides two different types: reference types and primitive types (or built-in types). Int is a primitive data type of java, and Integer is a wrapper class provided by java for int. 4. The difference between string, stringBuild and stringBuffer in java

   

(1)string

       1. Stirng is an object and not a basic data type 
       . 2. String is a final class and cannot be inherited. An immutable object, once created, its value cannot be modified. 
       3. For an existing Stirng object, modifying its value is to recreate an object, and then assign the new value to the object

(2)stringBuffer

   1, a string buffer similar to String, its modification will not recreate the object like String. 
   2. Use the append() method to modify the value of Stringbuffer, and use the toString() method to convert it to a string. 

        3. StringBuffer is a mutable class, any changes to the string it refers to will not generate a new object, and it is thread-safe .

(3)stringBuild

       It is a class used to replace stringBuffer after jdk1.5, and can replace StringBuffer most of the time. The difference from StringBuffer is that Stringbuild is a single-threaded class, and it is not worth performing thread synchronization, so it is faster and more efficient than StringBuffer. is thread unsafe

    Comparison of the three in terms of execution speed:

StringBuilder > StringBuffer > String


    5. What are the similarities and differences between runtime exceptions and general exceptions
    ? Exceptions represent abnormal states that may occur during program operation, while runtime exceptions represent exceptions that may be encountered in the usual operations of virtual machines, which are common operating errors. The java compiler requires that methods must declare to throw non-runtime exceptions that may occur, but it does not require that methods must declare to throw uncaught runtime exceptions.

    6. Tell the life cycle of Servlet and tell the difference between Servlet and CGI?
    After the servlet is instantiated by the server, the container runs its init method, runs its service method when the request arrives, and the service method automatically dispatches the doXXX methods (doGet, doPost) corresponding to the request, etc. When the server decides to destroy the instance, it calls its destroy method.
    The difference with cgi is that servlet is in the server process, it runs its service method through multi-threading, one instance can serve multiple requests, and its instance is generally not destroyed, while CGI generates a new process for each request, After the service is completed, it is destroyed, so the efficiency is lower than that of servlet.

    7. State the storage performance and characteristics of ArrayList, Vector, and LinkedList.
    Both ArrayList and Vector use arrays to store data. The number of elements in this array is larger than the actual data stored in order to add and insert elements. They all allow elements to be indexed directly by serial number, but Inserting elements involves memory operations such as array element movement, so indexing data is fast and inserting data is slow. Vector usually has worse performance than ArrayList due to the use of the synchronized method (thread safety), while LinkedList uses a doubly linked list for storage, indexing data by serial number. It is necessary to traverse forward or backward, but when inserting data, it is only necessary to record the preceding and following items of this item, so the insertion speed is faster.

    8. What technologies is EJB based on?
    EJB includes Session Bean, Entity Bean, and Message Driven Bean, which are implemented based on technologies such as JNDI, RMI, and JAT.
    SessionBean is used in J2EE applications to complete some server-side business operations, such as accessing databases and calling other EJB components. EntityBean is used to represent the data used in the application system.
    For the client, a SessionBean is a non-persistent object that implements some business logic that runs on the server.
    To the client, an EntityBean is a persistent object that represents an object view of an entity stored in persistent storage, or an entity implemented by an existing enterprise application.
    Session Bean can also be subdivided into Stateful Session Bean and Stateless Session Bean, both of which can implement system logic in methods. The difference is that Stateful Session Bean can record the state of the caller, so it usually comes to Say, a user will have a corresponding Stateful Session Bean entity.

    9. What is the difference between Collection and Collections?
    Collection is the upper-level interface of the collection class. The interfaces that inherit from it mainly include Set and List.
    Collections is a helper class for the collection class. It provides a series of static methods to realize the search, sorting, thread safety and other operations of various collections. . 10. The difference between & and && &&     is a bit operator, which means bitwise AND operation, && is a logical operator, which means logical and (and). 11. What is the difference between HashMap and Hashtable?

   


   
    HashMap is a lightweight implementation of Hashtable (non-thread-safe implementation), and they all complete the Map interface. The main difference is that HashMap allows null (null) key values ​​(key), which may be more efficient than Hashtable due to non-thread-safety.
    HashMap allows null as the key or value of an entry, while Hashtable does not.
    Hashtable inherits from Dictionary class, and HashMap is an implementation of Map interface introduced by Java 1.2.
    The difference is that Hashtable's method is Synchronize, while HashMap is not. When multiple threads access Hashtable, you do not need to synchronize its methods yourself, while HashMap must provide external synchronization for it.   12. What is the difference between final, finally, finalize?     final is used to declare properties, methods and classes, which means that properties are immutable, methods cannot be overridden, and classes cannot be inherited.     finally is part of the exception handling statement structure, which means that it is always executed.     finalize is a method of the Object class. This method of the reclaimed object will be called when the garbage collector is executed. This method can be overridden to provide other resource recycling during garbage collection, such as closing files. 13. What is the difference between sleep() and wait()?      sleep is a method of the thread class (Thread), which causes the thread to suspend execution for a specified time, giving the execution opportunity to other threads, but the monitoring state is still maintained, and it will automatically resume after the time is up. . Calling sleep does not release the object lock.

 




   

    wait is a method of the Object class. Calling the wait method on this object causes the thread to give up the object lock and enter the waiting lock pool waiting for this object. Only after the notify method (or notifyAll) is issued for this object, the thread enters the object lock pool to prepare to acquire The object lock enters the running state.

    14. What is the difference between Overload and Override? Whether an Overloaded method can change the type of the return value
    Overriding Overriding and overloading Overloading are different manifestations of Java polymorphism. Overriding Overriding is a manifestation of polymorphism between parent and child classes, and overloading Overloading is a manifestation of polymorphism in a class. If a method is defined in a subclass with the same name and parameters as its parent class, we say the method is overriding (Overriding). When an object of the subclass uses this method, the definition in the subclass will be called, and for it, the definition in the superclass is "masked". If multiple methods with the same name are defined in a class, and they either have different parameter numbers or different parameter types, it is called method overloading. Overloaded methods can change the type of the return value.

    15. What is the difference between error and exception? An
    error indicates a serious problem where recovery is not impossible but difficult. For example, out of memory. It is impossible to expect a program to handle such a situation.
    exception indicates a design or implementation problem. That is, it represents a situation that would never have happened if the program was running correctly.

    16. What are the similarities and differences between synchronous and asynchronous, and under what circumstances are they used? for example.
    If the data will be shared between threads. For example, the data being written may be read by another thread later, or the data being read may have been written by another thread, then these data are shared data and must be accessed synchronously.
    Asynchronous programming should be used when an application calls a method on an object that takes a long time to execute and does not want the program to wait for the method to return. In many cases, the asynchronous approach is often more efficient.

    17. What is the difference between heap and stack?
    A stack is a linear collection where adding and removing elements should be done in the same segment. The stack is processed in a last-in, first-out manner. The heap is an element of the stack

    18. What is the difference between forward and redirect?
    Forward is the server requesting resources. The server directly accesses the URL of the target address, reads the response content of that URL, and then sends the content to the browser. The browser does not know where the content sent by the server comes from, so Its address bar is still the original address.
    Redirect means that the server sends a status code based on logic to tell the browser to re-request that address. Generally speaking, the browser will re-request with all the parameters just requested, so the session and request parameters can be obtained.

    19. What is the difference between Static Nested Class and Inner Class?
    Static Nested Class is an inner class declared as static (static), which can be instantiated without depending on the outer class instance. The usual inner class needs to be instantiated after the outer class is instantiated.

    20. What is the difference between dynamic INCLUDE and static INCLUDE in JSP?
    Dynamic INCLUDE is implemented with jsp:include action <jsp:include page="included.jsp" flush="true" /> It always checks for changes in included files, suitable for including dynamic pages, and can take parameters.
Static INCLUDE is implemented with include pseudo-code, and will not check the changes of the included files. It is suitable for including static pages <%@ include file="included.htm" %> 

    21. When to use assert?
    Assertion is a common debugging method in software development, and many development languages ​​support this mechanism. In implementation, assertion is a statement in the program that checks a boolean expression. A correct program must ensure that the value of the boolean expression is true; if the value is false, the program is already in an incorrect state , the system will give a warning or exit. Generally speaking, assertion is used to ensure the most basic and critical correctness of the program. Assertion checks are usually turned on during development and testing. To improve performance, assertion checking is usually turned off after the software is released.

    22. What is GC? Why is GC? 
    GC means garbage collection (Gabage Collection). Memory processing is a place where programmers are prone to problems. Forgetting or wrong memory recycling can lead to instability or even crash of the program or system. The GC function provided by Java can automatically monitor whether the object exceeds the scope to achieve the purpose of automatic memory recovery. The Java language does not provide a display operation method for releasing allocated memory. 

    23. short s1 = 1; s1 = s1 + 1; what is wrong? short s1 = 1; s1 += 1; what is wrong? 
    short s1 = 1; s1 = s1 + 1; type, you need to cast the type)
    short s1 = 1; s1 += 1; (can be compiled correctly)

    24. What is Math.round(11.5) equal to? What is Math.round(-11.5) equal to? 
    Math.round(11.5)==12
    Math.round(-11.5)==-11
    The round method returns the long integer closest to the parameter. After adding 1/2 to the parameter, find its floor.

    25, String s = new String(" xyz"); How many String Objects are created? 
    Two

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326443300&siteId=291194637