Interview [JAVA] other basis


1, custom annotation

@target
explains Annotation of the modified target range: constructor, method, field, package , type and so on.
@retention
defines the time length of the reserved Annotation, Source (source file retention), class (class reserved), (active runtime) runtime.
@inherited
certain type is marked inherited. Mark a class with annotations @inherited, then he also has a sub-class of this comment.
@document
modified annotations to generate the javadoc.

2, inner classes

  • Internal divided into categories: members of inner classes, anonymous inner classes, static inner classes, local inner class.
  • In addition to static inner classes, other classes can not have internal static variables or static methods, because the internal part of a class member variable outside the class, the first class to load external load inner classes.
    the reason:
    1. Static variable when the class loader needs to replace the reference symbol and at this time no direct references to objects within the class.
    2. Internal class can not be used directly in the outer class instance does not.
2.1 Why static inner class can have static constants

Because the value of the static constant is determined at compile time it will be stored in the constant pool, and access to the pool constant is the constant need to load the class.

2.2, usage scenarios inner classes
  1. To achieve the effect of a multiple inheritance
  2. Access control, can only be called by an external class

    3, automatic packing unpacking

  3. The basic types and reference conversion between types.
  4. Collections only accept objects.
  5. Note that the value of the cache wrapper class, Float and Double value is not cached value, Integer, and Long cache is -128 to 127 after the object is automatically converted to exceed. Need to use equals When comparing two types of packaging.

    4, String Why is the difference between final, StringBuilder and StringBuffer of

  6. String定义成final类型表示不能被继承,确保不会在子类中改变语义。
    每次对string对象的改变相当于重新生成了一个新的string对象。经常改变的字符串不建议使用String。
  7. StringBuffer是线程安全的, StringBuilder是非线程安全的。

5、transient

  1. 被标记的成员变量不参与序列化过程。
  2. 只能修饰成员变量,不能修饰类和方法。

    6、如何进行序列化

  3. 实现Serializable接口。
  4. 序列化使用输出流进行writeObject。
  5. 反序列化使用输入流进行readObject。

7、如何实现对象克隆

  1. 实现Cloneable接口,并重写clone方法。
  2. 也可通过序列化方式进行深拷贝
  3. 一般实际使用过程中我们只需要拷贝对象的属性,通常使用BeanUtils.copy()
    这种拷贝都是浅拷贝
  4. 几种拷贝对象的性能
    cglib>Spring>apache, 一般不建议使用apache的因为对象转换会出错,Spring的date类型转换也可能会出错。

    8、异常

    8.1、Error

    系统级别的错误,程序不必处理。出了错误之后只能退出运行。

    8.2、Exception
  5. 需要进行捕捉或者程序处理的异常。
  6. Exception分为运行时异常和受检异常
    RuntimeException包括:空指针异常,数组下标越界,classNotFound,类型转换异常等等。
    受检异常指:编译器要求方法必须声明抛出可能发生的受检异常。

    9、Object中的finalize方法

    如果类中重写了finalize方法,当该类对象被回收时,finalize方法有可能会被触发。

tencent.jpg

Guess you like

Origin www.cnblogs.com/clawhub/p/12064462.html