变量和基本数据类型
变量
保存数据的存储单位(容器)
声明变量的语法: 类型 变量名称
int i;
int number = 123;
String name = "Java";
String str;
基本数据类型
原始数据类型。系统事先规定好的类型(8个)
整数、浮点数、字符类型、布尔型
- boolea (逻辑类型)
- byte (整数类型)
- char (字符类型)
- short (整数类型)
- int (整数类型)
- long (整数类型)
- float (浮点数类型)
- double (浮点数类型)
关于Java的8种基本数据类型,其名称、位数、默认值、取值范围及示例如下表所示:
序号 | 数据类型 | 位数 | 默认值 | 取值范围 | 举例说明 |
---|---|---|---|---|---|
1 | byte(位) | 8 | 0 | -2^7 - 2^7-1 | byte b = 10; |
2 | short(短整数) | 16 | 0 | -2^15 - 2^15-1 | short s = 10; |
3 | int(整数) | 32 | 0 | -2^31 - 2^31-1 | int i = 10; |
4 | long(长整数) | 64 | 0 | -2^63 - 2^63-1 | long l = 10l; |
5 | float(单精度) | 32 | 0.0 | -2^31 - 2^31-1 | float f = 10.0f; |
6 | double(双精度) | 64 | 0.0 | -2^63 - 2^63-1 | double d = 10.0d; |
7 | char(字符) | 16 | 空 | 0 - 2^16-1 | char c = 'c'; |
8 | boolean(布尔值) | 8 | false | true、false | boolean b = true; |
整数
byte(位)、short(短整数)、 int(整数)、long(长整数) 范围
System.out.println("byte的最大值 " + Byte.MAX_VALUE);
System.out.println("byte的最小值 " + Byte.MIN_VALUE);
System.out.println("short 的最大值 " + Short.MAX_VALUE);
System.out.println("short 的最小值 " + Short.MIN_VALUE);
System.out.println("int 的最大值 " + Integer.MAX_VALUE);
System.out.println("int 的最小值 " + Integer.MIN_VALUE);
System.out.println("long 的最大值 " + Long.MAX_VALUE);
System.out.println("long 的最小值 " + Long.MIN_VALUE);
控制台结果:
byte的最大值 127
byte的最小值 -128
short 的最大值 32767
short 的最小值 -32768
int 的最大值 2147483647
int 的最小值 -2147483648
long 的最大值 9223372036854775807
long 的最小值 -9223372036854775808
整数的上溢或下溢
// 上溢或下溢
int int1 = Integer.MAX_VALUE + 1; // 上溢 变成int的最小值
System.out.println("int 类型的值上溢: " + int1);
int int2 = Integer.MIN_VALUE - 1; // 下溢 变成int的最大值
System.out.println("int 类型的值下溢: " + int2);
控制台结果:
int 类型的值上溢: -2147483648
int 类型的值下溢: 2147483647
浮点数
float(单精度浮点小数)、double(双精度浮点小数) 范围
float float1 = 234.234f;//四个字节
float float2 = 24.234F;
System.out.println("float 的最大值 " + Float.MAX_VALUE);
System.out.println("float 的最小值 " + Float.MIN_VALUE);
double double1 = 123.05d; // 8个字节
double double2 = 1.33568e2D;
System.out.println("double 的最大值 " + Double.MAX_VALUE);
System.out.println("double 的最小值 " + Double.MIN_VALUE);
控制台输出:
float 的最大值 3.4028235E38
float 的最小值 1.4E-45
double 的最大值 1.7976931348623157E308
double 的最小值 4.9E-324
浮点数的上溢或下溢
float negativeInfinity = Float.NEGATIVE_INFINITY;// 负无穷大
float positiveInfinity = Float.POSITIVE_INFINITY; // 正无穷大
float naN = Float.NaN; // 分母为0
System.out.println("负无穷大 " + negativeInfinity);
System.out.println("正无穷大 " + positiveInfinity);
System.out.println("不是一个数字 " + naN);
控制台结果:
负无穷大 -Infinity
正无穷大 Infinity
不是一个数字 NaN
布尔型数据(逻辑)
true(真)、false(假)
boolean b; // 包装类Boolean 默认值为false
boolean b1,b2,b3;
boolean b5 = true , b6 = false;
字符型
char 字符类型
har char1 = 'A'; // 两个字节
char char2 = '汉';
char char3 = '@';
char char回车 = '\r';
char char单引号 = '\''; // 转义符
char char反斜线 = '\\';
System.out.println("A的编码 " + (int)char1);
System.out.println("a的编码 " + (int)'a');
System.out.println("A的编码 " + (int)char2);
int A = char1; // 字符char可以转换成int
char1 = (char)A; // int 转char 必须强制转换
A = char1 + int1; // char 类型可以隐式转换成int类型
控制台结果:
A的编码 65
a的编码 97
A的编码 27721
非基本数据类型
由基本数据类型组合成的新类型,例如:数组,字符串、类、接口
网友评论