大二期末复习java时搜藏的一些面试时会用到的问题

来源:互联网 发布:聚创淘宝培训 编辑:程序博客网 时间:2024/04/29 15:32

1、java什么时候会给变量自动进行初始化?

成员变量会被初始化,局部变量不会初始化。public class Main {static int a; // 成员变量public static void main(String[] args) {int b; // 局部变量System.out.println(a);// 正确输出,默认值是0System.out.println(b);// 编译异常,没有初始化。因为程序不会自动赋值}}

Q: How manyobjects are created in the following piece of code?

MyClass c1, c2,c3;

c1 = new MyClass();

c3 = new MyClass();

A: Only 2objects are created, c1 and c3. The reference c2 is only declared and not

initialized.  

2、Checked exceptions and unchecked exceptions

Q: What areChecked and UnChecked Exception?

A: A checkedexception is some subclassof Exception (or Exception itself), excluding class RuntimeException and itssubclasses.

Making anexception checked forces client programmers to deal with the possibility thatthe exception will be thrown. eg, IOException(属于checked) thrown by java.io.FileInputStream's read() method·

Unchecked exceptions areRuntimeException and any of its subclasses. Class Errorand its subclasses also are unchecked. With an unchecked exception, however,the compiler doesn't force client programmers either to catch the exception ordeclare it in a throws clause. In fact, client programmers may not even knowthat the exception could be thrown. eg, StringIndexOutOfBoundsException thrownby String's charAt() method· Checkedexceptions must be caught at compile time. Runtime exceptions do not need tobe. Errors often cannot be.

Q: What arechecked exceptions?

A: Checkedexception are those which the Java compiler forces you to catch. e.g.

IOException arechecked Exceptions.

[ Received fromSandesh Sadhale] TOP

Q: What areruntime exceptions?

A: Runtimeexceptions are those exceptions that are thrown at runtime because of

either wronginput data or because of wrong business logic etc. These are not checked  by the compiler at compile time. 

3、Interface  and  abstract Class

在接口中定义变量必须初始化,因为它默认的是public, static, final。方法默认是public,abstract

接口中的方法一定是抽象方法,抽象类只要有抽象方法就可以

接口和抽象类之间的关系:相同点:

(1)接口和抽象了都不能被实例化,它们都位于继承树的顶端,用于被其他的类实现和继承。    (2)接口和抽象类都是可以包含抽象方法的,实现接口或是继承抽象类的普通子类都必须实现这些抽象方法。

不同点:

(1)接口只能包含抽象方法,不能包含已经提供实现的方法;抽象类则完全可以包含普通的方法    (2)接口不能定义静态方法;抽象类完全可以定义静态方法。    (3)接口中只能定义静态常量Field,不能定义普通的Field;抽象类既可以定义普通的Field也能定义静态常量Field    (4)接口不能包含构造器;抽象类中可以包含构造器,抽象类中的构造器并不是用于创建对象的,而是让其子类调用这些构造器来完成抽象类的初始化操作。   (5)接口里面不能够包含初始化块;但是抽象类里面则完全可以包含初始化块   (6)一个类最多只能有一个直接父类,包括抽象类;但是一个类可以直接实现多个接口,通过实现多个接口可以弥补Java中的单继承的不足

4、在Java中,有两种初始化块:静态初始化块和非静态初始化块.

静态初始化块:使用static定义,当类装载到系统时执行一次.若在静态初始化块中想初始化变量,那仅能初始化类变量,即static修饰的数据成员.非静态初始化块:在每个对象生成时都会被执行一次,可以初始化类的实例变量.

非静态初始化块会在构造函数执行时,且在构造函数主体代码执行之前被运行.

 1 package com.tiandinet.studyjava; 2   3 public class TestInitiateBlock { 4   5     { 6         System.out.println("In non-static initialization block!"); 7     }; 8   9     static {10         System.out.println("In static initialization block!");11     };12  13     public TestInitiateBlock() {14         System.out.println("In Constructor1!");15     }16  17     public void show() {18         System.out.println("In show()!");19     }20  21     /**22      * @param args23      */24     public static void main(String[] args) {25         TestInitiateBlock ti = new TestInitiateBlock();26         ti.show();27     }28  29 }
运行结果:In static initialization block!In non-static initialization block!In Constructor1!In show()!

0 0
原创粉丝点击