Analysis and summary of java android interview questions

There are many references in this article, thank you!

http://www.blogjava.net/fanyingjie/archive/2007/06/27/126467.aspx

http://baike.baidu.com/view/1788559.htm

http://honda418.iteye.com/blog/315893

http://jeff-tang.blog.163.com/blog/static/141686909201022010522906/

http://www.cnblogs.com/mgod/archive/2007/08/05/844011.html

Books referenced

" In-depth understanding of the Java virtual machine : JVM advanced features and best practices"

12. The difference between ArrayList, Vector, LinkedList

 

  ArrayList Vector LinkedList
Implementation principle array array Doubly linked list
thread safety no Yes no
advantage 1. Array implementation is better than traversal
2. Non-thread safe, high efficiency
1. Array implementation is better than traversal
2. Thread safety
1. The addition and deletion of nodes does not require the reconstruction of objects
2. There is no waste of space utilization
shortcoming 1. Not thread-safe
2. Unused elements in the array are a waste of space
3. Expansion may cause object reconstruction
4. Additions and deletions may cause array elements to move
1. Unused elements in the array cause a waste of space
2. Expansion may cause object reconstruction
3. Thread safety, relatively low efficiency
4. Additions and deletions may cause movement of array elements
1. Less efficient traversal
2. Non-thread safe
Expansion 0.5 times increments 1x increment Add or delete as needed
scenes to be used 1. No thread requirements.
2. More traversal, fewer additions and deletions
1. There are thread safety requirements
2. There are many traversal scenarios, and fewer additions and deletions
When many scenes are added or deleted

11. The difference between int and Integer

 

  int Integer
Types of basic type composite type
Defaults 0 null
storage stack (local variables)
heap (member variables, to be further confirmed)
On the heap (can only be created with new)
method Basic types have no methods have
speed Fast (operations on the stack are relatively fast) slow
Generics support No (generics are not supported in java, templates are supported in C++) support
Container class support No (direct use usually does boxing) support
existential meaning 1. Historical reasons (extended in C/C++)
2. Convenient and fast (no need for new)
The wrapper class of the basic type int
provides support for generics, container classes

9. Put String objects into a List<Integer> container

Some knowledge to know

  • Generics in Java are pseudo-generics. (See Zhou Zhiming's "In -depth Understanding of Java Virtual Machine : JVM Advanced Features and Best Practices")
    • Generics are syntactic sugar for the compiler and do not yield the actual type List<Integer>.
    • Both List<Integer> and List<String> are used as List<Object> types after compilation.
    • In the Java Virtual Machine, there is no concept of generics.
  • Some mandatory checks can be postponed to runtime through reflection.
    • If you insert a String object directly into List<Integer>, you will definitely get the compiler's check and prompt an error.
    • The reflection mechanism can delay this kind of error checking until runtime. (This is a disadvantage of reflection, and it will delay the problem that can be checked at compile time until it is discovered at runtime)
  • 由于List<Integer>对象在运行期,本身就是以List<Object>的形式存在,故其在运行期不会产生错误。
  • 反射机制,通过编译期的检查骗过了我们。而我们又可以通过反射骗过了编译器的检查。
实现代码
  1. packagecom.jue.test;
  2. importjava.lang.reflect.InvocationTargetException;
  3. importjava.lang.reflect.Method;
  4. importjava.util.ArrayList;
  5. importjava.util.List;
  6. publicclassTestMain{
  7. List<Integer>mIntList=newArrayList<Integer>();
  8. publicstaticvoidmain(String[]args)throwsSecurityException,
  9. NoSuchFieldException,IllegalArgumentException,
  10. IllegalAccessException,InvocationTargetException,
  11. NoSuchMethodException{
  12. TestMaintm=newTestMain();
  13. MethodaddMethod=List.class.getMethod("add",
  14. newClass[]{Object.class});
  15. //使用反射,我们避免了编译期的强制检查
  16. addMethod.invoke(tm.mIntList,newObject[]{newString("abc")});
  17. addMethod.invoke(tm.mIntList,newObject[]{newString("123")});
  18. addMethod.invoke(tm.mIntList,newObject[]{newString("cde")});
  19. addMethod.invoke(tm.mIntList,newObject[]{newString("fgh")});
  20. for(Objecto:tm.mIntList){
  21. System.out.println(o);
  22. }
  23. }
  24. }

输出结果:

abc
123
cde
fgh

10.List<Integer>与List<String>在编译后都是List<Object>形式存在

  1. packagecom.jue.test;
  2. importjava.util.List;
  3. publicclassTestMain{
  4. publicvoidtestList(List<Integer>list){
  5. }
  6. publicvoidtestList(List<String>list){
  7. }
  8. }

结果:编译失败!

分析:

  • 如上所述,Java的泛型是编译器的语法糖,在编译后,统一使用List<Object>代替。
  • 对于重载,方面名相同,而签名不同,由于参数都将是List<Object>,故编译失败,因为不可能产生签名一样的两个方法。

1.short转换相关的

其一,

  1. packagecom.jue.test;
  2. publicclassTestMain{
  3. publicstaticvoidmain(Stringargs[]){
  4. shorts1=1;
  5. s1=s1+1;
  6. }
  7. }

编译结果

DescriptionResourcePathLocationType
Type mismatch: cannot convert from int to short TestMain.java/TestShort/src/com/jue/testline 6Java Problem

分析:

s1+1会自动转换成int类型,导致s1= s1+1;损失精度。

其二,

  1. packagecom.jue.test;
  2. publicclassTestMain{
  3. publicstaticvoidmain(Stringargs[]){
  4. shorts2=2;
  5. s2+=2;
  6. }
  7. }

编译结果:成功

分析:

反编译之后

  1. packagecom.jue.test;
  2. publicclassTestMain
  3. {
  4. publicstaticvoidmain(String[]args)
  5. {
  6. shorts2=2;
  7. s2=(short)(s2+2);
  8. }
  9. }

故猜测:这可能是java编译器的语法糖。

2.RuntimeException与普通异常,error的区别。

Checked Exception:在编译时就能够被Java编译器所检测到的。

UncheckedException:则是编译时,java编译器不能检查到。

 

  RuntimeException 普通Exception Error
受控异常
产生原因 开发者的编程错误 由于外界环境所限,
本身潜在的一些问题
Java运行时的系统错误,资源耗尽,是一种严重的,
程序无法修复的问题
例子 NullPointerException
ArrayOutOfIndexException
ClassCastException
ArithmeticException
UnsupportedOperationException
ClassNotFoundException
IOException
FileNotFoundException
VirtualMachineError
StackOverflowError
OutOfMemoryError

 

3.finally的一个面试题

  1. packagecom.jue.test;
  2. importjava.util.ArrayList;
  3. importjava.util.List;
  4. publicclassTestMain{
  5. publicstaticvoidmain(String[]args){
  6. test();
  7. }
  8. privatestaticvoidtest(){
  9. Listlist=newArrayList();
  10. try{
  11. System.out.println("return!!");
  12. return;
  13. }catch(Exceptione){
  14. System.out.println("catchException!!");
  15. }finally{
  16. System.out.println("finally!!");
  17. }
  18. }
  19. }


结果:

return!!
finally!!

分析:即便在try中return;finally总会被执行的意义不变,仍然会执行。

 

4.final,finalize,finally的区别

final:关键字,表不变

修饰:

  • 方法:方法不可Override
  • 类:不可被继承
  • 基本类型量:常量,值不可变
  • 符合类型量:引用不可变,即引用的值不可变
  1. finalObjecto1=newObject();
  2. o1=newObject();

finally:关键字,Java异常处理机制的一部分,在异常发生时,用来提供一个必要的清理的机会。

finalize:Object类的方法(参考自百度百科

意义:Java技术允许使用finalize()方法在垃圾回收器将对象回收之前,做一些必要的清理操作。

调用前提:这个对象确定没有被引用到。

工作原理:

  • 垃圾收集器准备好释放对象占用的空间。
  • 首先调用其finalize方法。
  • 下一次垃圾收集过程中,真正回收内存。

不确定性:

  • finalize的执行时间是不缺定的。
  • 一个对象引用另一个对象,并不能保证finalize的方法按照特定的执行顺序。

5.Override,Overload

 

  Override Overload
签名+返回值 相同 方法名相同,签名不同
关系 父子类继承关系 通常是同一类层次中
识别 运行时多态
根据具体的对象,
查询对象的虚方法表,确定调用关系
编译时多态
由对象的外观类型(即声明类型)决定
修饰符限制 非private
非static
非final
无特别
异常关系 子类方法不能抛出被父类方法更多的异常 无特别
可见性关系 子类不能比父类访问权限更窄
(里氏替换原则决定)
无特别

6.Collection Collections

Collection:接口,集合类的接口,一个契约,提供了集合基本的大小,添加,清除,遍历方法等。

Collections:工具类,提供了很多静态方法,给集合提供一些查询,比较,排序,交换,线程安全化等方法。

7.Integer 缓存

  1. packagecom.jue.test;
  2. publicclassTestMain{
  3. publicstaticvoidmain(String[]args){
  4. Integeri1=1;
  5. Integeri11=1;
  6. System.out.println(i1==i11);
  7. Integeri2=200;
  8. Integeri22=200;
  9. System.out.println(i2==i22);
  10. }
  11. }

结果 :

true

false

分析:反编译结果为

  1. packagecom.jue.test;
  2. importjava.io.PrintStream;
  3. publicclassTestMain
  4. {
  5. publicstaticvoidmain(String[]args)
  6. {
  7. Integeri1=Integer.valueOf(1);
  8. Integeri11=Integer.valueOf(1);
  9. System.out.println(i1==i11);
  10. Integeri2=Integer.valueOf(200);
  11. Integeri22=Integer.valueOf(200);
  12. System.out.println(i2==i22);
  13. }
  14. }

可以看出,对于Integer i = 1;编译器做了额外的处理,即Integer.valueof();

Integer source code

  1. publicstaticIntegervalueOf(inti){
  2. assertIntegerCache.high>=127;
  3. if(i>=IntegerCache.low&&i<=IntegerCache.high)
  4. returnIntegerCache.cache[i+(-IntegerCache.low)];
  5. returnnewInteger(i);
  6. }

可以看出Integer对于一定 范围内的数字从Cache中取得,对于额外的,调用new创建。

IntegerCache源码如下:

  1. privatestaticclassIntegerCache{
  2. staticfinalintlow=-128;
  3. staticfinalinthigh;
  4. staticfinalIntegercache[];
  5. static{
  6. //highvaluemaybeconfiguredbyproperty
  7. inth=127;
  8. StringintegerCacheHighPropValue=sun.misc.VM
  9. .getSavedProperty("java.lang.Integer.IntegerCache.high");
  10. if(integerCacheHighPropValue!=null){
  11. inti=parseInt(integerCacheHighPropValue);
  12. i=Math.max(i,127);
  13. //MaximumarraysizeisInteger.MAX_VALUE
  14. h=Math.min(i,Integer.MAX_VALUE-(-low));
  15. }
  16. high=h;
  17. cache=newInteger[(high-low)+1];
  18. intj=low;
  19. for(intk=0;k<cache.length;k++)
  20. cache[k]=newInteger(j++);
  21. }
  22. privateIntegerCache(){
  23. }
  24. }
故可以知道Integer的大小,默认是从-128到127,对于这个范围内的数组做了缓存的处理。

8.sleep方法和wait方法的区别

 

  wait sleep
所属类 Object Thread
意义 让线程挂起 让线程休眠指定的时间
释放锁 否(这个跟锁本来就没有关系)
恢复 1.有参:wait指定时间
2.无参:等待其他线程notify
1.根据参数长度自动恢复。
2.异常打断
使用限制 wait,notify必须持有当前对象锁的情况下调用 无特别
抛出异常
静态方法

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326563723&siteId=291194637