Java 基本数据类型及封装类型详解
Java 提供了8种基本数据类型,每种都有对应的封装类型(包装类)。以下是详细介绍:
1. byte (8位)
java
// 基本类型
byte b1 = 100;
byte b2 = -50;
// 封装类型 - 支持自动装箱
Byte byteObj1 = Byte.valueOf((byte) 100);
Byte byteObj2 = (byte) 100; // 自动装箱
// 使用和打印
System.out.println("byte基本类型 b1: " + b1);
System.out.println("byte封装类型 byteObj1: " + byteObj1);
System.out.println("byte最大值: " + Byte.MAX_VALUE);
System.out.println("byte最小值: " + Byte.MIN_VALUE);
2. short (16位)
java
// 基本类型
short s1 = 1000;
short s2 = -2000;
// 封装类型 - 支持自动装箱
Short shortObj1 = Short.valueOf((short) 1000);
Short shortObj2 = (short) 1000; // 自动装箱
// 使用和打印
System.out.println("short基本类型 s1: " + s1);
System.out.println("short封装类型 shortObj1: " + shortObj1);
System.out.println("short最大值: " + Short.MAX_VALUE);
3. int (32位)
java
// 基本类型
int i1 = 100000;
int i2 = -50000;
// 封装类型 - 支持自动装箱
Integer intObj1 = Integer.valueOf(100000);
Integer intObj2 = 100000; // 自动装箱
// 使用和打印
System.out.println("int基本类型 i1: " + i1);
System.out.println("int封装类型 intObj1: " + intObj1);
System.out.println("int最大值: " + Integer.MAX_VALUE);
4. long (64位)
java
// 基本类型
long l1 = 100000L; // 注意L后缀
long l2 = 123456789012345L;
// 封装类型 - 支持自动装箱
Long longObj1 = Long.valueOf(100000L);
Long longObj2 = 100000L; // 自动装箱
// 使用和打印
System.out.println("long基本类型 l1: " + l1);
System.out.println("long封装类型 longObj1: " + longObj1);
System.out.println("long最大值: " + Long.MAX_VALUE);
5. float (32位)
java
// 基本类型
float f1 = 3.14f; // 注意f后缀
float f2 = 1.23e2f; // 科学计数法
// 封装类型 - 支持自动装箱
Float floatObj1 = Float.valueOf(3.14f);
Float floatObj2 = 3.14f; // 自动装箱
// 使用和打印
System.out.println("float基本类型 f1: " + f1);
System.out.println("float封装类型 floatObj1: " + floatObj1);
System.out.println("float最大值: " + Float.MAX_VALUE);
6. double (64位)
java
// 基本类型
double d1 = 3.14159;
double d2 = 1.23e-4; // 科学计数法
// 封装类型 - 支持自动装箱
Double doubleObj1 = Double.valueOf(3.14159);
Double doubleObj2 = 3.14159; // 自动装箱
// 使用和打印
System.out.println("double基本类型 d1: " + d1);
System.out.println("double封装类型 doubleObj1: " + doubleObj1);
System.out.println("double最大值: " + Double.MAX_VALUE);
7. char (16位)
java
// 基本类型
char c1 = 'A';
char c2 = '\u0041'; // Unicode编码
char c3 = 65; // ASCII码
// 封装类型 - 支持自动装箱
Character charObj1 = Character.valueOf('A');
Character charObj2 = 'A'; // 自动装箱
// 使用和打印
System.out.println("char基本类型 c1: " + c1);
System.out.println("char封装类型 charObj1: " + charObj1);
System.out.println("char数值: " + (int)c1);
8. boolean (1位)
java
// 基本类型
boolean flag1 = true;
boolean flag2 = false;
// 封装类型 - 支持自动装箱
Boolean boolObj1 = Boolean.TRUE;
Boolean boolObj2 = false; // 自动装箱
// 使用和打印
System.out.println("boolean基本类型 flag1: " + flag1);
System.out.println("boolean封装类型 boolObj1: " + boolObj1);
关于 decimal 类型
Java 标准库中没有 decimal 基本数据类型。如果需要高精度的十进制计算,可以使用 BigDecimal 类:
java
import java.math.BigDecimal;
BigDecimal decimal1 = new BigDecimal("10.5");
BigDecimal decimal2 = BigDecimal.valueOf(20.3);
System.out.println("BigDecimal值 decimal1: " + decimal1);
System.out.println("BigDecimal值 decimal2: " + decimal2);
Java BigDecimal 与 SQL Server 类型映射
BigDecimal 可以映射到 SQL Server 的以下类型:
decimal- 最常用映射,完全匹配numeric- 与 decimal 等价,可互换使用money- 用于货币值存储smallmoney- 用于较小范围的货币值
这种映射保证了在数据库操作中数值的精确性和完整性,特别适用于金融计算等对精度要求极高的场景。