JAVA interview questions (a)


java scope public private protected not write the difference -friendly?
  • public: can be any class reference.
  • protected: the package can not be used, among other, the current class, descendant class, all the packages to be under the same class
    use.
  • friendly: When not writing, descendant classes and other packages can not be used, the current class, you can use the same package.
  • private: Only the current class can be used.

And & & & distinction.
  • It is the bitwise operators &, and logical operators &&. The two are not comparable.

The difference between the collection and collections.
  • It is the top level interface in java collection container frame. It is mainly inherited list and set two interfaces.
  • collections is a help for the class container class. It offers a range of static methods to achieve a variety of container
    operations to search, sort, thread safety of.
  • Thread-safe: Use the synchronized method will become a collection of safe collection thread synchronization.
class model{
    List list1 = Collections.synchronizedList(new ArrayList());
    List list2 = Collections.synchronizedMap(new Map<String,String>());
    List list3 = Collections.synchronizedSet(new TreeSet<>());
}

String s = new String ( "asd") created a number of objects.
  • We created two. First create asd string object. Then the new string to give it asd.
  • String s = "asd": in this way in the process variable storage area.
  • new String ( "asd"): in this way objects are stored on the heap.

There is no goto keyword in java?
  • java reserved word, there is no use in java. goto statement is often used in conjunction with conditional statements. It may be used to achieve
    a conditional branch, loop configuration, the loop out of function and the like. But in a structured program, generally do not use the goto statement, in order to avoid
    confusion the program flow, so difficult to understand and debug procedures.

3 5%, 3,5% -5% -3, -5% -3 result is how much?
  • Subject to the right of the symbol: 2-22-2

Overload and Override the difference
  • Overload: Overload in the same class, as long as the same method name, the number of arguments, return values ​​are not consistent. Access type can also be changed.
  • Rewrite: Override the class hierarchy use, method name, return value, the number of parameters must be consistent. The type of access will be great parent.

set container elements are not repeated, then what method to achieve?
  • Underlying first set is set to achieve the map, and map the key is not repeated. key value map will not be repeated because the put method will carry out a
    comparison of hashcode, so if the value is consistent then the equals method comparison. This ensures that key worth uniqueness.

The difference between error and exception of
  • error can be restored, but generally refers to very difficult problems, such as stack overflow, memory overflow.
  • exception refers to the abnormal, it is a matter of time to achieve the design or operation.

The main new features of jdk8
  • Lambda expressions and functional interfaces:
lambda表达式也称为闭包。是jdk8中的最大的改变,它允许我们将函数当成参数传递给某个方法,或者把代码
本身当做数据处理:函数式开发。
  • The default interface adds methods and static methods:
default和static
  • Method references:
方法引用使得开发者可以引用现存的方法,java类的构造方法或者实例对象。一般和lambda表达式配合使用。
使得代码简介,紧凑。
构造器引用 Class::new///Class<T>::new
静态方法引用 Class::static_method
某个类的成员方法引用 Class::method 
实例对象的成员方法引用 instance::method
  • Repeat Note:
自从java5引进注解之后,注解使用的越来越多,尤其是在框架中,但是注解不能在一个地方多次使用,
java8打破了这个限制,可以在一个地方重复使用同一个注解,使用 @Repeatable定义重复的注解
  • Better type inference
编译器可以推导出某个参数的数据类型,使得代码更加的简洁
  • Broaden the application scenario annotations
现在注解几乎可以使用在任何元素上。局部变量,接口,超类,实现类等

The difference between abstract class interface
  • The same point: both can not be directly instantiated
  • difference:
1. 抽象类是可以有方法体的,接口中没有,但是java8的新特性中接口可以写static和default关键字
的方法。
2. 抽象类中的成员变量可以随意修饰,接口中只能是public sttaic final类型 
3. 接口中不能含有静态代码块,静态方法。抽象类可以有,新特性之后,接口可以写静态方法。
4. 一个类只能单继承,但是可以多实现。
5. 抽象类可以定义成员变量,接口中的变量都是常量。

Whether the entity class abstract class can inherit?
  • An abstract class can be inherited entity class, but only if the entity class with a clear constructor.

constructor can be override it?
  • No, because the constructor can not be inherited. But it may be overload.

String class can be inherited it?
  • No, the final class is modified

In the try-catch-final in, return statements in the try, then the execution order?
  • The final code will be executed before return.

The difference between static variables and instance variables
  • Static variables are modified static variable, also known as class variables, any object does not belong to a class, create a class no matter how many objects are
    static variables and only one copy in memory.
  • Instance variables must rely on a certain instance, the need to create objects to access through the object, static variables can be implemented so that multiple objects shared
    memory

The difference between ArrayList and the Vector, HashMap and Hashtable difference of.

ArrayList和Vector:

  • Synchronization: ArrayList is not thread-safe, that is not synchronized, Vector is thread-safe, that is synchronized.
  • Data Growth: 50% ArrayList will grow in time to reach the threshold of the original collection, Vector will grow by 100%.

    HashMap和Hashtable:

  • For historical reasons: Hashtable Dictionary is based on class, HashMap is a realization java1.2 introduction of the Map interface.
  • Synchronization: Hashtable is thread-safe, HaspMap thread is unsafe.
  • Value: HashMap can only null values, key or value.


char variables can store a character it? why?
  • Can, java to unicode coding, a char accounted for 16 characters, so put the Chinese is no problem.
    ***

    GC is the What? Why should there be GC?
  • GC is a garbage collector. Unlike java C define a variable manually release the memory. GC is mainly achieved, he can automatically detect an object
    exceeds the scope so as to achieve the purpose of automatic recovery.


The difference between String and StringBuffer and StringBuilder of
  • String length is invariable, and the Buffer Builder is variable.
  • buffer and builder biggest difference is that buffer is thread-safe, builder thread is unsafe. So multithreaded recommended
    buffer, single-threaded use builder.

The difference between final, finally, finalize the
  • final: modifier, the modified class: can not be inherited, modified variables: become constant, modification methods: You can not reload
  • finally: exception is processed finally block to perform any cleanup operations.
  • finalize: the method name, java allowed to finalize method to do the necessary clean-up work before the GC will remove the object from memory. This method is
    called GC determined that the object is not used when part of the Object class, so all classes have the method.

Object-oriented features
  • abstract
  • inherit
  • Package
  • Polymorphism

Basic data types
  • byte char int short long double float boolean

The difference between int and integer
  • int is the basic data types, integer int is package type.

ArrayList and storage properties and characteristics of the Vector and LinkedList
  • ArrayList and Vector are based on an array of implementation, the query faster, but ArrayList is not thread-safe, Vector is thread-
    safe, and secondly, the poor performance of the Vector.
  • LinkedList is based on the list, so the insert, delete, change fast.

The difference between heap and stack

heap heap, stack stack.

  • Stack: fast access, after the CPU registers, the data size of the stack area, and survival are determined, the lack of flexibility. Stack data can be shared
  • Heap: dynamically allocated memory size: GC heap may be periodically charged object is no longer in use. Because the dynamic allocation of memory access slower.

Guess you like

Origin www.cnblogs.com/Ruoqian/p/ms102_1.html