八大基本数据类型
public class Demo02 {
public static void main(String[] args) {
//八大基本类型
byte num2 = 10;
int num1 = 20; //最常用
short num3 = 30;
long num4 = 40L; //long类型要在数字后面加L
//小数: 浮点数
float num5 = 50.1F; //float类型要在数字后面加F
double num6 = 3.1415;
//字符
char name = '秦'; //只能写一个字符 多个报错
//布尔值
boolean flag = true;
//boolean flag = falsel;
//String 字符串,String不是关键词,是类
String a = "Hello";
System.out.println(a);
}
}
数据类型拓展
public class Demo03 {
public static void main(String[] args) {
// 整数拓展 进制 二进制0b 十进制 八进制0 十六进制0x
int i = 10;
int i2 = 010;
int i3 = 0x10;
int i4 = 0b10;
System.out.println(i); //二进制0b开头
System.out.println(i2); //八进制0开头
System.out.println(i3); //十六进制0x开头 0~9 A~F F=15
System.out.println(i4); //二进制0b开头
// ===========================================================
// ===========================================================
System.out.println("===========================================");
//浮点数拓展 银行业务怎么表示?
//float 有限 离散的 舍入误差 大约 接近但不等于
//double
//最好完全避免使用浮点数进行比较
//最好完全避免使用浮点数进行比较
//最好完全避免使用浮点数进行比较
float f = 0.1f;
double d = 1.0/10;
System.out.println(f==d);
System.out.println(f);
System.out.println(d);
float f1 = 12312312313f;
float f2 = f1 + 1;
System.out.println(f1 == f2);
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 Excel
//
char c3 = '\u0061'; // 16进制表示 61 == 97
System.out.println(c3);
//转义字符
// \t 制表符
// \n 换行
System.out.println("hello\tworld");
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 sc = ("hello world");
String sd = ("hello world");
System.out.println(sc == sd); // true
// 对象 从内存分析
System.out.println("======================");
//布尔值拓展
boolean flag = true;
if (flag == true){
System.out.println(666);
}
if (flag); {
System.out.println(666);
} //这两个相等
// Less is Mose ! 代码要精简已读
}
}