提示:文章内容总结自网络,如有侵权请及时联系我。
文章目录
前置知识
Java基本数据类型介绍
注意:一个字节等于8比特
- byte 字节 取值范围(-128~127),占用1个字节(-2的7次方到2的7次方-1).
- short 短整型 取值范围(-32768~32767),占用2个字节(-2的15次方到2的15次方-1)
- int 整形 取值范围(-2147483648~2147483647),占用4个字节(-2的31次方到2的31次方-1)
- long 长整形 取值范围(-9223372036854774808~9223372036854774807),占用8个字节(-2的63次方到2的63次方-1)
- float 浮点型 占用4个字节
- double 双精度浮点型,占用8个字节
float和double的取值范围
类型转换规则
- 小类型向大类型的转换会自动完成,不需要额外的操作,JVM自动完成。(自动类型转换也叫隐式类型转换)
- 自动转换的规则,符号位会自动扩展,负数补1,正数补0.
- 自动类型转换包含以下情况:
(1)、byte<short<int<long<float<double
(2)、int和char类型的数据在某些情况下,可以完成自动转换 - 整数到浮点数,会损失精确度。
提示:以下是本篇文章正文内容,下面案例可供参考
一、先上总结
1. 基本数据类型之间的相互转换。
- 小转大,JVM帮你直接转;大转小,编译不通过。
- 大转小怎么办?
- 告诉编译器,括号里面是要转换的类型,如下图所示。例如:(short)(int)(long)等等。
- 借助包装类。
以Interger为例,先看Interger里面的代码:
public byte byteValue() {
return (byte)value;
}
/**
* Returns the value of this {@code Integer} as a {@code short}
* after a narrowing primitive conversion.
* @jls 5.1.3 Narrowing Primitive Conversions
*/
public short shortValue() {
return (short)value;
}
/**
* Returns the value of this {@code Integer} as an
* {@code int}.
*/
public int intValue() {
return value;
}
通过上面代码,我们可以发现,存在xxxValue方法,可以将大类型转换为小类型,其本质实现还是刚才所述的告诉编译器要转换的类型。
//先实例化,再使用对应方法
Integer i1 =111;
short s1= i1.shortValue();
2.String类型转换
基本数据类型转String
- 包装类中的toString方法
- String类中的String.valueOf()方法
- 直接拼接
//以Long包装类为例
Long l1=new Long(5L);
//包装类中的toString方法
String s=l1.toString();
System.out.println(s);
//String类中的String.valueOf()方法
String s2=String.valueOf(l1);
System.out.println(s2);
//直接拼接
String s3 = "" + l1;
System.out.println(s3);
String转基本数据类型
- 包装类中的parseXxx()方法
- 包装类中的valueOf()方法
//String转Double
String str ="9.0";
Double d=Double.parseDouble(str);
Double d2=Double.valueOf(str);
Object之间的转换
Object转其它
Object有个toString方法,所以我们一般是先转String,然后String转其他。
Object obj="111";
int i=Integer.parseInt(obj.toString());
其它转Object
因为java中所有类都是继承自Object,所以可以直接转换。相当于父类的对象用子类来实例化
Object obj=111;
二、来个例子
1.以long类型转其它为例:
代码如下(示例):
package TypeChange;
public class LongTest {
public static void main(String[] args) {
long l=10L;
Long l1=new Long(10L);
System.out.println("之前的long值:"+l1);
//1、转成int
int i=(int)l;
System.out.println("直接转换为:"+i);
int i1=l1.intValue();
System.out.println("方法转成的int值:"+i1);
// int i2=(int)l1;
//2、转化为Float类型
float f=(float) l;
System.out.println("直接转换为:"+f);
float f1=l1.floatValue();
Float f2=(float)l1;
System.out.println("直接转换:"+f2);
float f3=l;
System.out.println(f3);
//3、转化为double类型
double d=(double) l;
double d1=(double) l1;
double d2=l1.doubleValue();
double d3=l;
System.out.println(d);
System.out.println(d1);
System.out.println(d2);
System.out.println(d3);
//4、转化为String类型
String s=String.valueOf(l);
String s1=l1.toString();
System.out.println(s);
System.out.println(s1);
String s2=Long.toString(l);
//5、转化为Object类型
Object obj=l;
System.out.println(obj);
Object obj1=l1;
System.out.println(obj1);
Object obj2=(Object) l;
System.out.println(obj2);
Object obj3=(Object) l1;
System.out.println(obj3);
//6、long转short
short sh=(short) l;
// short sh1=(short) l1;
short sh2=l1.shortValue();
//7、long转byte
byte b1=l1.byteValue();
System.out.println(b1);
byte b2=(byte)l;
System.out.println(b2);
}
}
2.String转其它例子
代码如下(示例):
package TypeChange;
public class StringTest {
public static void main(String[] args) {
int i=12343;
String str = String.valueOf(i);
String str1 = Integer.toString(i);
System.out.println(str1);
//1、String转int
int i1=Integer.parseInt(str);
System.out.println(i1);
int i2=Integer.valueOf(str);
System.out.println(i2);
//2、String转Long
Long l=Long.parseLong(str);
Long l2=Long.valueOf(str);
//3、String转Double
Double d=Double.parseDouble(str);
Double d2=Double.valueOf(str);
//4、String转Float
Float f=Float.valueOf(str);
Float f1=Float.parseFloat(str);
//5、String转short
short sh=Short.valueOf(str);
short sh1=Short.parseShort(str);
//6、String转Byte
byte b=Byte.valueOf(str);
byte b1=Byte.parseByte(str);
//7、String转Object
Object obj=str;
}
}
总结
本文主要是类型转换中方法的调用,至于包装类的拆箱和装箱、类型转换中内存的溢出等等有空再更,也欢迎评论区大佬指正和补充。