// Java 基本数据类型示例
public class BasicDataTypeExample {
public static void main(String[] args) {
// 整数类型
byte byteValue = 127; // 8位,范围: -128 到 127
short shortValue = 32767; // 16位,范围: -32,768 到 32,767
int intValue = 2147483647; // 32位,范围: -2^31 到 2^31-1
long longValue = 9223372036854775807L; // 64位,范围: -2^63 到 2^63-1
// 浮点类型
float floatValue = 3.14f; // 32位,浮点数
double doubleValue = 3.141592653589793; // 64位,双精度浮点数
// 字符类型
char charValue = 'A'; // 16位,Unicode字符
// 布尔类型
boolean booleanValue = true; // 只有两个值:true 和 false
// 打印所有变量的值
System.out.println("byteValue: " + byteValue);
System.out.println("shortValue: " + shortValue);
System.out.println("intValue: " + intValue);
System.out.println("longValue: " + longValue);
System.out.println("floatValue: " + floatValue);
System.out.println("doubleValue: " + doubleValue);
System.out.println("charValue: " + charValue);
System.out.println("booleanValue: " + booleanValue);
}
}
整数类型:
byte
:8位,范围为 -128 到 127。short
:16位,范围为 -32,768 到 32,767。int
:32位,范围为 -2^31 到 2^31-1。long
:64位,范围为 -2^63 到 2^63-1。浮点类型:
float
:32位,单精度浮点数。double
:64位,双精度浮点数。字符类型:
char
:16位,表示一个 Unicode 字符。布尔类型:
boolean
:只有两个值,true
和 false
。以上代码展示了 Java 中的基本数据类型的定义和使用,并通过 System.out.println
方法打印出每个变量的值。
上一篇:java字符串比较
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站