package www.base;
/** 字节和位
* 1个字节 = 8 位 1byte=8bit 11001100
* 1bit 1位
* 1Byte 1字节
* 1024B=1KB
* 1024KB=1M
* 1024M=1G
*/
/** 基本数据类型 Primitive Type
* 整数 byte(1字节) < short(2字节) < int(4字节) < long(8字节)
* 小数 float < double
* 字符 char(2字节) / string是类,不是数据类型
* 对错 boolean(1位) <true/false>
*/
/** 引用类型 reference type
* 类 String
* 接口
* 数组
*/
public class Demo001_DataType {
public static void main(String[] args) {
long num1 = 10;
float num2 = 10;
char name1 = 'a';
String name2 = "a";
System.out.println(num1+" "+num2+" ");
System.out.println(name1+" "+name2+" "+name1==name2);
/**
* 整数拓展:进制
* 二进制 0b 十进制 八进制0 十六进制0x
*/
int i0 = 0b10;System.out.println(i0); //二进制
int i1 = 10;System.out.println(i1); //十进制
int i2 = 010;System.out.println(i2); //八进制
int i3 = 0x10;System.out.println(i3); //十六进制
System.out.println("//=====================================");
/**
* 浮点数拓展
* float 有限 离散 舍入误差 大约 接近但不等
* double
* 最好不要使用浮点数进行比较
*/
float f = 0.1f;
System.out.println(f);
double d = 1/10d;
System.out.println(d);
System.out.println(f==d); //false
float f1 = 123123123123123123123f;
float f2 = f1 + 1;
System.out.println(f1==f2); //true
System.out.println("//=====================================");
/**
* 字符本质还是数字,所以可以转换字符到数字
* Unicode 编码表: 2字节 0-65536
* U0000 UFFFF
*/
char c1 = 'a';
System.out.println(c1 + " "+(int)c1);//强制转换字符转数字
char c2 = '好';
System.out.println(c2+" "+(int)c2);//强制转换字符转数字
char c3 = '\u0061';
System.out.println(c3);
System.out.println("//=====================================");
/**
* 转义字符
* \t 制表符
* \n 换行
*/
System.out.println("hello\tWorld");
System.out.println("Oh\nSee you again");
System.out.println("//=====================================");
/**
* 字符串比较
*/
String s1 = new String("hello world");
String s2 = new String("hello world");
System.out.println(s1==s2); //false, 对象 从内存分析
String s3 = "hello world";
String s4 = "hello world";
System.out.println(s3==s4); //true
System.out.println("//=====================================");
/**
* 布尔值扩展
*/
boolean flag = true;
if (flag){
System.out.println("ok, true");
};
if (flag==true){
System.out.println("ok, well done");
};
}
}