1、存在使i + 1 < i的数吗?
存在,如果i为int型,那么当i为int能表示的最大整数时,i+1就溢出变成负数了,此时不就<i了吗。
存在使i > j || i <= j不成立的数吗?
存在,比如Double.NaN或Float.NaN
2、0.6332的数据类型是什么?
默认为double型,如果为float型需要加上f显示说明,即0.6332f
3、下面的方法,当输入为2的时候返回值是多少?
public static int getValue(int i) {
int result = 0;
switch (i) {
case 1:
result = result + i;
case 2:
result = result + i * 2;
case 3:
result = result + i * 3;
}
return result;
}
返回10。注意这里case后面没有加break,所以从case 2开始一直往下运行。
4、Integer
public class Main_1 {
public static void main(String[] args) {
Integer a = 1;
Integer b = 2;
Integer c = 3;
Integer d = 3;
Integer e = 321;
Integer f = 321;
Long g = 3L;
Long h = 2L;
System.out.println(c == d);
System.out.println(e == f);
System.out.println(c == (a + b));
System.out.println(c.equals(a + b));
System.out.println(g == (a + b));
System.out.println(g.equals(a + b));
System.out.println(g.equals(a + h));
}
}
在Java中会有一个Integer缓存池,缓存的大小是:-128~127
true
false
true
true
true
false
true
说明:
使用==的情况
- 如果比较Integer变量,默认比较的是地址值。
- Java的Integer维护了从-128~127的缓存池
- 如果比较的某一边有操作表达式(例如a+b),那么比较的是具体数值
使用equals()的情况:
- 无论是Integer还是Long中的equals()默认比较的是数值。
- Long的equals()方法,JDK的默认实现:会判断是否是Long类型
- 注意自动拆箱,自动装箱问题。
反编译一下:
public class Main_1 {
public static void main(String[] paramArrayOfString) {
Integer localInteger1 = Integer.valueOf(1);
Integer localInteger2 = Integer.valueOf(2);
Integer localInteger3 = Integer.valueOf(3);
Integer localInteger4 = Integer.valueOf(3);
Integer localInteger5 = Integer.valueOf(321);
Integer localInteger6 = Integer.valueOf(321);
Long localLong = Long.valueOf(3L);
// 缓存池
System.out.println(localInteger3 == localInteger4);
// 超出缓存池范围
System.out.println(localInteger5 == localInteger6);
// 存在a+b数值表达式,比较的是数值
System.out.println(localInteger3.intValue() == localInteger1.intValue() + localInteger2.intValue());
// equals比较的是数值
System.out.println(localInteger3.equals(Integer.valueOf(localInteger1.intValue() + localInteger2.intValue())));
// 存在a+b数值表达式,比较的是数值
System.out.println(localLong.longValue() == localInteger1.intValue() + localInteger2.intValue());
// Long的equals()先判断传递进来的是不是Long类型,而a+b自动装箱的是Integer类型
System.out.println(localLong.equals(Integer.valueOf(localInteger1.intValue() + localInteger2.intValue())));
// ... 最后一句在这里漏掉了,大家应该可以推断出来
}
}
反编译工具是jd-gui,下载地址:https://github.com/java-decompiler/jd-gui/releases
5、下面代码的输出结果是什么?
public class Test {
public static void main(String[] args) {
double val = 11.5;
System.err.println(Math.round(val));
System.err.println(Math.floor(val));
System.err.println(Math.ceil(val));
}
}
这个是在考Math取整数的三种方法。round()是四舍五入取证,floor()是舍去小数位,ceil()是向上进一位。floor是地板ceil是天花板,一个在下,则舍去,一个在上,则向上进1。那是不是结果应该为12、11、12呢?还要考虑返回值类型,round()返回值类型为long长整型,floor()和ceil()返回值的是double类型,因此正确的答案应该是12、11.0、12.0。
6、true or false?
public class Test {
public static void main(String[] args) {
Integer i1 = 127;
Integer i2 = 127;
System.err.println(i1 == i2);
i1 = 128;
i2 = 128;
System.err.println(i1 == i2);
}
}
这个是对Integer包装类型中应用的享元模式的考察。具体可以看一下http://blog.csdn.net/m0_37240709/article/details/78644341
这个运行的结果是,第一个为true,第二个为false。为什么会这样呢?Integer到底有怎样的密码呢?
Integer.java中的缓存整数的类IntegerCache类。当整数在【-128,127】范围内,自动装箱时不会创建新的对象,是共享的。这是享元模式的一种应用。
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() {}
}
由于这个原因,所以,当Integer的数值在-128到127之间时,创建的对象只要值是相同的,那么这两个对象就是同一个对象,可以用==进行比较。
再来看一下这种情况,结果怎样呢?
public class Test{
public static void main(String[] args) {
Integer i1 = new Integer(97);
Integer i2 = new Integer(97);
System.out.println(i1 == i2);
}
}
这时便是false了,new是在堆中创建了不同的对象。
6、true or false?
public class Test {
public static void main(String[] args) {
System.err.println(12 - 11.9 == 0.1);
}
}
结果为false。这个题我只说下我的想法,12-11.9进行运算后会转换成对象,不在是基本数据类型,因此在进行恒等判断时,就会是false。
7、以下代码输出是什么?
public class Test {
public static void main(String[] args) {
BigInteger one = new BigInteger("1");
BigInteger two = new BigInteger("2");
BigInteger three = new BigInteger("3");
BigInteger sum = new BigInteger("0");
sum.add(one);
sum.add(two);
sum.add(three);
System.out.println(sum.toString());
}
}
这个是对大整数的考察。结果是不是6呢?看起来好像没毛病,其实不然。sum.add(one)与我们基本类型的sum+=one可不同,前者不会讲结果赋值给sum对象,结果被赋值了这条语句的返回值。因此不管怎么add,sum对象的值是没有变化的,因此结果为0。
8、Java中基本数据类型各占多少字节?
类型 |
字节数 |
位数 |
byte |
1 |
8 |
short |
2 |
16 |
int |
4 |
32 |
long |
8 |
64 |
float |
4 |
32 |
double |
8 |
64 |
Boolean |
1 |
|
char |
2 |
16 |
中文2占2个字节
9、64位的JVM当中,int的长度是多少?
Java 中,int 类型变量的长度是一个固定值,与平台无关,都是 4 位,32字节。意思就是说,在 32 位 和 64 位 的Java 虚拟机中,int 类型的长度是相同的。
10、int 和Integer谁占用的内存更多?
Integer 对象会占用更多的内存。Integer是一个对象,需要存储对象的元数据。但是 int 是一个原始类型的数据,所以占用的空间更少。
11、java当中使用什么类型表示价格比较好?
如果不是特别关心内存和性能的话,使用BigDecimal,否则使用预定义精度的 double 类型。
12、如何将byte转为String
可以使用 String 接收 byte[] 参数的构造器来进行转换,需要注意的点是要使用的正确的编码,否则会使用平台默认编码,这个编码可能跟原来的编码相同,也可能不同。
13、可以将int强转为byte类型么?会产生什么问题?
我们可以做强制转换,但是Java中int是32位的而byte是8 位的,所以,如果强制转化int类型的高24位将会被丢弃,byte 类型的范围是从-128到128
14、java当中采用的是大端还是小端?
1、什么是大端和小端?
1) Little-Endian就是低位字节排放在内存的低地址端,高位字节排放在内存的高地址端。
2) Big-Endian就是高位字节排放在内存的低地址端,低位字节排放在内存的高地址端。
java虚拟机把系统的大端和小端给屏蔽了,内部都是小端。
Java大端字节和小端字节
举例说明:例如数字0x12345678在内存中表现形式为:
1)大端模式:
低地址 -----------------> 高地址
0x12 | 0x34 | 0x56 | 0x78
2)小端模式:
低地址 ------------------> 高地址
0x78 | 0x56 | 0x34 | 0x1
2、Java 大端互转代码
public static int toLittleEndian(int a) {
return (((a & 0xFF) << 24) | (((a >> 8) & 0xFF) << 16) | (((a >> 16) & 0xFF) << 8) | ((a >> 24) & 0xFF));
}
15、什么是隐式转换,什么是显式转换
显示转换就是类型强转,把一个大类型的数据强制赋值给小类型的数据;
隐式转换就是大范围的变量能够接受小范围的数据;
隐式转换和显式转换其实就是自动类型转换和强制类型转换。
16、Char类型能不能转成int类型?能不能转化成string类型,能不能转成double类型
Char在java中也是比较特殊的类型,它的int值从1开始,一共有2的16次方个数据;Char<int<long<float<double;
Char类型可以隐式转成int,double类型,但是不能隐式转换成string;
如果char类型转成byte,short类型的时候,需要强转。
17、Java中各种数据默认值
Byte,short,int,long默认是都是0 Boolean默认值是false Char类型的默认值是’’ Float与double类型的默认是0.0 对象类型的默认值是null