Java review(3) Java类型转换
- boolean 不能转换成其他类型,其他类型也不能转换成boolean
- 其它基本类型之间可以任意进行强制转换
```java
@Test
public void test1() throws Exception {
// boolean 不能转换成其他类型,其他类型也不能转换成boolean
boolean b = false;
System.out.println("------------ char -----------");
char c = 65;
System.out.println(c);
c = '1';
System.out.println(c);
// System.out.println((boolean) c);
System.out.println((char)c);
System.out.println((byte)c);
System.out.println((short)c);
System.out.println((int)c);
System.out.println((long)c);
System.out.println((float)c);
System.out.println((double)c);
// ------------ char -----------
// A
// 1
// 1
// 49
// 49
// 49
// 49
// 49.0
// 49.0
System.out.println("------------ short -----------");
short s = 65;
System.out.println(s);
s = '1';
System.out.println(s);
// System.out.println((boolean) s);
System.out.println((char)c);
System.out.println((byte)s);
System.out.println((short)s);
System.out.println((int)s);
System.out.println((long)s);
System.out.println((float)s);
System.out.println((double)s);
// ------------ short -----------
// 65
// 49
// 1
// 49
// 49
// 49
// 49
// 49.0
// 49.0
System.out.println("------------ byte -----------");
byte bte = 65;
System.out.println(bte);
bte = '1';
System.out.println(bte);
// System.out.println((boolean) bte);
System.out.println((char)c);
System.out.println((byte)bte);
System.out.println((short)bte);
System.out.println((int)bte);
System.out.println((long)bte);
System.out.println((float)bte);
System.out.println((double)bte);
// ------------ byte -----------
// 65
// 49
// 1
// 49
// 49
// 49
// 49
// 49.0
// 49.0
// ... 省略其他变量类型
}
3. 当你使用的转型常量大于该变量最大值时,则需要强制转型。下例中,char最大值是2 ^ 16 -1(无符号),当值大于65535时会要求显式进行转型。转型采用截取低位的方法。
```java
@Test
public void test2() throws Exception {
// char 最大值 65535
System.out.println("char max: " + Math.round(Math.pow(2, 16) - 1));
char a = (char) (65535 + 65);
System.out.println(a);
System.out.println((long) a);
// char max: 65535
// @
// 64
}
低精度向高精度转型无需强制转换,因为不会损失精度
高精度向低精度转型需强制转换,因为会损失精度
Java和c语言不同,在不同系统上它的基础变量类型的位数也是一致的。这也正是它高度移植性的原因。在这里插入图片描述
```java
@Test
public void test3() throws Exception {byte bte = (byte) 126; System.out.println(Byte.toString(bte)); System.out.println(Integer.toBinaryString(bte)); System.out.println(bte); // 向高精度转型无需强制转换 double d = bte; System.out.println(d); d = 1234123412.123421341234; System.out.println(d); // 向低精度转型需强制转换 bte = (byte) d; System.out.println(bte); // 126 // 1111110 // 126 // 126.0 // 1.2341234121234214E9 // -108
}
6. 父类转子类会抛出ClassCastException,子类转父类则可以隐式转换,不会抛出异常
```java
class A {}
class B extends A {}
@Test
public void test4() throws Exception {
A a = new A();
try {
B b = (B) a;
} catch (ClassCastException e) {
System.out.println(e.getMessage());
}
B b = new B();
A aa = b;
// cn.hengyumo.review.r1.Test1$A cannot be cast to cn.hengyumo.review.r1.Test1$B
}
- 实现同一个接口的父子类同样无法直接转型
interface I {}
class D implements I {}
class E extends D implements I {}
@Test
public void test5() throws Exception {
D d = new D();
try {
E e = (E) d;
} catch (ClassCastException e) {
System.out.println(e.getMessage());
}
I i = d;
try {
E e = (E) i;
} catch (ClassCastException e) {
System.out.println(e.getMessage());
}
// cn.hengyumo.review.r1.Test1$D cannot be cast to cn.hengyumo.review.r1.Test1$E
// cn.hengyumo.review.r1.Test1$D cannot be cast to cn.hengyumo.review.r1.Test1$E
}
- 包装类之间可以任意转换,但是基础类型转高精度类型的包装类时无法再进行隐式转换
@Test
public void test6() throws Exception {
int a = 2;
Integer i = a;
int aa = i;
// cannot cast 'int' to 'java.lang.Long'
// Long l = (Long) a;
Long l = (long) a;
long ll = l;
// cannot cast 'int' to 'java.lang.Short'
// Short s = (Short) a;
Short s = (short) a;
short ss = s;
// Long lll = s;
// cannot cast 'java.lang.Short' to 'java.lang.Long'
// Long lll = (Long) s;
}
- 包装类为null时转换基本类型会抛出空指针异常
@Test
public void test7() throws Exception {
Boolean b = null;
try {
boolean bb = b;
} catch (NullPointerException e) {
System.out.println(e.getMessage());
}
Integer i = null;
try {
long l = i;
} catch (NullPointerException e) {
System.out.println(e.getMessage());
}
// null
// null
}
版权声明
本文章由作者“衡于墨”创作,转载请注明出处,未经允许禁止用于商业用途
发布时间:2019年11月28日 10:35:03
备案号:
闽ICP备19015193号-1
关闭特效
评论区#
还没有评论哦,期待您的评论!
引用发言