# 2020-12-29 面试题

9 篇文章 0 订阅

2020-12-29 面试题

1.面对对象的特征有哪些方面?

  • 抽象:抽象时将以类对象的共同特征总结出来构造类的过程,包括数据抽象和行为抽象两方面。
  • 继承:继承时从以由类得到继承信息创建新类的过程。提供继承信息的类称为父类。得到继承信息的类称为子类。
  • 封装:通常认为是把数据和操作数据的方法绑定起来。对数据的访问只能通过一定义的接口。
  • 多态性:多态性实直允许不同子类型的对象对同一信息做出不同的响应。

2.访问修饰符 public,private,protected 以及默认时的区别?

修饰符 当前类 同包 子类 其他包
public √ √ √ √
protected √ √ √
dafault √ √
private √

类成员不写访问修饰时的默认为default。默认对于统一包种的其他类相当于公开。对于不是同一包种的其他类相当于私有。受保护对子类相当于公开,对不是同一包种的没有父子关系的类相当于私有。

3.String 是最基本的数据类型吗?

不是,Java 中的基本类型有八种:byte、short、int、long、float、double、char、boolean;除了基本类型(primitive type)剩下的都是引用类型。,Java 5 以后引入的美剧类型也算是一种比较特殊的引用类型。

4. float=3.4正确吗?

不正确,3.4是双精度。双精度double赋值给浮点float型属于转型。会造成精度损失。也成为强制转型。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型,因此结果是int型,需要强制转型才能赋值short型。
而,short型 short s1=1;s1+=1 仍然是short型,因为s1+=1相当于s1=(short)(s1+1);其中隐含强制类型转换。

6.Java有没有goto?

答:goto是Java中的保留字,在目前版本的Java中没有使用,goto和counst仍然是保留字。无法使用的关键字。

7. int和Integer有什么区别?

答:Java是一个近乎纯洁的面对对象编程的语言。但是为了编程,引入了基本数据类型(8)种。
但为了将基本数据类型当成对象操作,Java为每一个基本数据类型都引入了对应的包装类型。
从 Java5开始引入了自动装箱/拆箱机制,使得二者可以强制转换。
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比较
    }
}

第一个输出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.high");
            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)
            assert IntegerCache.high >= 127;
        }

        private IntegerCache() {}
    }

简单的说,如果整型字面量的值在-128到127之间,那么不会new新的Integer对象,而是直接引用常量池中的Integer对象,所以上面的面试题中f1f2的结果是true,而f3f4的结果是false。

8.&和&&的区别?

答:&运算符有两种用法:(1)按位与;(2)逻辑与。
&&运算符是短路与运算。逻辑左边为false则整条逻辑语句都为false
&运算符 逻辑语句左边为false,还会继续比较。

9.Math.round(11.5) ? Math.round(-11.5)?

答:Math.round(11.5) 12
Math.round(-11.5)
-11
四舍五入的原理是在参数上加0.5然后进行取整。

10.switch 是否能用在byte上?是否能作用在long上,是否能作用在String上?

答: 在Java5之前,switch(expr)中,exper只能是byte、short、char、int。从Java中引入了枚举类型,expr也可以是enum类型,从Java7开始,expr还可以是字符串(String),但是长整型(long)在目前版本中都是不可以的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值