java有八种基本数据类型:
四种整数类型:(byte(8位),short(16位),int(32位),long(64位))。
默认整数类型是int.意思是所用字面量都是int类型。如100,200。如果想要让字面量表示long类型就在字面量后加l。如:100l(此时字面量占64位。前面有0填充)。
类型转换:范围小的能自动转为范围小的。范围大的需要强制转换才能表示范围小的(保证准确性)。
关于默认整数类型。编译器有个骚操作令我实在看不懂。。。
//编译错误。不能从int变为byte。
//byte test1=1000;
//这样居然能编译通过
byte test2=10;
//我觉得正确的写法不该是这样吗?
byte test2=(byte)10;
只能理解为不超过表示范围就可以将int类型的数赋值给范围更小的。
两种浮点数类型(float、double)
float:32 位。double:64位(二进制表示方法要讲的话太麻烦)
浮点数字面量默认都为 double 类型,所以在定义的 float 型数据后面加F 或 f 。浮点数字面量要有一个小数点。
(ps:long是可以自动转换为float的,因为float能表示的数要比long多。具体原因在还是要看浮点数的表示方式。这是一门专门的课程,叫计算机系统基础) 。
一种字符类型(char):16位。用的是0~2^16-1(65535)的Unicode 码代表字符。
char test1='a';
char test2='b';
//输出a。
System.out.println(test1);
//输出a与b的Unicode码相加的值。
System.out.println(test1+test2);
**一种布尔类型(boolean):**true 真 和 false 假。
java表达式的转型:
如果表达式有double,float,long,int型的变量或字面量。按照范围最大的转。
float test1=1.1f;
int test2=5;
//报错,不能从float转为long。
//long test3=test1+test2;
float test3=test1+test2;
//其实double test=5+1.1f也行。就是精度会存在问题。
float test=5+1.1f;
如果表达式有byte,short,char型的数据类型,全部转换为int。
byte test1=5;
short test2=4;
//test1+test2的类型是int。也可以用int上的类型接收
//如long,float。当然会存在自动转型。
float test3=test1+test2;
前面两种情况在转型时会有一点小小的不同。
//这样是对的。
short test1=5;
//这样却不对。1.2f也没有超过表示范围。
//long test2=1.2f;
所以表现在运算转型上也会不同:
byte test1=10;
byte test2=10;
//报错,int不能自动向short转
//byte test3=test1+test2;
//这个不会报错。因为还没有超过范围。
byte test3=10+10;
//超出范围报错。
byte test4=100+100;
上面出现的情况是int型字面量可以自动反向转型,只要字面量不超过转型后的类型的取值范围
但是这种情况在double,float ,int,long上不会出现。
//报错。double不能自动转为float
float test=1.1+1.2;
//报错。
int test1=10l;
final修饰的的基本数据类型不会转型。
final byte test1=1;
final byte test2=2;
byte test3=test1+test2;//test1和test2不会变成int。
(用final修饰的int,short,byte会发生一些更奇怪的现象。)
java包装类:
个人认为:基本数据类型与java这种面向对象编程的风格格格不入。基本数据类型的存在或许只是为了方便。严格按照万物皆是对象的思想来看的话,六种基本数据类型其实是被抽象为Number类的。char型与boolean自立门户。封装成了自己的类。
基本数据类型与包装类的对应关系:
基本数据类型 | 包装类 |
---|---|
byte | Byte |
short | Short |
int | Integer |
long | Long |
char | Character |
float | Float |
double | Double |
boolean | Boolean |
自动装箱和自动拆箱:
Integer a=100;//装箱操作,将100变成了对象。相当与执行了Integer a=new Integer(100);,具体调用了valueOf()
Integer a1=Integer.valueOf(100);
int b=a;//拆箱操作,相当与执行了int b=a.intValue();
int b1=a.intValue();
//下面要测试le
Integer c1=127;
Integer c2=127;
Integer c3=128;
Integer c4=128;
Integer c5=new Integer(127);
Integer c6=new Integer(127);
System.out.println(c1==c2);//true
System.out.println(c3==c4);//false
System.out.println(c5==c6);//false
/*解释:当值位于[-128,127]之间时,Integer.valueof()会直接返回intege缓存数组中相应对象的应用。
* 不在这个区间时才会重新创建对象。(byte,short,Long,character)都一样。,float,double永远返回新的对象
* (因为小数是无限的)。
* */
下面的代码多说两句。
Integer a=100;//装箱操作,将100变成了对象。相当与执行了Integer a=new Integer(100);,具体调用了valueOf()
这句代码并没有执行Integer a=new Integer(100);自动装箱执行的Integer.valueOf(100);
包装类的equals方法和“==”
int test1=1;
long test2=1;
System.out.println(test1==test2);//true
Integer test3=1;
Short test4=1;
System.out.println(test3==test4);//直接报错,不同类型的对象无法用“==”连接
System.out.println(test1==test3);//true.自动拆箱。包装类和基本数据类型比较时会自动拆箱。
不存在数据类型的自动转换(byte,short这两个异类除外)。
//前面说到小范围可以自动转为大范围,但是在包装类中就不会转
long test=100;
Long test=100;//报错,int不能转为Long。
Long test=(long)100;//强转。
//但是这样又可以
Short test1=100;//没超过表示范围。
equals方法:
源码:
public boolean equals(Object obj) {
if (obj instanceof Integer) {
return value == ((Integer)obj).intValue();
}
return false;
}
首先,同类型或是有父子关系且值相等才能返回true。比较过程也是先转型再拆箱。
Integer test1=10;
Short test2=10;
System.out.println(test1.equals(test2));//false
System.out.println(test1.equals(10));//true,10应该会进行装箱。