数据类型
数据类型其实只有两种:基本类型和引用类型
基本类型就是我们平时常用的Java自带的8中类型
引用类型就是我们使用的库或者自定义类型
基本类型
类型名称 | 类型大小(单位:Byte) | 类型范围 |
---|---|---|
byte | 1 | -128~127即-27 ~27 -1 |
short | 2 | -215 ~215 -1 |
int | 4 | -231 ~231 -1 |
long | 8 | -263 ~263 -1 |
float | 4 | 单精度 3.4028235E38~1.4E-45 |
double | 8 | 双精度 4.9E-324~1.7976931348623157E308 |
char | 2 | |
boolean | 1 |
Tip1:这个Byte我就不多补充了奥,就是字节的意思
Tip2:后续可能会上传,也可能不上传,显示范围都会在Java中进行示例演示,可以贴出来
package day01_基本类型;
public class Test01 {
public static void main(String[] args) {
byte show1 = -128;
byte show2 = 127;
short show3 = Short.MAX_VALUE;
short show4 = Short.MIN_VALUE;
int show5 = Integer.MAX_VALUE;
int show6 = Integer.MIN_VALUE;
long show7 = Long.MAX_VALUE;
long show8 = Long.MIN_VALUE;
System.out.println(show1);
System.out.println(show2);
System.out.println(show3);
System.out.println(show4);
System.out.println(show5);
System.out.println(show6);
System.out.println(show7);
System.out.println(show8);
}
}
//结果
/*
-128
127
32767
-32768
2147483647
-2147483648
9223372036854775807
-9223372036854775808
*/
float f1 = Float.MAX_VALUE;
float f2 = Float.MIN_VALUE;
double d1 = Double.MAX_VALUE;
double d2 = Double.MIN_VALUE;
System.out.println(f1);
System.out.println(f2);
System.out.println(d1);
System.out.println(d2);
//结果
/*
3.4028235E38
1.4E-45
1.7976931348623157E308
4.9E-324
*/
存储方式
利用byte类型进行讲解
0000 0000 表示 0
0000 0001 表示 1
0000 0010 表示 2
以此类推
0111 1111 表示 127
这是可能会出现问题,为什么最前面那个0不会变成1?
因为在8位表示中,只有后7位是用来表示数据大小的
而第1位是用来表示数据符号的
即0为+,1为-
并且在存储负值的时候存储方式为
1111 1111 表示 -1
1111 1110 表示 -2
1111 1101 表示 -3
以此类推
1000 0000 表示 -128
char类型详解
char类型表示的是uniconde编码表
编码 | 表示字符 |
---|---|
0 | ? |
1 | ? |
… | … |
65 | A |
66 | B |
… | … |
这玩意儿最经典的就是ASCII码表了,懂得都懂昂,最大的就是65535
PS:ASCII不是unicode的一种昂,这是两种,只是举个例子让大家了解一下
关于整数型和浮点型的小练习
变量交换
变量交换也是很基础的东西了,当我们将两个容器中的物品进行交换时,我们需要第三个容器中进行其中一个物品的暂时存放
所以很简单,我们只需要一个临时的中间变量就可以进行交换
package day01_基本类型;
import java.util.Scanner;
public class Test02 {
public static void main(String[] args) {
//进行变量交换
System.out.print("请输入数字a:");
//利用Scanner包进行变量加入
int a = new Scanner(System.in).nextInt();
System.out.print("请输入数字b:");
int b = new Scanner(System.in).nextInt();
System.out.println("a:" + a);
System.out.println("b:" + b);
int c = a;
a = b;
b = c;
System.out.println("a:" + a);
System.out.println("b:" + b);
}
}
自由落体距离计算
这个东西我们其实就是用公式计算了即 m = 1 2 g t 2 m=\frac {1}{2}gt ^{2} m=21gt2
package day01_基本类型;
import java.util.Scanner;
public class Test03 {
public static void main(String[] args) {
//自由落体距离
System.out.print("输入降落时间:");
double t = new Scanner(System.in).nextDouble();
System.out.println("距离为:" + 9.8*t*t/2);
}
}
关于char类型的练习
验证uniconde编码与表示字符的实际表现
package day01_基本类型;
public class Test04 {
public static void main(String[] args) {
char c1 = 'a';
char c2 = 97;
char c3 = '中';
char c4 = 20013;
char c5 = 20014;
System.out.println("c1:" + c1);
System.out.println("c2:" + c2);
System.out.println("c3:" + c3);
System.out.println("c4:" + c4);
System.out.println("c5:" + c5);
}
}
//显示结果
c1:a
c2:a
c3:中
c4:中
c5:丮