Java拓展
//面试注意
public class HelloWorld{
public static void main(String[] args) {
//整数拓展: 进制 二进制0b 十进制 八进制0 十六进制0x
int i = 10;
int i2 = 010;//八进制0
int i3 = 0x10;//十六进制0x
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
System.out.println(f);
System.out.println(d);
float d1 = 242342353f;
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);//强制类型转换
//所有的字符本质还是数字
//编码 Unicode 2字节 0~65536 Excel的长度为2^16 65536
char c3 ='\u0061';
System.out.println(c3);
//转义字符
// \t 制表符
// \n 换行
//......
System.out.println("Hello\tHello");
//
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){}//老手
}
}
Java类型转换
-
由于java是强类型语言,所以有时候有些运算要用到类型转换
//低------------------------------------------>高 //byte,short,char ->int -> long -> float -> double
-
强制类型转换的实验代码
public class Demo04 { public static void main(String[] args) { int i =128; byte b = (byte)i;//内存溢出() System.out.println(i); System.out.println(b); System.out.println("========================"); //强制转换 (类型)变量名 高-->低 //自动转换 低--->高 int n=128; double m= i; System.out.println(n); System.out.println(m); //注意: /* *1.不能对布尔型进行转换 *2.不能把对象类型转换为不相干的类型 * 3.在把高容量转换到低容量的时候,强制转换 * 4.转换的时候可能存在内存溢出,或者精度问题! */ System.out.println("========================="); System.out.println((int)23.7);//23 System.out.println((int)-45.98f);//-45 System.out.println("========================="); char c='a'; int d=c+1; System.out.println(d); System.out.println((char)d); } } public class Demo05 { public static void main(String[] args) { //操作比较大的数的时候,注意溢出问题 //JDK7新特性,数字之间可以用下划线分割 int money = 1000000000; // int money = 10_0000_0000; int years = 20; int total = money*years;//-14748366480,计算的时候溢出 long total2 = money*years;// long total3 = money*((long)years); System.out.println(total3); } }