Java2基础
强类型语言
变量的使用要严格符合规定,所有变量都必须先定义后才能使用
public class Demo02 {
public static void main(String[] args) {
String a = "hello";
int num = 10;
System.out.println(a);
System.out.println(num);
}
}
弱类型语言
Java的数据类型分为两大类
基本类型(Primitive Type)
| 类型 | 子类型 | 类名 | 字节 | 范围 |
|---|---|---|---|---|
| 数值类型 | 整数类型 | byte | 1个字节 | -128-127 |
| short | 2个字节 | -32768-32767 | ||
| int | 4个字节 | -2147483648-2147483647 | ||
| long | 8个字节 | -9223372036854775808-9223372036854775807 | ||
| 浮点类型 | float | 4个字节 | ||
| double | 8个字节 | |||
| 字符类型 | char | 2个字节 | ||
| boolean类型 | 只有true和false | 1位 |
public class Demo02 {
public static void main(String[] args) {
//八大基本数据类型
int num1 = 10;//最常用
byte num2 = 20;
short num3 = 30;
long num4 = 30L;//Long类型要在数字后加个L
//小数:浮点数
float num5 = 50.1F;//float类型要在数字后加个F
double num6 = 3.141592653589793238462643;
//字符
char name = '国';
//字符串,String不是关键字,类
//String namea = "老铁";
//布尔值:是非
boolean flag = true;
//boolean flag = false;
}
}
引用类型(Reference Type)
| 类 | 接口 | 数组 |
|---|
字节
位(bit):是计算机 内部数据 储存的最小单位,11001100是一个八位二进制数
字节(byte):是计算机中 数据处理 的最小单位,习惯上用大写B表示
1B(byte,字节)= 8bit(位)
字符:是指计算机中使用的字母、数字、字和符号
-
1bit表示1位
1Byte表示一个字节
1024B=1KB
1024KB=1M
1024M=1G
1024G=1TB
数据类型拓展
public class Demo03 {
public static void main(String[] args) {
//整数拓展: 进制 二进制0b 十进制 八进制0 十六进制0x
int i = 10;
int i2 = 010;//八进制0
int i3 = 0x10;//十六进制0x 0~9 A~F 16
System.out.println(i);
System.out.println(i2);
System.out.println(i3);
System.out.println("========================================");
//=============================================
//浮点数拓展: 银行业务表示:钱
//BigDecimal 数学工具类
//=============================================
//float 有限 离散 舍入误差 大约 接近但不等于
//double
//最好完全使用浮点数进行比较
//最好完全使用浮点数进行比较
//最好完全使用浮点数进行比较
float f = 0.1f; //0.1
double d = 1.0/10; //0.1
System.out.println(f==d); //false
float d1 = 23221313211312f;
float d2 = d1 + 1;
System.out.println(d1==d2); //true
System.out.println("========================================");
//=============================================
//字符拓展
//=============================================
char c1 = 'a';
char c2 = '中';
System.out.println(c1);
System.out.println((int)c1);//强制转换
System.out.println(c2);
System.out.println((int)c2);//强制转换
//所有的字符本质还是数字
//编码 Unicode 表:(97=a、65=A) 2字节 65536
//U0000 UFFFF
char c3 = '\u0061';
System.out.println(c3); //a
//转义字符
//\t 制表符
//\n 换行
//.....
System.out.println("Hello\nWorld");
System.out.println("========================================");
String sa = new String("hello world");
String sb = new String("hello world");
System.out.println(sa==sb);//false
String