
1、面向对象的特征有哪些方面?
� 抽象:将同类对象的共同特征提取出来构造类。
� 继承:基于基类创建新类。
� 封装:将数据隐藏起来,对数据的访问只能通过特定接口。
� 多态性:不同子类型对象对相同消息作出不同响应。
2、访问修饰符 public,private,protected,以及不写(默认)时的区别?
protected 当前类,同包,异包子类。
3、String 是最基本的数据类型吗?
答:不是。Java 中的基本数据类型只有 8 个:byte、short、int、long、
float、double、char、boolean;除了基本类型(primitive type),剩下的
都是引用类型(reference type),Java 5 以后引入的枚举类型也算是一种比
较特殊的引用类型。
4、float f=3.4;是否正确?

答:不正确。3.4 是双精度数,将双精度型(double)赋值给浮点型(float)属
于下转型(down-casting,也称为窄化)会造成精度损失,因此需要强制类型
转换 float f =(float)3.4; 或者写成 float f =3.4F;
5、short s1 = 1; s1 = s1 + 1;有错吗?short s1 = 1; s1 += 1;有错吗?
答:对于 short s1 = 1; s1 = s1 + 1;由于 1 是 int 类型,因此 s1+1 运算结
果也是 int 型,需要强制转换类型才能赋值给 short 型。而 short s1 = 1; s1
+= 1;可以正确编译,因为 s1+= 1;相当于 s1 = (short)(s1 + 1);其中有隐含
的强制类型转换。
6、Java 有没有 goto?
答:goto 是 Java 中的保留字,在目前版本的 Java 中没有使用。(根据 James
Gosling(Java 之父)编写的《The Java Programming Language》一书
的附录中给出了一个 Java 关键字列表,其中有 goto 和 const,但是这两个是
目前无法使用的关键字,因此有些地方将其称之为保留字,其实保留字这个词应
该有更广泛的意义,因为熟悉 C 语言的程序员都知道,在系统类库中使用过的
有特殊意义的单词或单词的组合都被视为保留字)
7、int 和 Integer 有什么区别?
答:Java 是一个近乎纯洁的面向对象编程语言,但是为了编程的方便还是引入
了基本数据类型,但是为了能够将这些基本数据类型当成对象操作,Java 为每
一个基本数据类型都引入了对应的包装类型(wrapper class),int 的包装类

就是 Integer,从 Java 5 开始引入了自动装箱/拆箱机制,使得二者可以相互
转换。
Java 为每个原始类型提供了包装类型:
� 原始类型: boolean,char,byte,short,int,long,float,double
� 包装类型:Boolean,Character,Byte,Short,Integer,Long,
Float,Double
class AutoUnboxingTest {
public static void main(String[] args) {
Integer a = new Integer(3);
Integer b = 3; // 将 3 自动装箱成 Integer 类型
int c = 3;
System.out.println(a == b); // false 两个引用没有引用同一对象
System.out.println(a == c); // true a 自动拆箱成 int 类型再和 c 比较
}
}
最近还遇到一个面试题,也是和自动装箱和拆箱有点关系的,代码如下所示:
public class Test03 {
public static void main(String[] args) {
Integer f1 = 100, f2 = 100, f3 = 150, f4 = 150;
System.out.println(f1 == f2);

System.out.println(f3 == f4);
}
}
如果不明就里很容易认为两个输出要么都是 true 要么都是 false。首先需要注意
的是 f1、f2、f3、f4 四个变量都是 Integer 对象引用,所以下面的==运算比
较的不是值而是引用。装箱的本质是什么呢?当我们给一个 Integer 对象赋一
个 int 值的时候,会调用 Integer 类的静态方法 valueOf,如果看看 valueOf
的源代码就知道发生了什么。
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
IntegerCache 是 Integer 的内部类,其代码如下所示:
/**
* Cache to support the object identity semantics of autoboxing for values between
* -128 and 127 (inclusive) as required by JLS.
*
* The cache is initialized on first usage. The size of the cache
* may be controlled by the {@code -XX:AutoBoxCacheMax=<size>} option.

* During VM initialization, java.lang.Integer.IntegerCache.high property
* may be set and saved in the private system properties in the
* sun.misc.VM class.
*/
private static class IntegerCache {
static final int low = -128;
static final int high; static final Integer cache[];
static {
// high value may be configured by property
int h = 127;
String integerCacheHighPropValue =
sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.hig
h");
if (integerCacheHighPropValue != null) {
try { int i = parseInt(integerCacheHighPropValue);
i = Math.max(i, 127);
// Maximum array size is Integer.MAX_VALUE
h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
} catch( NumberFormatException nfe) {
// If the property cannot be parsed into an int, ignore it.
}
}
high = h;
cache = new Integer[(high - low) + 1];
int j = low;
for(int k = 0; k < cache.length; k++)
cache[k] = new Integer(j++);
// range [-128, 127] must be interned (JLS7 5.1.7)