强类型语言
要求变量的使用要严格符合规定,所有变量都必须先定义后才能使用
优点:安全性高、速度性慢
字节
位(bit):是计算机内部数据储存的最小单位,11001100是一个八位二进制数
1bit表示1位
1Byte表示一个字节 1B = 8b
1024B = 1KB
1024KB = 1M
1024M = 1G
扩展
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); //10
System.out.println(i2); //8
System.out.println(i3); //16
System.out.println("=============================");
// 浮点数扩展? 银行业务怎么表示? 钱: BigDecimal
// float 有限 离散 舍入误差 大约 接近但不等于
// double
// 最好完全使用浮点数进行比较
float f = 0.1f;
double d = 1.0/10;
//前面打一个红点就是调试了,运行时在红点处停止,点Debug
System.out.println(f == d); //false
System.out.println(f);
System.out.println(d);
float d1 = 231313123112312313f;
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);
//所有的字符本质还是数字,char涉及到编码问题
//编码:Unicode表,2字节,0~65536,最早的Excel只有2**16=65536
// 从U0000到UFFFF
char c3 = '\u0061';
System.out.println(c3); //a
System.out.println("=============================");
//转义字符
// \t 制表符
// \n 换行
System.out.println("Hello\t\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){}
if (flag == true){}
// Less is More! 代码要精简易读
}
}
强制类型转换
public class Demo06 {
public static void main(String[] args){
//操作比较大的数的时候,注意溢出问题
//JDK7新特性,数字之间可以用下划线分割
int money = 10_0000_0000;
int years = 20;
int total = money * years;
System.out.println(money);
System.out.println(total); //-1474836480,计算的时候溢出了
long total2 = money * years; //默认是int
System.out.println(total2); //-1474836480,计算的时候先计算好在转换
long total23 = money * ((long)years); //20000000000,先把一个数转换为long
System.out.println(total23);
}
}