一.java包含哪两大数据类型?其中基本类型的每种类型的取值范围和默认值是多少?
数据类型:基本类型 、复合类型
基本类型:
整数类型:
byte(8位):取值范围:-2^7~2^7-1 默认值:0
short(16):取值范围:-2^15~2^15-1 默认值:0
int(32) :取值范围:-2^31~2^31-1 默认值:0
long(64):取值范围:-2^63~2^63-1 默认值:0L
浮点类型:
float :默认值:0.0f
double:默认值:0.0d
字符类型:
char(16位无符号整数,使用Unicode字符集):默认值: \u0000
布尔类型:
boolean(值为true或false,C++ 的布尔类型是bool):默认值:false
二. Java在什么情况会发生整型溢出?请举例说明,并给出解决方案。
两个数字进行数学运算的时候容易发生整型溢出
如:
public static void main(String[] args)
{
int m = Integer.MAX_VALUE/2+1;
int n = Integer.MAX_VALUE/2+1;
int sum = m + n;
System.out.println(sum);
}
修改为:
public static void main(String[] args)
{
int m = Integer.MAX_VALUE/2+1;
int n = Integer.MAX_VALUE/2+1;
long sum = (long) m + n;
System.out.println(sum);
}
三. Java基本类型的包装类分别是哪些?其高频区间数据缓存范围分别是什么?请选择一种包装类型编程验证其数据缓存特性。
基本类型包装类:Character、Byte、Short、Integer、Long、Float、Double
其中Double和Float没有缓存,其他类型都有高频缓存区间。
其高频缓存区间的缓存范围是:
Boolean:使用静态final,就会返回静态值
Byte:-128~127
Short:-128~127
Character:0~127
Long:-128~127
Integer:-128~127
如:
Integer i1 = 127;
Integer i2 = 127;
i1 == i2 ->true
Integer i1 = 128;
Integer i2 = 128;
i1 == i2 ->false
四.什么是自动装箱,什么是自动拆箱,请举例说明。
装箱就是自动将基本数据类型转换为包装器类型;拆箱就是自动将包装器类型转换为基本数据类型。
Integer varInteger=100;
<——>Integer varInteger=Integer.valueOf(100);//自动装箱
int varint=varInteger;
<——>int varint=varInteger.intValue();//自动拆箱
五. int与Integer有什么区别,它们之间的相互转化是怎样的? 请通过JDK文档自主学习Integer类,对主要方法进行测试。
Integer是int的包装类
从Java 5开始引入了自动装箱/拆箱机制,使得二者可以相互转换。
(1)由于Integer变量实际上是对一个Integer对象的引用,所以两个通过new生成的Integer变量永远是不相等的(因为new生成的是两个对象,其内存地址不同)。
Integer i = new Integer(100);
Integer j = new Integer(100);
System.out.print(i == j); //false
(2)Integer变量和int变量比较时,只要两个变量的值是向等的,则结果为true(因为包装类Integer和基本数据类型int比较时,java会自动拆包装为int,然后进行比较,实际上就变为两个int变量的比较)
Integer i = new Integer(100);
int j = 100;
System.out.print(i == j); //true
(3)非new生成的Integer变量和new Integer()生成的变量比较时,结果为false。因为非new生成的Integer变量指向的是静态常量池中cache数组中存储的指向了堆中的Integer对象,而new Integer()生成的变量指向堆中新建的对象,两者在内存中的对象引用(地址)不同。
Integer i = new Integer(100);
Integer j = 100;
System.out.print(i == j); //false
4)对于两个非new生成的Integer对象,进行比较时,如果两个变量的值在区间-128到127之间,则比较结果为true,如果两个变量的值不在此区间,则比较结果为false。
Integer i = 100;
Integer j = 100;
System.out.print(i == j); //true
Integer i = 128;
Integer j = 128;
System.out.print(i == j); //false
六. 逻辑运算符&和&&的区别是什么?逻辑运算符&与位运算符&的区别是什么?请分别举例说明。
算符 | 运符 | 举例 | 运算规则 |
& | 与 | x&y | x,y都为true时,结果为true |
&& | 条件与 | x&&y | x,y都为true时,结果为true |
区别:
1.&在执行操作时,运算符左右两边的表达式首先被运算执行,再对两表达式的结果进行运算
2.&&在执行操作时,如果从左边表达式中得到操作数能确定运算结果,则不再对右边表达式进行运算,即——短路效应
如:
public class Start
{
public static void main(String[] args)
{
int a = 0;
boolean b = false;
if (b == true && a == 10)
{
System.out.println("相等,a="+a);
} else
{
System.out.println("不等,a="+(a+10));
}
}
}
//输出a=10
public class Start
{
public static void main(String[] args)
{
int a = 0;
boolean b = false;
if (b == true & a == 10)
{
System.out.println("相等,a="+a);
} else
{
System.out.println("不等,a="+(a+10));
}
}
}
//输出a=10
七. Java语言中可以采用什么语句跳出多重循环?请举例说明
1.break:
break;(跳出本层循环)
break lab; (跳出多重循环的外层循环)
其中:lab是用户定义的标号。
break lab语句用在循环语句中,必须在外循环入口语句的前方写上lab标号,可以使程序流程退出标号所指明的外循环。
lab:
for(int i =0; i<1; i++)
{
for(int j=0; j<10; j++)
{
if (j >1)
{
break lab;
}
System.out.println(“break");
}
}
//输出break
2.continue:
continue;(跳出本层的本次循环,继续本层下一次循环)
continue lab;(跳出外层的本次循环,继续外层下一次循环)
lab是用户定义的标号。
当程序中有嵌套的多层循环时,为从内循环跳到外循环,可使用带标号的continue lab语句。此时应在外循环的入口语句前方加上标号。
lab:
for(int i =0; i<2; i++)
{
for(int j=0; j<10; j++)
{
if (j >1)
{
continue lab;
}
System.out.println("continue");
}
System.out.println("************");
}
//输出continue continue continue continue