数据类型拓展及面试题
-
进制
a.二进制 0b开头
b.十进制
c.八进制 0开头
d.十六进制 0x开头 0到9 A到F
-
举例:
int i=10
int i=010
int i=0x10
public class Demo03 {
public static void main(String[] args) {
//整数拓展
int i=10;
int i2=010;//八进制
int i3=0x10;//十六进制
System.out.println(i);
System.out.println(i2);
System.out.println(i3);
//================================================================================
//浮点数拓展 银行业务怎么表示
//================================================================================
//float 和double 拓展
//float 表示的数值有限 表示的是离散的 会舍入误差 是大约数 接近但不等于
//最好完全避免使用浮点数进行比较!!!
//最好完全避免使用浮点数进行比较!!!
//最好完全避免使用浮点数进行比较!!!
//BigDecimal 数学工具类
System.out.println("================================================================================");
float f=0.1f; //结果0.1
double d=1.0/10;//结果 0.1
System.out.println(f);
System.out.println(d);
System.out.println(f==d);//判断f和d 是否相等 输出结果位false
System.out.println("================================================================================");
float d1=5646543215456f;
float d2=d1+1;
System.out.println(d1==d2);
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编码 excel表格只有:2的16次方 长度 65536
//Unicode 从U0000 UFFFF
//Unicode 在程序里面的转意格式
char c3='\u0061';
System.out.println(c3);
System.out.println("================================================================================");
//转义字符
// \t 中间空格
// \n 换行
System.out.println("hello\tworld");
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){}
}
}
本文详细介绍了Java中的数据类型,包括二进制、八进制、十进制和十六进制的表示方式,并通过实例展示了整数、浮点数的使用。强调了避免使用浮点数进行精确比较的重要性,提到了BigDecimal类作为高精度计算的解决方案。同时,讨论了字符的Unicode编码以及字符串和对象的比较。最后,提醒开发者注意布尔值在条件判断中的常见用法。
188

被折叠的 条评论
为什么被折叠?



