Java-基础(1)-数据类型与变量
1.基本数据类型

基本类型变量存的是值本身,引用类型变量存的是对象的地址。
1. 1整数类型
用于存储整数,默认为 int 类型。
| 类型 | 大小 | 取值范围 | 默认值 | 示例 |
|---|---|---|---|---|
| byte | 8 位 (1字节) | -128 ~ 127 | 0 | byte b = 100; |
| short | 16 位 (2字节) | -32,768 ~ 32,767 | 0 | short s = 1000; |
| int | 32 位 (4字节) | -2³¹ ~ 2³¹-1 (约 -21亿 ~ 21亿) | 0 | int i = 100000; |
| long | 64 位 (8字节) | -2⁶³ ~ 2⁶³-1 | 0L | long l = 100000L; // 建议加 |
1.2 浮点类型
用于存储小数,默认为 double 类型。
| 类型 | 大小 | 取值范围大约 | 默认值 | 示例 |
|---|---|---|---|---|
| float | 32 位 (4字节) | ±3.4E+38 (约 7 位有效小数位) | 0.0f | float f = 3.14f; // 必须加 F 或 f 后缀 |
| double | 64 位 (8字节) | ±1.7E+308 (约 15 位有效小数位) | 0.0d | double d = 3.14159; |
1.3 字符类型
用于存储单个字符。
| 类型 | 大小 | 取值范围 | 默认值 | 示例 |
|---|---|---|---|---|
| char | 16 位 (2字节) | Unicode 字符,\u0000 ~ \uffff | '\u0000' | char c = 'A'; |
1.4 布尔类型
用于表示真/假。
| 类型 | 大小 | 取值范围 | 默认值 | 示例 |
|---|---|---|---|---|
| boolean | 理论上1位,JVM依赖 | true, false | false | boolean flag = true; |
2.变量
2.1变量概念
变量是程序中存储数据的基本单元。
变量的三要素
变量名 (Identifier):变量的名称,用于访问数据。
数据类型 (Data Type):决定变量可以存储什么类型的数据以及占多大空间。
值 (Value):变量中存储的具体数据。
2.2语法格式
定义变量的语法格式为:
数据类型 变量名 = 初始值;
int count = 10; // 整数
double pi = 3.14159; // 浮点数
char grade = 'A'; // 字符
boolean isJavaFun = true; // 布尔值
String message = "Hello World"; // 字符串
2.3变量的命名规则
可以包含:字母(a-z, A-Z)、数字(0-9)、下划线(_)、美元符号($)
必须以:字母、下划线(_)或美元符号($)开头
区分大小写:
age和Age是两个不同的变量不能是关键字:不能使用
int,class,public等 Java 关键字不能有空格
2.4四种基本数据类型变量
2.4.1整型变量
整型变量用于存储整数值(没有小数部分的数字)。
public class IntegerExample {
public static void main(String[] args) {
// byte 类型
byte smallNumber = 100; // -128 到 127
byte maxByte = 127;
// short 类型
short mediumNumber = 30000; // -32768 到 32767
short minShort = -32768;
// int 类型(最常用)
int age = 25; // 年龄
int count = 1000; // 计数
int population = 1400000000; // 14亿
// long 类型(大数值需要加 L 后缀)
long bigNumber = 10000000000L; // 100亿,必须加 L
long worldPopulation = 7800000000L; // 78亿
long timestamp = System.currentTimeMillis(); // 时间戳
System.out.println("byte: " + smallNumber);
System.out.println("int: " + age);
System.out.println("long: " + bigNumber);
// 数值运算
int a = 10;
int b = 20;
int sum = a + b; // 30
int product = a * b; // 200
// 不同整型运算时的自动类型提升
byte byteVal = 10;
int intVal = 20;
int result = byteVal + intVal; // byte 会自动提升为 int
}
}
long类型的大数值必须加L后缀(建议用大写 L,避免与数字 1 混淆)整型运算时,小类型会自动提升为大类型
2.4.2浮点型变量
浮点型变量用于存储带小数点的数值。
public class FloatExample {
public static void main(String[] args) {
// float 类型(必须加 f 后缀)
float price = 19.99f; // 价格
float weight = 65.5f; // 体重
float piApprox = 3.14159f; // 圆周率近似值
// double 类型(默认,可加 d 后缀或不加)
double precisePi = 3.141592653589793; // 精确圆周率
double distance = 384400.5; // 地月距离(公里)
double bankBalance = 1234567.89;// 银行余额
// 科学计数法表示
double largeNumber = 1.23e6; // 1.23 × 10^6 = 1230000
double smallNumber = 2.5e-3; // 2.5 × 10^-3 = 0.0025
System.out.println("float: " + price);
System.out.println("double: " + precisePi);
System.out.println("科学计数法: " + largeNumber);
// 浮点数运算
double x = 10.5;
double y = 3.2;
double sum = x + y; // 13.7
double division = x / y; // 3.28125
// 注意浮点数的精度问题
System.out.println(0.1 + 0.2); // 输出 0.30000000000000004
}
}
float类型必须加f后缀
double是默认的浮点类型,精度更高浮点数是一个近似值,不是一个精确值。
2.4.3字符型变量
字符型变量用于存储单个字符。
public class CharExample {
public static void main(String[] args) {
// 用单引号表示字符
char symbol = '$'; // 符号
char digitChar = '5'; // 数字字符(不是数字5)
// Unicode 转义序列
char unicodeChar = '\u0041'; // 'A' 的 Unicode
char chineseChar = '\u4E2D'; // '中' 字
char heart = '\u2764'; // ❤ 心形符号
// 特殊转义字符
char tab = '\t'; // 制表符
char newline = '\n'; // 换行符
char backslash = '\\'; // 反斜杠
char singleQuote = '\''; // 单引号
System.out.println("字母: " + grade);
System.out.println("中文: " + chineseChar);
System.out.println("特殊符号: " + heart);
// 字符运算(基于 Unicode 编码)
char letterA = 'A';
char letterB = (char)(letterA + 1); // 'B'
int unicodeValue = '中'; // 20013
System.out.println("A 的下一个字母: " + letterB);
System.out.println("'中'的Unicode值: " + unicodeValue);
}
}
使用单引号
''表示字符,双引号""表示字符串一个
char变量只能存储一个字符基于 Unicode 编码,支持多语言字符
c语言中使用ASCII表示字符,而java中使用Unicode表示字符,因此一个字符占用两个字节。
2.4.4布尔型变量
布尔型变量用于表示逻辑值:真或假。
public class BooleanExample {
public static void main(String[] args) {
// 布尔变量声明和初始化
boolean isJavaFun = true; // Java 有趣吗?是
boolean isRaining = false; // 现在下雨吗?否
// 默认值
boolean defaultBoolean; // 局部变量必须初始化
// System.out.println(defaultBoolean); // 错误:未初始化
// 类中的布尔变量有默认值 false
Test test = new Test();
System.out.println("默认值: " + test.flag);
// 布尔运算
int age = 25;
boolean isAdult = (age >= 18); // true
boolean canVote = isAdult && hasLicense; // true && true = true
int score = 85;
boolean isExcellent = (score >= 90); // false
boolean isPass = (score >= 60); // true
// 条件判断
if (isJavaFun) {
System.out.println("Java 很有趣!");
}
if (!isRaining) {
System.out.println("天气晴朗,可以出门");
}
System.out.println("是成年人: " + isAdult);
System.out.println("可以投票: " + canVote);
}
}
class Test {
boolean flag; // 实例变量,默认值为 false
}
取值只能是
true或false,不能是 0 或 1局部布尔变量必须初始化后才能使用
主要用于条件判断和逻辑运算
2.4.5基本类型与包装类型的对应关系
| 基本数据类型 | 包装类型 | |
|---|---|---|
| byte | Byte | |
| short | Short | |
| int | Integer | |
| long | Long | |
| float | Float | |
| double | Double | |
| char | Character | |
| boolean | Boolean |
2.5类型转换
Java 中的类型转换主要分为两大类:
-
自动类型转换(隐式转换) - 小类型转大类型
-
强制类型转换(显式转换) - 大类型转小类型

2.5.1自动类型转换(隐式转换)
编译器自动完成,不需要程序员干预。发生在小类型向大类型转换时。
(1)基本规则
转换方向:
byte → short → int → long → float → double
以及:char → int
public class AutoConversion {
public static void main(String[] args) {
// 1. byte 到 short/int/long/float/double
byte b = 10;
short s = b; // byte → short
int i = b; // byte → int
long l = b; // byte → long
float f = b; // byte → float
double d = b; // byte → double
// 2. short 到 int/long/float/double
short sVal = 100;
int iVal = sVal; // short → int
long lVal = sVal; // short → long
float fVal = sVal; // short → float
double dVal = sVal; // short → double
// 3. char 到 int/long/float/double
char c = 'A';
int charToInt = c; // char → int (Unicode值: 65)
long charToLong = c; // char → long
float charToFloat = c; // char → float
double charToDouble = c; // char → double
// 4. int 到 long/float/double
int intVal = 1000;
long intToLong = intVal; // int → long
float intToFloat = intVal; // int → float
double intToDouble = intVal; // int → double
// 5. long 到 float/double
long longVal = 1000000L;
float longToFloat = longVal; // long → float (可能精度损失)
double longToDouble = longVal; // long → double
// 6. float 到 double
float floatVal = 3.14f;
double floatToDouble = floatVal; // float → double
System.out.println("char 'A' to int: " + charToInt); // 输出 65
}
}
(2)注意事项
public class AutoConversionNotes {
public static void main(String[] args) {
// 表达式中的自动提升
byte a = 10;
byte b = 20;
// byte result = a + b; // 错误!表达式结果自动提升为int
int result = a + b; // 正确
// char参与运算提升为int
char c1 = 'A';
char c2 = 'B';
int charSum = c1 + c2; // 65 + 66 = 131
// 混合类型运算,向最大类型提升
int i = 100;
long l = 200L;
float f = 3.14f;
double result2 = i + l + f; // 全部提升为double
System.out.println("char相加: " + charSum);
System.out.println("混合运算: " + result2);
}
}
2.5.2强制类型转换(显式转换)
需要程序员明确指定,发生在大类型向小类型转换时,可能丢失精度或数据。
(1)基本数据类型的强制转换
语法:
(目标类型) 值
public class ForceConversion {
public static void main(String[] args) {
// 1. double/float → int(丢失小数部分)
double d = 3.14159;
int i = (int) d; // 强制转换为int,结果为3
float f = 2.718f;
int i2 = (int) f; // 结果为2
// 2. long → int(可能溢出)
long l = 2147483648L; // 超过int最大值
int i3 = (int) l; // 溢出,结果为-2147483648
// 3. int → short/byte(可能溢出)
int largeInt = 1000;
short s = (short) largeInt; // 1000在short范围内,安全
byte b = (byte) largeInt; // 溢出,1000 % 256 = -24
// 4. int → char
int num = 65;
char c = (char) num; // 转换为字符'A'
// 5. char → byte/short
char ch = '中'; // Unicode: 20013
byte b2 = (byte) ch; // 溢出,数据丢失
short s2 = (short) ch; // 20013在short范围内,安全
System.out.println("double 3.14159 → int: " + i);
System.out.println("long 2147483648L → int: " + i3);
System.out.println("int 1000 → byte: " + b);
System.out.println("int 65 → char: " + c);
}
}
(2)强制转换的风险和处理
public class ForceConversionRisk {
public static void main(String[] args) {
// 检查是否在目标类型范围内
int value = 1000;
// 转换为byte前的检查
if (value >= Byte.MIN_VALUE && value <= Byte.MAX_VALUE) {
byte safeByte = (byte) value;
System.out.println("安全转换为byte: " + safeByte);
} else {
System.out.println("值 " + value + " 超出byte范围");
}
// 处理浮点数转整型
double price = 19.99;
int intPrice = (int) price; // 直接截断:19
int roundedPrice = (int) Math.round(price); // 四舍五入:20
System.out.println("直接截断: " + intPrice);
System.out.println("四舍五入: " + roundedPrice);
}
}
| 转换类型 | 方式 | 安全性 | 示例 |
|---|---|---|---|
| 自动转换 | 隐式 | 安全 | int i = 10; double d = i; |
| 强制转换 | 显式 | 可能丢失数据 | double d = 3.14; int i = (int)d; |
2.6类型提升

2.6.1一元运算的类型提升
在单操作数运算中,
byte、short、char会自动提升为int。
public class UnaryPromotion {
public static void main(String[] args) {
byte b = 10;
short s = 20;
char c = 'A'; // Unicode 65
// 一元运算:自动提升为int
int result1 = +b; // byte → int
int result2 = -s; // short → int
int result3 = +c; // char → int
// 位运算也提升为int
int bitwise = ~b; // byte → int
System.out.println("+b: " + result1); // 10
System.out.println("-s: " + result2); // -20
System.out.println("+c: " + result3); // 65
System.out.println("~b: " + bitwise); // -11
}
}
2.6.2二元运算的类型提升
(1)如果操作数类型不同,提升为范围更大的类型
public class DifferentTypesPromotion {
public static void main(String[] args) {
int i = 10;
long l = 20L;
float f = 3.14f;
double d = 2.718;
// int + long → long
long result1 = i + l;
// long + float → float
float result2 = l + f;
// float + double → double
double result3 = f + d;
// int + double → double
double result4 = i + d;
System.out.println("int + long: " + result1); // 30
System.out.println("long + float: " + result2); // 23.14
System.out.println("float + double: " + result3); // 5.858
System.out.println("int + double: " + result4); // 12.718
}
}
(2)如果操作数类型相同,结果类型与操作数相同(但至少为int)
public class SameTypePromotion {
public static void main(String[] args) {
byte b1 = 10, b2 = 20;
short s1 = 30, s2 = 40;
char c1 = 'A', c2 = 'B'; // 65, 66
int i1 = 50, i2 = 60;
long l1 = 70L, l2 = 80L;
float f1 = 1.1f, f2 = 2.2f;
double d1 = 3.3, d2 = 4.4;
// byte + byte → int (提升为至少int)
int byteSum = b1 + b2;
// short + short → int (提升为至少int)
int shortSum = s1 + s2;
// char + char → int (提升为至少int)
int charSum = c1 + c2; // 65 + 66 = 131
// int + int → int
int intSum = i1 + i2;
// long + long → long
long longSum = l1 + l2;
// float + float → float
float floatSum = f1 + f2;
// double + double → double
double doubleSum = d1 + d2;
System.out.println("byte + byte: " + byteSum); // 30
System.out.println("short + short: " + shortSum); // 70
System.out.println("char + char: " + charSum); // 131
System.out.println("int + int: " + intSum); // 110
}
}
更多推荐
所有评论(0)