基本数据结构总共有八种,我将针对其简单应用做个总结。
1.整型int: 4个字节
2.短整型short: 2个字节
3.长整型long: 8个字节
4.单精度浮点类型float:4个字节
5.双精度浮点类型double:8个字节
public static void main1(String[] args) {
int a = 10;
long b = 20L;//8
double c = 12.5;//8
System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("c = " + c);
}
public static void main(String[] args) {
//短整型 short 2 -32768 - 32767
short s = 32767;
System.out.println(s);
System.out.println(Short.MAX_VALUE);
}
public static void main3(String[] args) {
int a = 1;
int b = 2;
//两个整形若相除,并不会保存小数;
System.out.println(a/b);//0
double c = 1.1;
double d = 1.1;
System.out.println(c*d);//1.2100000000002
//程序中小数存储方式不一样;
}
public static void main4(String[] args) {
float e = 12.5f;
System.out.println(e);//12.5
double f = 1.0D;
System.out.println(f);//1.0
//优先使用double
}
6.字符型char:2个字节(整型)
public static void main5(String[] args) {
//字符型
//java中字符型为两个字节,Unicode字符集;> C中ASCII码字符集
char c = 'a';
char d = '高';
System.out.println(c);
System.out.println(d);
int f = c;
System.out.println(f);//97 (a = 97);
}
7.字节类型byte:1个字节(整型)
public static void main5(String[] args) {
//字节类型 byte 1 -2^7 - 2^7-1
//-128 127
//
//总结来说,给变量赋值的时候,赋值的值不能够超过
//其能够表达的范围
byte b = 127;
System.out.println(b);
//如果进行直接赋值,就是把一个越界的数字给对应的类型
//这种情况下会报错。
int c = 214748364;
System.out.println(c);
int a = Integer.MAX_VALUE+1;
System.out.println(a);
System.out.println(Integer.MAX_VALUE+1);
}
8.布尔类型:没有明确规定大小,只有 true 和 false 。
public static void main6(String[] args) {
//布尔类型 true false
boolean b = true;
System.out.println(b);
}
每一个程序都只有一个main入口,若不实行,可修改main函数的名称,如上图。