- 1.数据类型
- 2.整型溢出
- 3.包装类型
- 4.自动装箱与自动拆箱
- 5.int与integer
- 6.逻辑运算符&与&&,逻辑运算符&与位运算符&
- 7.跳出多重循环
- 8. 注意
-
Java包含哪两大类数据类型?其中基本类型的每种类型的取值范围和默认值分别是多少?请编程验证。
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 | 0 |
| float | 32 | 1.4E-45 ~3.4E+38 | 0.0f |
| double | 64 | 4.9E-324 ~1.7E308 | 0.0d |
| char | 16 | —— | ‘u0000’ |
| boolean | 64 | —— | false |
public class IntegerExample {
public static void main(String[] args) {
System.out.println(Integer.MAX_VALUE);
System.out.println(Integer.MIN_VALUE);
}
}
2147483647 -2147483648
字符型变量:类型为char,它在机器中占16位,其范围为0~65535,
默认值为’u0000’,表示NUL,空的不可见字符
字符串变量:用String表示,String 不是原始类型,而是一个类(class)
String str=“Hellou0041n”;//定义一个字符串变量,初值为HelloA char[] str=“Hello”;//编译错误,不支持字符串类型到字符数组类型的转换
2.Java在什么情况会发生整型溢出?请举例说明,并给出解决方案。
在选用整数类型上,一定要注意数的范围,否则可能由于数的类型选择不当而造成溢出,例如下面的代码add就存在着潜在的溢出问题,从而为程序带来Bug。
public static void main(String[] args) {
int m = Integer.MAX_VALUE/2+1; //1073741824
int n = Integer.MAX_VALUE/2+1;
int overflow = m + n;
System.out.println(overflow); //-2147483648
}
public static void main(String[] args) {
int m = Integer.MAX_VALUE/2+1; //1073741824
int n = Integer.MAX_VALUE/2+1;
long normal = (long) m + n;
System.out.println(normal); //2147483648
}
3.Java基本类型的包装类分别是哪些?其高频区间数据缓存范围分别是什么?请选择一种包装类型编程验证其数据缓存特性。
| 普通数据类型 | 对应的包装类 | 高频区间数据缓存范围 |
|---|---|---|
| byte | Byte | -128~127 |
| short | Short | -128~127 |
| int | Integer | -128~127 |
| long | Long | -128~127 |
| float | Float | —— |
| double | Double | —— |
| char | Character | —— |
| boolean | Boolean | 使用静态final,就会返回静态值 |
public static void main(String[] args) {
Integer a = 100;//自动装箱
Integer b = Integer.valueOf(100);
System.out.println(a == b);//true
}
public static void main(String[] args) {
Integer a = 1000;//自动装箱
Integer b = Integer.valueOf(1000);
System.out.println(a == b);//false
}
4.什么是自动装箱,什么是自动拆箱,请举例说明。
自动装箱:将基本数据类型转换为封装类型 自动拆箱:将封装类型转换为基本数据类型
Integer a = 100;//自动装箱 Integer a = Integer.valueOf(100); int b = a;//自动拆箱 int b = a.intValue(); //自动装箱,相当于Java编译器替我们执行了 Integer.valueOf(XXX); //自动拆箱,相当于Java编译器替我们执行了Integer.intValue(XXX);
5.int与Integer有什么区别,它们之间的相互转化是怎样的?请通过JDK文档自主学习Integer类,对主要方法进行测试。
JDK文档:Java Platform SE8
6.逻辑运算符&和&&的区别是什么?逻辑运算符&与位运算符&的区别是什么?请分别举例说明
7.Java语言中可以采用什么语句跳出多重循环?请举例说明。
Java语言提供了4种转移语句:break,continue,return和throw
break语句:
break;//跳出本层循环 break lab;//跳出多重循环的外层循环 其中:break是关键字;lab是用户定义的标号
public static void main(String args[]){
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语句:
continue语句只能用于循环结构中,其作用是使循环短路。 continue;//跳出本层的本次循环,继续本层下一次循环 continue lab;//跳出外层的本次循环,继续外层下一次循环
public static void main(String args[]){
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//i=0,j=0 continue//i=0,j=1 continue//i=1,j=0 continue//i=1,j=1
返回语句return:
return语句从当前方法中退出,返回到调用该方法的语句处,并从紧跟该语句的下一条语句继续程序的执行 返回语句有两种格式: return expression; return; return语句通常用在一个方法体的最后,否则会产生编译错误。8. 注意
1.两个整数相加,结果默认转化为int,赋值给byte或short时会发生类型转化问题。
例如下面的代码b+c的结果必须进行显式转化。
public static void main(String[] args) {
byte b = 27;
byte c = 26;
byte d = b + c;
//Type mismatch: cannot convert from int to byte
//不兼容的类型: 从int转换到byte可能会有损失
}
public static void main(String[] args) {
byte b = 27;
byte c = 26;
byte d = (byte)(b + c);
}
- 简单数据类型举例
public class Test {
public static void main(String args[]){
int x,y;//定义x,y两个整型变量
float z=1.234f;//指定变量z为float型,且赋初值为1.234
double w=1.234;//指定变量w为double型,且赋初值为1.234
boolean flag=true;//指定变量flag为boolean型,且赋初值为true
char c;//定义字符型变量c
String str;//定义字符串型变量str
String str1="Hi";//指定变量str1为String型,且赋初值为Hi
c='A';//给字符型变量c赋值'A'
str="bye";给字符串变量str赋值"bye"
x=12;//给整型变量x赋值为12
y=300;//给整型变量y赋值为300
}
}
3.数据类型转换
不同类型数据间的优先关系如下: 低---------------------------------------->高 byte,short,char->int->long->float-> double
自动类型转换规则:
①byte、short、char类型被提升到int类型
②整型, 浮点型,字符型数据可以混合运算。运算中,不同类型的数据先转化为同一类型,然后进行运算,转换从低级到高级



