提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
一、数据类型
二、使用步骤
1.声明并初始化
代码如下(示例):
public class demo {
public static void main(String[] args) {
byte a1 = 1;
short a2 = 2;
int a3 = 3;
char a4 = 65;
long a5 = 5;
double a6 = 6;
float a7 = 7;
boolean a8 = false;
String a9 = "a9";
System.out.println("byte:" + a1);
System.out.println("short:" + a2);
System.out.println("int:" + a3);
System.out.println("char:" + a4);
System.out.println("long:" + a5);
System.out.println("double:" + a6);
System.out.println("flaat:" + a7);
System.out.println("boolean:" + a8);
System.out.println("String:" + a9);
}
}
输出
byte:1
short:2
int:3
char:A
long:5
double:6.0
flaat:7.0
boolean:false
String:a9
2.数据转化
(1)级别从低到高为:byte,char,short(这三个平级)–>int–>float–>long–>double
(2)自动类型转换:从低级别到高级别,系统自动转的;
(3)强制转换
代码如下(示例):
public class demo {
public static void main(String[] args) {
byte byte_1 = 10;
int int_1 = byte_1;//低转高不需要强转
byte byte_2 = (byte) int_1;//需要强转
double dou_1 = int_1;//低转高不需要强转
double dou_2 = 10.7;
int int_2 = (int) dou_2;
System.out.println("byte_1:" + byte_1);
System.out.println("int_1:" + int_1);
System.out.println("dou_1:" + dou_1);
System.out.println("int_2:" + int_2);//不会四舍五入
}
}
输出:
byte_1:10
int_1:10
dou_1:10.0
int_2:10
总结
提示: