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, which 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包括Session Bean、Entity Bean、Message Driven Bean,基于JNDI、RMI、JAT等技术实现。
    SessionBean在J2EE应用程序中被用来完成一些服务器端的业务操作,例如访问数据库、调用其他EJB组件。EntityBean被用来代表应用系统中用到的数据。
    对于客户机,SessionBean是一种非持久性对象,它实现某些在服务器上运行的业务逻辑。
    对于客户机,EntityBean是一种持久性对象,它代表一个存储在持久性存储器中的实体的对象视图,或是一个由现有企业应用程序实现的实体。
    Session Bean 还可以再细分为 Stateful Session Bean 与 Stateless Session Bean ,这两种的 Session Bean都可以将系统逻辑放在 method之中执行,不同的是 Stateful Session Bean 可以记录呼叫者的状态,因此通常来说,一个使用者会有一个相对应的 Stateful Session Bean 的实体。

    9、Collection 和 Collections的区别?
    Collection是集合类的上级接口,继承与他的接口主要有Set 和List.
    Collections是针对集合类的一个帮助类,他提供一系列静态方法实现对各种集合的搜索、排序、线程安全化等操作。

    10、&和&&的区别
    &是位运算符,表示按位与运算,&&是逻辑运算符,表示逻辑与(and)。

    11、HashMap和Hashtable的区别?
    HashMap是Hashtable的轻量级实现(非线程安全的实现),他们都完成了Map接口,主要区别在于HashMap允许空(null)键值(key),由于非线程安全,效率上可能高于Hashtable。
    HashMap允许将null作为一个entry的key或者value,而Hashtable不允许。
    Hashtable继承自Dictionary类,而HashMap是Java1.2引进的Map interface的一个实现。
    的不同是,Hashtable的方法是Synchronize的,而HashMap不是,在多个线程访问Hashtable时,不需要自己为它的方法实现同步,而HashMap 必须为之提供外同步。

    12、final, finally, finalize的区别?
    final 用于声明属性,方法和类,分别表示属性不可变,方法不可覆盖,类不可继承。
    finally是异常处理语句结构的一部分,表示总是执行。
    finalize是Object类的一个方法,在垃圾收集器执行的时候会调用被回收对象的此方法,可以覆盖此方法提供垃圾收集时的其他资源回收,例如关闭文件等。

    13、sleep() 和 wait() 有什么区别? 
    sleep是线程类(Thread)的方法,导致此线程暂停执行指定时间,给执行机会给其他线程,但是监控状态依然保持,到时后会自动恢复。调用sleep不会释放对象锁。
    wait是Object类的方法,对此对象调用wait方法导致本线程放弃对象锁,进入等待此对象的等待锁定池,只有针对此对象发出notify方法(或notifyAll)后本线程才进入对象锁定池准备获得对象锁进入运行状态。

    14、Overload和Override的区别?Overloaded的方法是否可以改变返回值的类型
    方法的重写Overriding和重载Overloading是Java多态性的不同表现。重写Overriding是父类与子类之间多态性的一种表现,重载Overloading是一个类中多态性的一种表现。如果在子类中定义某方法与其父类有相同的名称和参数,我们说该方法被重写 (Overriding)。子类的对象使用这个方法时,将调用子类中的定义,对它而言,父类中的定义如同被“屏蔽”了。如果在一个类中定义了多个同名的方法,它们或有不同的参数个数或有不同的参数类型,则称为方法的重载(Overloading)。Overloaded的方法是可以改变返回值的类型。

    15、error和exception有什么区别?
    error 表示恢复不是不可能但很困难的情况下的一种严重问题。比如说内存溢出。不可能指望程序能处理这样的情况。
    exception 表示一种设计或实现问题。也是说,它表示如果程序运行正常,从不会发生的情况。

    16、同步和异步有何异同,在什么情况下分别使用他们?举例说明。
    如果数据将在线程间共享。例如正在写的数据以后可能被另一个线程读到,或者正在读的数据可能已经被另一个线程写过了,那么这些数据是共享数据,必须进行同步存取。
    当应用程序在对象上调用了一个需要花费很长时间来执行的方法,并且不希望让程序等待方法的返回时,应该使用异步编程,在很多情况下采用异步途径往往更有效率。

    17、heap和stack有什么区别?
    栈是一种线形集合,其添加和删除元素的操作应在同一段完成。栈按照后进先出的方式进行处理。堆是栈的一个组成元素

    18、forward 和redirect的区别?
    forward是服务器请求资源,服务器直接访问目标地址的URL,把那个URL的响应内容读取过来,然后把这些内容再发给浏览器,浏览器根本不知道服务器发送的内容是从哪儿来的,所以它的地址栏中还是原来的地址。
    redirect是服务端根据逻辑,发送一个状态码,告诉浏览器重新去请求那个地址,一般来说浏览器会用刚才请求的所有参数重新请求,所以session,request参数都可以获取。

    19、Static Nested Class 和 Inner Class的不同?
    Static Nested Class是被声明为静态(static)的内部类,它可以不依赖于外部类实例被实例化。而通常的内部类需要在外部类实例化后才能实例化。

    20、JSP中动态INCLUDE与静态INCLUDE的区别?
    动态INCLUDE用jsp:include动作实现 <jsp:include page="included.jsp" flush="true" />它总是会检查所含文件中的变化,适合用于包含动态页面,并且可以带参数。
静态INCLUDE用include伪码实现,定不会检查所含文件的变化,适用于包含静态页面<%@ include file="included.htm" %> 

    21、什么时候用assert?
    assertion(断言)在软件开发中是一种常用的调试方式,很多开发语言中都支持这种机制。在实现中,assertion是在程序中的一条语句,它对一个boolean表达式进行检查,一个正确程序必须保证这个boolean表达式的值为true;如果该值为false,说明程序已经处于不正确的状态下,系统将给出警告或退出。一般来说,assertion用于保证程序最基本、关键的正确性。assertion检查通常在开发和测试时开启。为了提高性能,在软件发布后,assertion检查通常是关闭的。

    22、GC是什么? 为什么要有GC? 
    GC是垃圾收集的意思(Gabage Collection),内存处理是编程人员容易出现问题的地方,忘记或者错误的内存回收会导致程序或系统的不稳定甚至崩溃,Java提供的GC功能可以自动监测对象是否超过作用域从而达到自动回收内存的目的,Java语言没有提供释放已分配内存的显示操作方法。 

    23、short s1 = 1; s1 = s1 + 1;有什么错? short s1 = 1; s1 += 1;有什么错? 
    short s1 = 1; s1 = s1 + 1; (s1+1运算结果是int型,需要强制转换类型)
    short s1 = 1; s1 += 1;(可以正确编译)

    24、Math.round(11.5)等於多少? Math.round(-11.5)等於多少? 
    Math.round(11.5)==12
    Math.round(-11.5)==-11
    round方法返回与参数最接近的长整数,参数加1/2后求其floor.

    25、String s = new String("xyz");创建了几个String Object? 
    两个

Guess you like

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