JAVA基础面试题 二

float f=3.4;是否正确?

  • 不正确。3.4是双精度数,将双精度型(double)赋值给浮点型(float)属于下转型(down-casting,也称为窄化)会造成精度损失
  • 因此需要强制类型转换float f =(float)3.4; 或者写成float f =3.4F;。
public class UserTest {
    public static void main(String[] args) {
        float f1 = 3.4F;
        float f2 = (float)3.4;
        System.out.println("f1"+f1);
        System.out.println("f2"+f2);
    }
}

---结果输出--

f1:3.4

f2:3.4
Process finished with exit code 0

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);其中有隐含的强制类型转换。
public class UserTest {
    public static void main(String[] args) {
        short s1 = 1;
        s1 = (short)(s1 +1);//不加(short)类型转换时,编译直接不通过
        System.out.println("s1:"+s1);

        short s2 = 2;
        s2 += 2;
        System.out.println("s2:"+s2);
    }
}

----输出结果-------

s12
4

Process finished with exit code 0

int和Integer有什么区别?

  • Java是面向对象的编程语言,但为了编程的方便还是引入了基本数据类型
  • 从Java 5开始引入了自动装箱/拆箱机制,使得二者可以相互转换
  • Java 为8个基本类型都提供了包装类型:
  1. - 原始类型: boolean,char,byte,short,int,long,float,double
  2. - 包装类型:Boolean,Character,Byte,Short,Integer,Long,Float,Double

装箱

  • 装箱:自动将基本数据类型转换为包装器类型
  • 装箱时自动调用的是Integer的valueOf(int)方法将基本类型转为封装类型,其余基本类型同理(如float、double等)
public static void main(String[] args) {
    Integer integer1 = 100;//装箱:等价于使用"Integer.valueOf(100)"
    int i = integer1;//拆箱:等价于使用"integer1.intValue()"
}

拆箱

  • 拆箱:自动将包装器类型转换为基本数据类型。
  • 拆箱时自动调用Integer的intValue方法

面试点一

  • Integer、Short、Byte、Character、Long这几个类的valueOf方法的实现是类似的
public static void main(String[] args) {
    Integer integer1 = 100;
    Integer integer2 = 100;

    Integer integer3 = 200;
    Integer integer4 = 200;
    
    System.out.println(integer1 == integer2);//输出true
    System.out.println(integer3 == integer4);//输出false
}
  • 这里要用到常量池的知识点,已经知道上面使用valueOf(int i)做了自动装箱,现在查看一下它的源码:



  • 意思是:通过valueOf方法创建Integer对象时,如果数值在[-128,127]之间,便返回指向IntegerCache.cache(常量池)中已经存在的对象的引用;否则创建一个新的Integer对象。
  • 所以integer1、integer2对象引用指向的是同一个对象而相等 ;integer3、integer4指向的是两个不同的对象,所以不会相等

面试点2

  • Float与Double的valueOf(xx)方法完全同理,都是直接新建对象
public static void main(String[] args) {
    Double i1 = 100.0;
    Double i2 = 100.0;
    Double i3 = 200.0;
    Double i4 = 200.0;

    System.out.println(i1==i2);//输出false
    System.out.println(i3==i4);//输出false
}
  • 如下Double的valueOf(double d)方法源码是直接新建对象,所以上面无论如何都不会相等


面试点3

public static void main(String[] args) {
    Boolean i1 = false;
    Boolean i2 = false;
    Boolean i3 = true;
    Boolean i4 = true;

    System.out.println(i1 == i2);//输出true
    System.out.println(i3 == i4);//输出true
}
  • 通过下面的源码就一目了然了



面试点4

public static void main(String[] args) {
    Integer v1 = 100;
    Integer v2 = Integer.valueOf(100);
    /**输出true
     * v1100执行装箱得到的,也是Integer.valueOf(100);v2则是从常量池中获取的,因为100[-128,127]范围内*/
    System.out.println(v1 == v2);

    /**输出为false
     * v1100通过装箱得到对象引用,v3是直接指向的新建的对象
     * */
    Integer v3 = new Integer(100);
    System.out.println(v1 == v3);

    /**输出false,显然完全是两个不同的对象引用*/
    Integer v4 = new Integer(100);
    System.out.println(v3 == v4);
}

&和&&的区别?

  • 二者都要求运算符左右两端的布尔值都是true整个表达式的值才是true。
  • &运算符逻辑与运算,无论左边是否为true,右边都会进行运算
  • &&运算符是短路与运算,如果&&左边的表达式的值是false,右边的表达式会被直接短路掉,不会进行运算。
  • 逻辑或运算符(|)和短路或运算符(||)也是同理。

Math.round(11.5) 等于多少?Math.round(-11.5)等于多少?

  • Math.round(11.5)的返回值是12,Math.round(-11.5)的返回值是-11。四舍五入的原理是在参数上加0.5然后进行下取整。
public static void main(String[] args) {
    long a = Math.round(11.5);
    long b = Math.round(-11.5);
    System.out.println(a + ":" + b);//输出"12:-11"
}

switch 是否能作用在byte、long、String上?

  • switch(expr1)中,expr1是一个整数表达式。
  • 所以JDK5以前传递给 switch 和 case 语句的参数只能是byte、short、 int、char
  • JDK5开始支持枚举Enum
  • JDK7开始支持String
  • 但目前float,double、long还不支持

public static void main(String[] args) {
    String param = "11";
    /** switch case现在支持byteshortintcharEnumString参数类型*/
    switch (param) {
        case "11":
            System.out.println(11);//输出
            break;
    }
}

猜你喜欢

转载自blog.csdn.net/wangmx1993328/article/details/80593524