内置数据类型 引用数据类型 八大基本数据类型:四个整数型(byte,short,int,long)两个浮点数型(float,double)一个字符型(char)一个布尔类型(bool)
| 类型 | 关键字 | 最小值 | 最大值 | 默认值 |
|---|---|---|---|---|
| 字节型 | byte | -27 | 27-1 | 0 |
| 短整型 | short | -215 | 215-1 | 0 |
| 整型 | int | -231 | 231-1 | 0 |
| 长整型 | long | -263 | 263-1 | 0L |
| 单精度浮点型 | float | -3.40E+38 | +3.40E+38 | 0.0F |
| 双精度浮点型 | double | -1.79E+308 | +1.79E+308 | 0.0D |
| 字符型 | char | 0 | 216-1 | ‘u0000’ |
| 布尔型 | bool | false | true | false |
当数值已经达到该类型的最大值并在继续扩大或已经达到该类型的最小值并继续缩小时,就会出现溢出问题。
public static void main(String[] args){
int x=Integer.MAX_VALUE/2+1;
int y=Integer.MAX_VALUE/2+1;
int sum=x+y;
System.out.println(sum);
此时sum发生了整型溢出,若要修改这一错误即需把sum的类型改为long型。
3. Java基本类型的包装类分别是哪些?其高频区间数据缓存范围分别是什么?请选择一种包装类型编程验证其数据缓存特性。| 数据类型 | 包装类 | 高频区间数据缓存范围 |
|---|---|---|
| byte | Byte | -128~127 |
| short | Short | -128~127 |
| int | Integer | -128~127 |
| long | Long | -128~127 |
| float | Float | / |
| double | Double | / |
| char | Character | 0~127 |
| bool | Boolean | 使用静态final,返回静态值 |
自动装箱:将基本数据类型自动转换为封装类型。
自动拆箱:将封装类型自动转换为基本数据类型。
//自动装箱 Integer x=1; //自动拆箱 int y=x;5. int与Integer有什么区别,它们之间的相互转化是怎样的? 请通过JDK文档自主学习Integer类,对主要方法进行测试。
int:一种数据类型
Integer:int的包装类
二者的互相转化
//int转为Integer int x=2; Integer y=new Integer(x); //Integer转为int Integer m=new Integer(2); int n=m.intValue();6. 逻辑运算符&和&&的区别是什么?逻辑运算符&与位运算符&的区别是什么?请分别举例说明
逻辑运算符&与&&
二者均为与运算符,&不是短路运算符,&&是短路运算符。
// &&
public class basic{
public static void main(String[] args){
int x=1;
boolean no = false;
if ((no==true) && (x+=1)==2){
System.out.println("相等,x="+x);
} else {
System.out.println("不相等,x="+x);
}
}
}
// &
public class basic {
public static void main(String[] args){
int x=1;
boolean no = false;
if ((no==true) & (x+=1)==2){
System.out.println("相等,x="+x);
} else {
System.out.println("不相等,x="+x);
}
}
}
逻辑运算符&与位运算符&
逻辑运算符:左右两边表达式均为true则结果为true
与位运算符:二进制相应位数均为1则该位为1,否则为0
//逻辑与 &
public class basic {
public static void main(String[] args){
int x=1;
boolean no=false;
if ((no==true) & (x+=1)==2){
System.out.println("相等,x="+x);
} else {
System.out.println("不相等,x="+x);
}
}
}
//位运算 &
int x=1,y=4;
int ans=x&y;
System.out.println(ans);
7. Java语言中可以采用什么语句跳出多重循环?请举例说明
break lab:
跳出lab标识的循环
lab:
for(int i =0; i<2; i++) {
for(int j=0; j<10; j++) {
if (j >1) {
break lab;
}
System.out.println(“break");
}
}
输出:
break break
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



