数据类型
强类型语言
- 要求变量的使用要严格符合规定,所有变量都必须先定义后才能使用
弱类型语言
java的数据类型分为两大类
- 基本类型(primitive type)
- 引用类型(reference type)
public class Demo02 {
public static void main(String[] args) {
//八大基本数据类型
//整数
byte num1 = 20; //最常用
short num2 = 30;
int num3 = 10;
long num4 = 30L; //Long类型要在数字后面加个L
//小数:浮点数
float num5 = 50.1F; //float类型要在数字后面加个F
double num6 = 3.14159265358;
//字符
char name ='a'; //单引号!!!!
//字符串,String不是关键字,是类
//String namea = "兰琦";
//布尔值:是非
boolean flag = true;
//boolean flag = false;
}
}
字节
-
位(bit):是计算机内部数据储存的最小单位,11001100是一个八位二进制数。
-
字节(byte):是计算机中数据处理的基本单位,习惯上用大写B来表示
-
1B=8bit
-
字符:是值计算机中使用的字母、数字、字和符号
-
1bit表示1位
-
1Byte表示一个字节,1B=8b
-
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)
System.out.println(i); //10
System.out.println(i2); //8
System.out.println(i3); //16
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 = 123123123123f;
float d2 = d1+1;
System.out.println(d1==d2); //true
//=================================================
//字符拓展
//=================================================
System.out.println("=============================");
char c1 ='a'; //单引号!!!!
char c2 ='中'; //单引号!!!!
System.out.println(c1); //a
System.out.println((int)c1);//强制换行 //97
System.out.println(c2); //中
System.out.println((int)c2);//强制换行 //20013
//所有的字符本质还是数字
//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 sc = "hello world";
String sd = "hello world";
System.out.println(sc==sd); //true
//对象 从内存
//布尔值拓展
boolean flag = true;
if (flag==true){} //新手
if (flag){} //老手
//Less is More! 代码要精简易读
}
}