整数类型扩展
进制: 二进制0b 八进制0 十进制 十六进制0x
public class Demo3{
public static void main(String[] args){
int i1 = 10;
int i2 = 010; // 八进制0
int i3 = 0x10; // 十六进制0x 0~9 A~F 16
System.out.printIn(i1); //10
System.out.printIn(i2); // 8
System.out.printIn(i2); // 16
}
}
浮点数类型扩展
银行业务怎么表示?钱
double/float ?
public class Demo3{
public static void main(String[] args){
float f = 0.1f; //0.1
double d = 1.0/10; //0.1
System.out.printIn( f==d ); // fasle
System.out.printIn( f ); // 0.1
System.out.printIn( d ); // 0.1
float d1 = 232323232323232f;
float d2 = d1 + 1;
System.out.printIn( di == d2); //true
}
}
float浮点数精度是有限的,也是离散的,有的数字无法精确表示,会有舍入误差也就是会四舍五入,所以最好完全避免浮点数进行比较
因此,当对数字精确度有很严谨的要求时,float,double都不建议使用。推荐使用BigDecimal数学工具类,可以表示任意精度的小数
字符型类型的扩展
public class Demo1{
public static void main(String[] args){
char c1 = 'a';
char c2 = '中';
System.out.printIn(c1); // a
System.out.printIn((int)c1); // 97
System.out.printIn(c2); // 中
System.out.printIn((int)c2); // 20013
//所有字符都是数字
//Unicod编码 0~ 65536
char c3 = '\u0061'; // Unicod编码 十六进制
System.out.printIn(c3); //a
//转义字符
// \t 水平制表符
// \n 换行符
}
}
字符串型类型的扩展
public class Demo1{
public static void main(String[] args){
String sa = new String("Hello world!");
String sb = new String("Hello world!");
System.out.printIn( sa == sb); //false
// 对象之间比较是比较地址,sa,sb是两个对象。new 说明是新的对象,sa,sb两个不同的对象。所以sa == sb 是错的。
String sc = "Hello World!";
String sd = "Hello World!";
System.out.printIn( sc == sd); // true
// sc = "Hello World!" 在内存中开辟了一个新的地址“Hello World!”地址,sd = "Hello World!",在内存中找是否有表示Hello World!的地址,如果有就表示内存中本来存在的,如果没有就行开辟一个地址
因此,sc == sd 是正确的
}
}
布尔值类型扩展
public class Demo3{
public static void main(String[] args){
boolean flag = true;
if( flag == true) // if与(flag)含义相同
{
}
}
}