java中的数据类型和变量定义练习

This commit is contained in:
2025-12-23 15:03:17 +08:00
parent 94a5e249bc
commit 8695169cfb

View File

@@ -21,29 +21,37 @@ Java中的默认类型整数类型是int 、浮点类型是double 。
*/ */
public class Demo02 { public class Demo02 {
public static void main(String[] args){ public static void main(String[] args){
//定义字节型变量 //定义字节型变量1个字节
byte b = 100; byte b = -128;
b = 100;
System.out.println(b); System.out.println(b);
//定义短整型变量 //定义短整型变量2个字节
short s = 1000; short s = -32768;
System.out.println(s); System.out.println(s);
//定义整型变量 //定义整型变量
int i = 123456; int i = 123456;
System.out.println(i); System.out.println(i);
//定义长整型变量 //定义长整型变量(8字节)
long l = 12345678900L; long l = 12345678900L;
System.out.println(l); System.out.println(l);
//定义单精度浮点型变量 //定义单精度浮点型变量
float f = 5.5F; float f = 5.5F;
System.out.println(f); System.out.println(f);
//定义双精度浮点型变量 //定义双精度浮点型变量
double d = 8.5; double d = -8.5;
System.out.println(d); System.out.println(d);
//定义布尔型变量 //定义布尔型变量
//ctrl+D:复制一行
boolean bool = false; boolean bool = false;
boolean bool1 = true;
System.out.println(bool); System.out.println(bool);
//定义字符型变量 //定义字符型变量
char c = 'A'; char c = ' ';
System.out.println(c); System.out.println(c);
//注意事项:变量可以先定义后赋值的,但是使用之前必须赋值
//定义一个num的int型变量不赋值
int num;
num = 200;
System.out.println(num);
} }
} }