5年大厂Java高频面试题及答案整理

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

1

2

3

4

5

6

7

8

9

10

1 class AutoUnboxingTest {

 2 

 3     public static void main(String[] args) {

 4         Integer a = new Integer(3);

 5         Integer b = 3;                  // 将3自动装箱成Integer类型

 6         int c = 3;

 7         System.out.println(a == b);     // false 两个引用没有引用同一对象

 8         System.out.println(a == c);     // true a自动拆箱成int类型再和c比较

 9     }

10 }

最近还遇到一个面试题,也是和自动装箱和拆箱有点关系的,代码如下所示:

1

2

3

4

5

6

7

8

9

1 public class Test03 {

2 

3     public static void main(String[] args) {

4         Integer f1 = 100, f2 = 100, f3 = 150, f4 = 150;

5 

6         System.out.println(f1 == f2);

7         System.out.println(f3 == f4);

8     }

9 }

如果不明就里很容易认为两个输出要么都是true要么都是false。首先需要注意的是f1、f2、f3、f4四个变量都是Integer对象引用,所以下面的==运算比较的不是值而是引用。装箱的本质是什么呢?当我们给一个Integer对象赋一个int值的时候,会调用Integer类的静态方法valueOf,如果看看valueOf的源代码就知道发生了什么。

IntegerCache是Integer的内部类,其代码如下所示:

12345678910111213141516171819202122232425262728293031323334353637383940	1 /** 2      * Cache to support the object identity semantics of autoboxing for values between 3      * -128 and 127 (inclusive) as required by JLS. 4      * 5      * The cache is initialized on first usage.  The size of the cache 6      * may be controlled by the {@code -XX:AutoBoxCacheMax=<size>} option. 7      * During VM initialization, java.lang.Integer.IntegerCache.high property 8      * may be set and saved in the private system properties in the 9      * sun.misc.VM class.10      */11 12     private static class IntegerCache {13         static final int low = -128;14         static final int high;15         static final Integer cache[];16 17         static {18             // high value may be configured by property19             int h = 127;20             String integerCacheHighPropValue =21                 sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");22             if (integerCacheHighPropValue != null) {23                 try {24                     int i = parseInt(integerCacheHighPropValue);25                     i = Math.max(i, 127);26                     // Maximum array size is Integer.MAX_VALUE27                     h = Math.min(i, Integer.MAX_VALUE - (-low) -1);28                 } catch( NumberFormatException nfe) {29                     // If the property cannot be parsed into an int, ignore it.30                 }31             }32             high = h;33 34             cache = new Integer[(high - low) + 1];35             int j = low;36             for(int k = 0; k < cache.length; k++) 37 cache[k] = new Integer(j++); 38 39 // range [-128, 127] must be interned (JLS7 5.1.7) 40 assert IntegerCache.high >= 127;41         }42 43         private IntegerCache() {}44     }

1 /**

 2      * Cache to support the object identity semantics of autoboxing for values between

 3      * -128 and 127 (inclusive) as required by JLS.

 4      *

 5      * The cache is initialized on first usage.  The size of the cache

 6      * may be controlled by the {@code -XX:AutoBoxCacheMax=<size>} option.

 7      * During VM initialization, java.lang.Integer.IntegerCache.high property

 8      * may be set and saved in the private system properties in the

 9      * sun.misc.VM class.

10      */

11 

12     private static class IntegerCache {

13         static final int low = -128;

14         static final int high;

15         static final Integer cache[];

16 

17         static {

18             // high value may be configured by property

19             int h = 127;

20             String integerCacheHighPropValue =

21                 sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");

22             if (integerCacheHighPropValue != null) {

23                 try {

24                     int i = parseInt(integerCacheHighPropValue);

25                     i = Math.max(i, 127);

26                     // Maximum array size is Integer.MAX_VALUE

27                     h = Math.min(i, Integer.MAX_VALUE - (-low) -1);

28                 catch( NumberFormatException nfe) {

29                     // If the property cannot be parsed into an int, ignore it.

30                 }

31             }

32             high = h;

33 

34             cache = new Integer[(high - low) + 1];

35             int j = low;

36             for(int k = 0; k < cache.length; k++) 37 cache[k] = new Integer(j++); 38 39 // range [-128, 127] must be int

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值