Java27 basis points

1. What are several basic data types JAVA is, how many bytes each occupation.

 

 

2, String class can inherit it, and why

Can not. In Java, as long as the class is defined as final, it can be said that the modified final class is not inherited.

3 difference, String, Stringbuffer, StringBuilder is.

 

 

4, ArrayList and LinkedList What is the difference.

Simple difference: 1.ArrayList achieve a dynamic data structure is an array, LinkedList list based on the data structure. (LinkedList is a doubly linked list, there is also next previous) 2. For random access get and set, ArrayList LinkedList feel better, because LinkedList to move the pointer. 3. For new and delete operations add and remove, LinedList comparative advantage, because ArrayList to move data.

Depth difference: 1. For ArrayList and LinkedList terms, add an element to the end of the list are spent on fixed costs. For ArrayList, the main increase in an internal array, pointing to the added elements, may occasionally lead to re-allocate the array; while LinkedList is concerned, this overhead is a unified, assign an internal Entry object.

2. Inserting or deleting an element in the middle of the ArrayList means that the remaining elements in the list will be moved; and inserting or deleting an element in the middle of LinkedList cost is fixed.

3. LinkedList does not support efficient random access elements.

4. Waste of space ArrayList is mainly reflected in some capacity reserved space at the end of the list list, and space spending LinkedList is reflected in each of its elements need to consume considerable space

5, instantiated class talk order.

Question: For example, the parent class static data, constructors, fields, subclass static data, constructors, fields, when the new time, their order of execution.

Answer: Procedure for instance when the class loader (loading -> Connection -> Initialization). Parent class static variables, the parent class static block of code, sub-class static variable, subclass static block of code, the parent class non-static variable (parent class instance member variables), the parent class constructor, subclasses non-static variable (subclass instance members variable), sub-class constructor.

6, which used the Map class, what's the difference.

Question: for example HashMap is thread-safe for you, what is the use of concurrent Map are and what they are internal principles, such as storage, hashcode, expansion, default capacity. Answer: unsafe, concurrent use ConcurrentHashMap.

7, JAVA8 of ConcurrentHashMap Why give up the sub-lock?

JDK source code and by reason of official documents it seems, they believe abandoned segments of the lock by the following:: Cause 1, add multiple segments lock wasted memory space. 2, the production environment, map when placed in competition with a lock probability is very small, but the segment will cause lock update operations such as long waits. 3, in order to improve the efficiency of GC

Since it abandoned the segment lock, then certainly by the new thread-safe program, we look at how to solve the source code is thread-safe? CAS

After the first find the corresponding list by hash, to see whether it is the first object, and if so, the principle of direct insertion cas, no need to lock, and then if the list is not the first object, directly locked with chain first object, here lock the synchronized addition is, though not as good as the efficiency of ReentrantLock, but to save space, there will always use the first object for the lock until the recalculated map size, such as expansion or operated until the first object.

8, ConcurrentHashMap (JDK1.8) Why use synchronized rather than as ReentranLock such a reentrant lock?

You can tell from the following aspects: first lock granularity lock granularity, and no thicker, the even become finer. Whenever expansion once, ConcurrentHashMap concurrency will be doubled. Hash conflict JDK1.7 in, ConcurrentHashMap from way through the secondary hash of (Segment -> HashEntry) to quickly find the elements looking for. In 1.8 make up put, the performance gap when get through the list adding red-black tree form. JDK1.8 expansion, when ConcurrentHashmap for expansion, expansion of other threads can be whether this linked list (red-black tree), the expansion of the particle size is reduced, improving the efficiency of expansion determined by detecting array node.

Why is synchronized, and not reentrant lock

  1. Reduce memory overhead is assumed reentrant lock to get support synchronization, each node needs to get synchronization support through inheritance AQS. But not every node needs to get synchronization support, only the head of the list node (the root node red-black tree) need to be synchronized, which will undoubtedly bring a huge waste of memory.
  2. JVM support to get the reentrant lock, after all, this level API, subsequent performance optimization space is small. JVM is synchronized directly supported, JVM can make the appropriate optimization measures at runtime: Lock coarsening, eliminating the lock, the lock spin and so on. This allows synchronized performance improvement can be obtained under the premise with the JDK version upgrade without code changes.

9, there is no sequential Map implementation class, and if so, how to ensure that they are ordered.

Hashmap and Hashtable is not ordered. TreeMap and LinkedHashmap are ordered. (Default key ascending the TreeMap, LinkedHashmap default data is inserted sequence) is based on the TreeMap comparator Comparator ordered achieved. LinkedHashmap is inserted in order to achieve data based on the list.

10, the difference between abstract classes and interfaces, what classes can inherit multiple classes, interfaces, multiple interfaces can inherit it, what class can implement multiple interfaces.

Differences: 1, abstract classes and interfaces can not be instantiated directly, if instantiated, abstract class variables must point to implement all the abstract methods subclass object, the interface variable must point class object implement all interface methods. 2, abstract class to be inherited by subclasses, the interface class to be realized. 3, the interface can only be done by method claims, the abstract class can declare methods do, you can do way to achieve 4, variables defined in the interface can only be public static constants, the abstract class variables are common variables. 5, the abstract class abstract method must be implemented by all the quilt class, subclass if the parent class abstract methods can not achieve full, then the subclass can only be an abstract class. Similarly, a time when implementation of the interface, if not all implement an interface method, the class can only be abstract. 6, abstract method can only affirm that can not be achieved. abstract void abc (); can not be written abstract void abc () {}. 7, an abstract class can be no abstract methods 8, if there is a class abstract method, then this class is an abstract class 9 only, the abstract method to be implemented, it can not be static, it can not be private. 10, an interface can inherit an interface, multiple inheritance and interfaces, but only single inheritance class.

Classes can not inherit multiple interfaces class can inherit multiple interfaces class can implement multiple interfaces

11, inheritance and the difference between where the polymerization.

Inheritance refers to a class that inherits another function of a class, and can increase the capacity of its own new features, the most common is the relationship between class and class inheritance or interface with the interface; in Java such relationships by keyword It extends clearly identified.

 

 

polymerization

Polymerization is reflected in whole-part relationship has, at this time between the part and the whole is detachable, they can have their own life cycle; for example, relations with CPU computer company and employee;

 

 

12, IO model which, nio talk about your understanding of the difference between him and bio, aio is valid to talk about reactor models.

Each IO difference:

 

 

What reactor that?

  1. Event-driven
  2. You may process one or more input sources
  3. The multiplexer input event by using the distributed synchronization Service Handle respective Request Handler (one or more) treatment

 

 

13, the principle of reflection, reflecting three ways to create instances of the class is.

//创建Class对象的方式一:(对象.getClass()),获取类中的字节码文件
Class class1 = p1.getClass();

//创建Class对象的方式二:(类.class:需要输入一个明确的类,任意一个类型都有一个静态的class属性)
Class class3 = Person.class;

//创建Class对象的方式三:(forName():传入时只需要以字符串的方式传入即可)
//通过Class类的一个forName(String className)静态方法返回一个Class对象,className必须是全路径名称;
//Class.forName()有异常:ClassNotFoundException
Class class4 = Class.forName("cn.xbmchina.Person");

14, reflection, and the Class.forName ClassLoader difference.

Class.forName(className)方法,内部实际调用的方法是 Class.forName(className,true,classloader); 第2个boolean参数表示类是否需要初始化, Class.forName(className)默认是需要初始化。 一旦初始化,就会触发目标对象的 static块代码执行,static参数也也会被再次初始化。 ClassLoader.loadClass(className)方法,内部实际调用的方法是 ClassLoader.loadClass(className,false); 第2个 boolean参数,表示目标对象是否进行链接,false表示不进行链接,由上面介绍可以, 不进行链接意味着不进行包括初始化等一些列步骤,那么静态块和静态对象就不会得到执行

15、描述动态代理的几种实现方式,分别说出相应的优缺点。

原理区别:

java动态代理是利用反射机制生成一个实现代理接口的匿名类,在调用具体方法前调用InvokeHandler来处理。

而cglib动态代理是利用asm开源包,对代理对象类的class文件加载进来,通过修改其字节码生成子类来处理。

1、如果目标对象实现了接口,默认情况下会采用JDK的动态代理实现AOP 2、如果目标对象实现了接口,可以强制使用CGLIB实现AOP

3、如果目标对象没有实现了接口,必须采用CGLIB库,spring会自动在JDK动态代理和CGLIB之间转换

如何强制使用CGLIB实现AOP? (1)添加CGLIB库,SPRING_HOME/cglib/*.jar (2)在spring配置文件中加入<aop:aspectj-autoproxy proxy-target-class="true"/>

JDK动态代理和CGLIB字节码生成的区别? (1)JDK动态代理只能对实现了接口的类生成代理,而不能针对类 (2)CGLIB是针对类实现代理,主要是对指定的类生成一个子类,覆盖其中的方法 因为是继承,所以该类或方法最好不要声明成final

16、final的用途。

1、被final修饰的类不可以被继承 2、被final修饰的方法不可以被重写 3、被final修饰的变量不可以被改变(切记不可变的是变量的引用而非引用指向对象的内容。) 4、被final修饰的方法,JVM会尝试为之寻求内联,这对于提升Java的效率是非常重要的。因此,假如能确定方法不会被继承,那么尽量将方法定义为final的,具体参见运行期优化技术的方法内联部分 5、被final修饰的常量,在编译阶段会存入调用类的常量池中,具体参见类加载机制最后部分和Java内存区域

17、写出三种单例模式实现 。

1 饿汉式

public class EagerSingleton {
static {
 System.out.println("EagerSingleton 被加载");
}

//私有化构造方法,限制直接构造,只能调用 getInstance() 方法获取单例对象
private EagerSingleton(){} 



private static final EagerSingleton eagerSingleton=new EagerSingleton(); // 私有化静态 final成员,类加载直接生成单例对象,比较占用内存 
public static EagerSingleton getInstance(){ //提供对外的公共api获取单例对象
 return eagerSingleton;
}

}

总结:饿汉式单例的特点:饿汉式在类创建的同时就实例化一个静态对象出来,不管之后会不会使用这个单例,都会占据一定的内存,但是相应的,在第一次调用时速度也会更快,因为其资源已经初始化完成。

2 懒汉式

public class LazySingleton {
static {
 System.out.println("LazySingleton 被加载");
}
private LazySingleton(){} //私有化构造方法,限制直接构造,只能调用 getInstance() 方法获取单例对象
private static LazySingleton lazySingleton=null;//静态域初始化为null,为的是需要时再创建,避免像饿汉式那样占用内存
public static LazySingleton getInstance(){//提供对外的公共api获取单例对象
 if(lazySingleton==null){ 
 synchronized (LazySingleton.class){ //在getInstance中做了两次null检查,确保了只有第一次调用单例的时候才会做同步,这样也是线程安全的,同时避免了每次都同步的性能损耗
 if(lazySingleton==null){
 lazySingleton = new LazySingleton();
 }
 }
 }
 return lazySingleton;
 }
}

总结:有同步锁的性能消耗

3 静态内部类实现

public class IoDHSingleton {
static {
System.out.println("IoDHSingleton 被加载");
}
private IoDHSingleton(){} //私有化构造方法,限制直接构造,只能调用 getInstance() 方法获取单例对象
public static IoDHSingleton getInstance(){//提供对外的公共api获取单例对象
//当getInstance方法第一次被调用的时候,它第一次读取HolderClass.ioDHSingleton,内部类HolderClass类得到初始化;
//而这个类在装载并被初始化的时候,会初始化它的静态域,从而创ioDHSingleton 的实例,由于是静态的域,因此只会在虚拟机装载类的时候初始化一次,并由虚拟机来保证它的线程安全性。
 return HolderClass.ioDHSingleton; 
}
 private static class HolderClass{
 static {
 System.out.println("HolderClass 被加载");
 }
 private static IoDHSingleton ioDHSingleton = new IoDHSingleton();
 }
 // 防止反序列化获取多个对象的漏洞 
 private Object readResolve() throws ObjectStreamException { 
 return HolderClass.ioDHSingleton; 
 } 
}

这个模式的优势在于,getInstance方法并没有被同步,并且只是执行一个域的访问,因此延迟初始化并没有增加任何访问成本。

考虑反射: 由于在调用 SingletonHolder.instance 的时候,才会对单例进行初始化,而且通过反射,是不能从外部类获取内部类的属性的。 所以这种形式,很好的避免了反射入侵。 考虑多线程: 由于静态内部类的特性,只有在其被第一次引用的时候才会被加载,所以可以保证其线程安全性。 总结: 优势:兼顾了懒汉模式的内存优化(使用时才初始化)以及饿汉模式的安全性(不会被反射入侵)。 劣势:需要两个类去做到这一点,虽然不会创建静态内部类的对象,但是其 Class 对象还是会被创建,而且是属于永久带的对象。

18、如何在父类中为子类自动完成所有的hashcode和equals实现?这么做有何优劣。

19、请结合OO设计理念,谈谈访问修饰符public、private、protected、default在应用设计中的作用。

访问修饰符,主要标示修饰块的作用域,方便隔离防护。

public: Java语言中访问限制最宽的修饰符,一般称之为“公共的”。被其修饰的类、属性以及方法不仅可以跨类访问,而且允许跨包(package)访问。

private: Java语言中对访问权限限制的最窄的修饰符,一般称之为“私有的”。被其修饰的类、属性以及方法只能被该类的对象访问,其子类不能访问,更不能允许跨包访问。

protect: 介于public 和 private 之间的一种访问修饰符,一般称之为“保护形”。被其修饰的类、属性以及方法只能被类本身的方法及子类访问,即使子类在不同的包中也可以访问。

default:即不加任何访问修饰符,通常称为“默认访问模式“。该模式下,只允许在同一个包中进行访问。

20、深拷贝和浅拷贝区别。

浅拷贝(Shallow Copy):

①对于数据类型是基本数据类型的成员变量,浅拷贝会直接进行值传递,也就是将该属性值复制一份给新的对象。因为是两份不同的数据,所以对其中一个对象的该成员变量值进行修改,不会影响另一个对象拷贝得到的数据。 ②对于数据类型是引用数据类型的成员变量,比如说成员变量是某个数组、某个类的对象等,那么浅拷贝会进行引用传递,也就是只是将该成员变量的引用值(内存地址)复制一份给新的对象。因为实际上两个对象的该成员变量都指向同一个实例。在这种情况下,在一个对象中修改该成员变量会影响到另一个对象的该成员变量值。

深拷贝:

首先介绍对象图的概念。设想一下,一个类有一个对象,其成员变量中又有一个对象,该对象指向另一个对象,另一个对象又指向另一个对象,直到一个确定的实例。这就形成了对象图。那么,对于深拷贝来说,不仅要复制对象的所有基本数据类型的成员变量值,还要为所有引用数据类型的成员变量申请存储空间,并复制每个引用数据类型成员变量所引用的对象,直到该对象可达的所有对象。也就是说,对象进行深拷贝要对整个对象图进行拷贝!

简单地说,深拷贝对引用数据类型的成员变量的对象图中所有的对象都开辟了内存空间;而浅拷贝只是传递地址指向,新的对象并没有对引用数据类型创建内存空间。

21、数组和链表数据结构描述,各自的时间复杂度。

数组和链表的区别: 1、从逻辑结构角度来看: 数组必须事先定义固定的长度(元素个数),不能适应数据动态地增减的情况。当数据增加时,可能超出原先定义的元素个数;当数据减少时,造成内存浪费。 链表动态地进行存储分配,可以适应数据动态地增减的情况,且可以方便地插入、删除数据项。(数组中插入、删除数据项时,需要移动其它数据项) 2、数组元素在栈区,链表元素在堆区; 3、从内存存储角度来看: (静态)数组从栈中分配空间, 对于程序员方便快速,但自由度小。 链表从堆中分配空间, 自由度大但申请管理比较麻烦。 数组利用下标定位,时间复杂度为O(1),链表定位元素时间复杂度O(n); 数组插入或删除元素的时间复杂度O(n),链表的时间复杂度O(1)。

22、error和exception的区别,CheckedException,RuntimeException的区别。

 

 

23、在自己的代码中,如果创建一个java.lang.String类,这个类是否可以被类加载器加载?为什么。

 

 

加载过程中会先检查类是否被已加载,检查顺序是自底向上,从Custom ClassLoader到BootStrap ClassLoader逐层检查,只要某个classloader已加载就视为已加载此类,保证此类只所有ClassLoader加载一次。而加载的顺序是自顶向下,也就是说当发现这个类没有的时候会先去让自己的父类去加载,父类没有再让儿子去加载,那么在这个例子中我们自己写的String应该是被Bootstrap ClassLoader加载了,所以App ClassLoader就不会再去加载我们写的String类了,导致我们写的String类是没有被加载的。

24、说一说你对java.lang.Object对象中hashCode和equals方法的理解。在什么场景下需要重新实现这两个方法。

对于equals()与hashcode(),比较通用的规则: ①两个obj,如果equals()相等,hashCode()一定相等 ②两个obj,如果hashCode()相等,equals()不一定相等

25、在jdk1.5中,引入了泛型,泛型的存在是用来解决什么问题。

面向对象的转型只会发生在具有继承关系的父子类中(接口也是继承的一种) 向上转型:其核心目的在于参数的统一上,根本不需要强制类型转换。 向下转型:是为了操作子类定义的特殊功能,需要强制类型转换,可是现在存在的问题是:向下转型其实是一种非常不安全的操作,以为编译的时候,程序不会报错,而在运行的时候会报错,这就是传说中的—迷之报错。

不过呢,在JDK1.5之后,新增加了泛型的技术,这就将上述向下转型的问题消灭在了萌芽之中。 泛型的核心意义在于:类在进行定义的时候可以使用一个标记,此标记就表示类中属性或者方法以及参数的类型,标记在使用的时候,才会去动态的设置类型。

26、Java中的HashSet内部是如何工作的。

HashSet 的内部采用 HashMap来实现。由于 Map 需要 key 和 value,所以HashSet中所有 key 的都有一个默认 value。类似于 HashMap,HashSet 不允许重复的 key,只允许有一个null key,意思就是 HashSet 中只允许存储一个 null 对象。

27、什么是序列化,怎么序列化,为什么序列化,反序列化会遇到什么问题,如何解决。

什么是序列化? 序列化:把对象转换为字节序列的过程称为对象的序列化。 反序列化:把字节序列恢复为对象的过程称为对象的反序列化

什么情况下需要序列化? 当你想把的内存中的对象状态保存到一个文件中或者数据库中时候; 当你想用套接字在网络上传送对象的时候; 当你想通过RMI传输对象的时候;

如何实现序列化? 实现Serializable接口即可

注意事项: transient 修饰的属性,是不会被序列化的 静态static的属性,他不序列化。 实现这个Serializable 接口的时候,一定要给这个 serialVersionUID 赋值

关于 serialVersionUID 的描述: 序列化运行时使用一个称为 serialVersionUID 的版本号与每个可序列化类相关联,该序列号在反序列化过程中用于验证序列化对象的发送者和接收者是否为该对象加载了与序列化兼容的类。如果接收者加载的该对象的类的 serialVersionUID 与对应的发送者的类的版本号不同,则反序列化将会导致 InvalidClassException。可序列化类可以通过声明名为 “serialVersionUID” 的字段(该字段必须是静态 (static)、最终 (final) 的 long 型字段)显式声明其自己的 serialVersionUID

Guess you like

Origin www.cnblogs.com/zeenzhou/p/11907854.html