1.java的数据类型
基本类型(8种)
引用类型
2.8种基本类型
Byte |
1 |
-128~127 -2^7~2^7-1 |
Short |
2 |
-2^15~2^15-1 |
Int |
4 |
-2^31~2^31-1 |
Long |
8 |
-2^63~2^63-1 |
Float |
4 |
单精度IEEE-754规范(美国电子电气工程师协会) |
Double |
8 |
双精度 |
Char |
2 |
字符,或字符的整数编码 0~65535 0~2^16-1 |
Boolean |
1 |
布尔值 真 true 假 false |
基本类型
package day0201; public class Test1 { public static void main(String[] args) { //定义8个变量,保存4种整数的最大值和最小值 byte a = -128; byte b = 127; //jdk类库种的java.short.Short //保存着short类型的最小值 short c = Short.MIN_VALUE; short d = Short.MAX_VALUE; int e = Integer.MIN_VALUE; int f = Integer.MAX_VALUE; long g = Long.MIN_VALUE; long h = Long.MAX_VALUE; System.out.println(a); System.out.println(b); System.out.println(c); System.out.println(d); System.out.println(e); System.out.println(f); System.out.println(g); System.out.println(h); } }
变量交换
import java.util.Scanner; public class Test1 { public static void main(String[] args) { System.out.print("输入整数a:"); int a = new Scanner(System.in).nextInt(); System.out.print("输入整数b:"); int b = new Scanner(System.in).nextInt(); int c = a; a = b; b = c; System.out.println("a ="+a); System.out.println("b ="+b); } }
浮点数
package day0201; public class Test2 { public static void main(String[] args) { //a b c d 4个变量保存浮点数的最小/大值 float a = Float.MIN_VALUE; float b = Float.MAX_VALUE; double c = Double.MIN_VALUE; double d = Double.MAX_VALUE; System.out.println(a); System.out.println(b); System.out.println(c); System.out.println(d); } }
自由落体距离
package day0203; import java.util.Scanner; public class Test1 { public static void main(String[] args) { System.out.print("输入下落时间(秒):"); double t = new Scanner(System.in).nextDouble(); double d = 0.5 * 9.8 * t * t; System.out.println(t + "秒,下落了" + d + "米"); } }
Char
unicode编码的字符
0 |
|
1 |
|
2 |
|
… |
|
65 |
A |
66 |
B |
… |
|
97 |
a |
98 |
b |
20013 |
中 |
65535 |
|
范围0~65535
Char c1 = ‘a’;
Char c1 = 97;
CHAR
package day0201;
public class Test3 {
public static void main(String[] args) {
char c1 = 'a';
char c2 = 97;
char c3 = 'b';
char c4 = 98;
char c5 = '中';
char c6 = 20013;
System.out.println(c1);
System.out.println(c2);
System.out.println(c3);
System.out.println(c4);
System.out.println(c5);
System.out.println(c6);
System.out.println('刘'+0);
System.out.println('立'+0);
System.out.println('博'+0);
}
}
3.基本类型的字面值规则(5条)
(1)整数字面值:int
int a =6364; Long a = 99999999999;//右侧是int类型超出int范围
(2)byte/short/char 三种比int小的整数类型
可以用范围内的值,直接赋值
Byte a =127;//右侧是byte类型 Byte a = 128;//超出范围,右侧是int类型
(3)浮点数字面值是double
Double a = 3.14; Float a = 3.14;//右侧是double类型
(4)字面值后缀
L long Long A = 999999999L; F float Float a = 3.14F; D double Double a = 3.14D;
(5)进制前缀
0x 十六进制
0 八进制
\u char类型 16进制
System.out.println('\u0061'); System.out.println('\u0062'); System.out.println('\u5218'); System.out.println('\u7acb'); System.out.println('\u535a');