1. JRE是什么?JDK是什么?
- JRE:Java RunTime Environment. Java运行的环境,为java提供运行环境。
- JDK: Java Development Kit. Java开发环境工具包,提供了Java开发环境和运行环境。
如果只需要运行Java有JRE就可以了,JDK包含了JRE,和辩译源码的javac,还包括很多Java程序的调试和分析工具。
2. == 和 equals的区别是什么?
- ==如果是基本类型则则比较值是否相等,引用类型则比较引用是否相等。
- equals一直比较的都是值
String x = "string";
String y = "string";
String z = new String("string");
System.out.println(x==y); // true
System.out.println(x==z); // false
System.out.println(x.equals(y)); // true
System.out.println(x.equals(z)); // true
- x==y为true是因为引用相等
- z开辟了新空间引用不相等
- equals一直比较的都是值
equals是重写了equals方法,equals默认是引用比较,把String和Integer等改成了值比较。
3. hashcode相等那么equals一定为true吗?
- equals相等则hashcode从的也一定相等
- hashcode相等equals不一定相等
String str1 = "通话";
String str2 = "重地";
System.out.println("str1 hashcode=>"+str1.hashCode());
System.out.println("str2 hashcode=>"+str2.hashCode());
System.out.println(str1.equals(str2));
hashcode是通过hash函数得来的,hashcode是在hash表中对应的位置,hashcode提高查找的快捷性,首先比较hashcode,然后比较equals,重写了equals要重写hashcode。
4. final关键字的作用?
- final修饰的类为最终类不能被继承
- final修饰的方法不能被重写
- final修饰的变量为常量,必须初始化,初始化后不能被修改。
5. Math.round(-1.5)等于多少?
- -1
System.out.println("1.4 round结果" + Math.round(1.4));
System.out.println("1.5 round结果" + Math.round(1.5));
System.out.println("1.6 round结果" + Math.round(1.6));
System.out.println("2.4 round结果" + Math.round(2.4));
System.out.println("2.5 round结果" + Math.round(2.5));
System.out.println("2.6 round结果" + Math.round(2.6));
System.out.println("-1.4 round结果" + Math.round(-1.4));
System.out.println("-1.5 round结果" + Math.round(-1.5));
System.out.println("-1.6 round结果" + Math.round(-1.6));
System.out.println("-2.4 round结果" + Math.round(-2.4));
System.out.println("-2.5 round结果" + Math.round(-2.5));
System.out.println("-2.6 round结果" + Math.round(-2.6));
运行结果为:
1.4 round结果1
1.5 round结果2
1.6 round结果2
2.4 round结果2
2.5 round结果3
2.6 round结果3
-1.4 round结果-1
-1.5 round结果-1
-1.6 round结果-2
-2.4 round结果-2
-2.5 round结果-2
-2.6 round结果-3
规律就是这个数加上0.5向下取整。