day8 基础04
数据类型扩展及面试题讲解
public class Demo03 {
public static void main(String[] args) {
// 整数拓展 进制 二进制0b 十进制 八进制0 十六进制0x
int i = 10;
int i2 =010; // 八进制0
int i3 =0x11; // 十六进制0x 0~9 A~F
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 判断f跟d是否一样
float d1 = 23131313313113f;
float d2 = d1 + 1;
System.out.println(d1==d2);//true
System.out.println(f);
System.out.println(d);
//===================================
//字符拓展?
//===================================
System.out.println("====================");
char c1 = 'A';
char c2 = '中';
System.out.println(c1);
System.out.println((int)c1);//强制转换为int类型
System.out.println(c2);
System.out.println((int)c2);//强制转换
//所有的字符本质还是数字
//编码 Unicode 表:(97=a,65=A) 2字节 0~65536 Excel 2的16次方=65536
//U0000 UFFFF
char c3 = '\u0061';
System.out.println(c3);//a 16进制,61是97
//转义字符
// \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);
String sc = "hello world";
String sd = "hello world";
System.out.println(sc==sd);
//对象 从内存分析
//布尔值拓展
boolean flag = true;
if(flag==true){} //新手
if(flag){} //老手
//Less is More! 代码要精简易读
}
}
day9 基础05
类型转换
运算中,不同类型的数据先转化为同一类型,然后进行运算
低---------------------------------------------------高
byte,short,char-->int-->long->float-->double
强制类型转换
自动类型转换
Demo05
public class Demo05 {
public static void main(String[] args) {
int i = 128;
double b = i;//内存溢出
//强制转换 (类型)变量名
//自动转换 低--高
System.out.println(i);
System.out.println(b);
/*
注意点:
1.不能对布尔值进行转换
2.不能把对象类型转换为不相干的类型
3.在把高容量转换到低容量的时候,强制转换
4.转换的时候可能存在内存溢出,或者精度问题!
*/
System.out.println("==========================");
System.out.println((int)23.7); //23
System.out.println((int)-45.89f); //-45
System.out.println("==========================");
char c = 'a';
int d = c+1;
System.out.println(d);
System.out.println((char)d);
}
}
Demo06
public class Demo06 {
public static void main(String[] args) {
//操作比较大的数的时候,注意溢出问题
//jdk7新特性,数字之间可以用下划线分割
int money = 10_0000_0000;
int years = 20;
int total = money*years; //-1474836480,计算的时候溢出了
long total2 = money*years;//默认是int,转换之前已经存在问题了?
long total3 =money*((long)years);//先把一个数转换为long
System.out.println(total3);
//L l
}
}