avatar

Catalog
Java面试题汇总

java面试题汇总

  1. 数据域的概念

  2. final 的概念

    错题:下列表述错误的是?(D)

    Code
    1
    2
    3
    4
    A int是基本类型,直接存数值,Integer是对象,用一个引用指向这个对象。
    B 在子类构造方法中使用super()显示调用父类的构造方法,super()必须写在子类构造方法的第一行,否则编译不通过
    C 封装的主要作用在于对外隐藏内部实现细节,可以增强程序的安全性
    D final是java中的修饰符,可以修饰类、接口、抽象类、方法和属性。
  3. 面向对象的基本特征

    封装

  4. 静态块与局部变量

    D

    java
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    public class Test{
    static{
    int x=5;
    }
    static int x,y;
    public static void main(String args[]){
    x--;
    myMethod( );
    System.out.println(x+y+ ++x);
    }
    public static void myMethod( ){
    y=x++ + ++x;
    }
    }
  5. java异常处理

    throw 用于抛出异常对象,throws可以在方法上声明要抛出的异常,在try中可以抛出异常

  6. java创建对象的方式有5种

    1、使用 new 关键字(最常用): ObjectName obj = new ObjectName();

    2、使用反射的Class类的newInstance()方法: ObjectName obj = ObjectName.class.newInstance();

    3、使用反射的Constructor类的newInstance()方法: ObjectName obj = ObjectName.class.getConstructor.newInstance();

    4、使用对象克隆clone()方法: ObjectName obj = obj.clone();

    5、使用反序列化(ObjectInputStream)的readObject()方法: try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(FILE_NAME))) { ObjectName obj = ois.readObject(); }

  7. 线程安全的概念

    加了synchronized修饰的方法都是线程安全的,没有加的不安全

    线程安全的类: vector,stringbuffer,properties

    线程不安全的类: Hashmap,ArrayList

  8. “Integer”的深入理解

    Code
    1
    `public` `class` `Test03 {` `    ` `    ``public` `static` `void` `main(String[] args) {``       ` `        ``Integer f1 = ``100``, f2 = ``100``, f3 = ``150``, f4 = ``150``;` `         ` `        ``System. out.println( f1 == f2); ``//true``        ``System. out.println( f3 == f4); ``//false``    ``}``}`

    当我们给一个Integer赋予一个int类型的时候会调用Integer的静态方法valueOf
    Integer f1 = Integer.valueOf(100);
    Integer f2 = Integer.valueOf(100);
    Integer f3 = Integer.valueOf(150);
    Integer f4 = Integer.valueOf(150);
    思考:那么Integer.valueOf()返回的Integer是不是是重新new Integer(num);来创建的呢?如果是这样的话,那么== 比较返回都是false,因为他们引用的堆地址不一样。

    具体来看看Integer.valueOf的源码

    Code
    1
    `public` `static` `Integer valueOf(``int` `i) {``        ` `        ``if` `(i >= IntegerCache.low && i <= IntegerCache.high)``            ``return` `IntegerCache.cache[i + (-IntegerCache.low)];``        ``return` `new` `Integer(i);``}`

    在IntegerCache中cache数组初始化如下,存入了-128 - 127的值

    Code
    1
    `cache = ``new` `Integer[(high - low) + ``1``];``int` `j = low;``for``( ``int` `k = ``0``; k < cache.length ; k ++)``    ``cache[k] =``    ``cache[k] = ``new` `Integer(j ++);`

    从上面我们可以知道给Interger 赋予的int数值在-128 - 127的时候,直接从cache中获取,这些cache引用对Integer对象地址是不变的,但是不在这个范围内的数字,则new Integer(i) 这个地址是新的地址,不可能一样的.

  9. cookie

    以下哪些方法可以取到http请求中的cookie值()?

    正确答案: B D 你的答案: D (错误)

    Code
    1
    2
    3
    4
    A. request.getAttribute
    B. request.getHeader
    C. request.getParameter
    D. request.getCookies

    解答:headers中包括cookie,从headers中可以取到cookie

Author: Jacob
Link: http://yoursite.com/2019/07/26/Java%E9%9D%A2%E8%AF%95%E9%A2%98%E9%94%99%E9%A2%98/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.
Donate
  • 微信
    微信
  • 支付寶
    支付寶