JAVA基础语法:数据类型拓展
整数
- 二进制 0b
- 八进制 01
- 十进制
- 十六进制 0x
int i = 10; //输出结果10
int i2 =010; //输出结果8
int i3 =0x10;//输出结果16
int i4 =0b10;//输出结果2
System.out.println("=========================");//表输出下滑线
浮点数
-
float
特点:有限、、离散、舍入误差、大约、接近但不等于。
-
double
根据下面代码可以得出,最好完全避免使用浮点数进行比较。
float f = 0.1f; //结果为0.1
double d = 0.1; //结果为0.1
System.out.println(f==d); //输出结果为false
float f1 = 151513854684153f;
flaot f2 = f1+1;
System.out.println(f1==f2);//输出结果为true
字符
- 强制转换:所有字符本质还是数字
- Unicode(又称统一码、万国码、单一码)是计算机科学领域里的一项业界标准,包括字符集、编码方案等。
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 属于强制转换
char c3= '\u0061';
System.out.println(c3);//输出结果为a
转义字符
public class demo4 {
public static void main(String[] args) {
System.out.println("Hello\tword");//输出结果为 Hello word
System.out.println("Hello\nword");/*输出结果为 Hello
word*/
System.out.println("Hello\bword");//输出结果为 Hellword
System.out.println("Hello\rword");//输出结果为 word
System.out.println("Hello\\word");//输出结果为 Hello\word
System.out.println("Hello\0word");//输出结果为 Hello word
System.out.println("Hello\'word");//输出结果为 Hello'word
System.out.println("Hello\fword");//输出结果为 Helloword
}
}
对象
- 从内存分析
String sa = new String("hello world");
String sb = new String( "hello world");
System.out.println(sa==sb);//输出结果 fales
String sc = "hello world";
String sd = "hello world";
System.out.println(sc==sd);//输出结果 true
布尔值
- Less is More 最少及更多,代码要精简易读
boolean flag = true;
if (flag==true){}
if (flag){} //两种表示方式意思相同