【超级无敌详细的韩顺平java笔记】从入门到精通---基本数据类型的转换

1.自动类型转换

当Java程序在进行赋值或运算时,精度小的类型自动转换为精度大的数据类型,这个就是自动类型转换。

数据类型按照精度(容量)大小排序为:

 注意和细节:

1 : 有多种类型的数据混合运算时,系统首先自动将所有数据转换成容量最大的那种数据类型,然后再进行计算。
public class AutoConvertDetail {
//编写一个 main 方法
public static void main(String[] args) {
//细节 1: 有多种类型的数据混合运算时,
//系统首先自动将所有数据转换成容量最大的那种数据类型,然后再进行计算
int n1 = 10; //ok
//float d1 = n1 + 1.1;//错误 n1 + 1.1 => 结果类型是 double
//double d1 = n1 + 1.1;//对 n1 + 1.1 => 结果类型是 double
float d1 = n1 + 1.1F;//对 n1 + 1.1 => 结果类型是 float

 		System.out.println(n1);
 		System.out.println(d1);
    }
}

2.当我们把精度(容量)大 的数据类型赋值给精度(容量)小 的数据类型时,就会报错,反之就会进行自动类型转换。

//int n2 = 1.1;//错误 double -> int
3: (byte, short) char 之间不会相互自动转换
// 当把具体数赋给 byte 时, (1) 先判断该数是否在 byte 范围内,如果是就可以
byte b1 = 10; //对 , -128-127
// int n2 = 1; //n2 是 int
// byte b2 = n2; //错误,原因: 如果是变量赋值,判断类型
//
// char c1 = b1; //错误, 原因 byte 不能自动转成 char

4.byteshortchar 他们三者可以计算,在计算时首先转换为 int 类型(包括byte+byte=int,byte+short=int)

byte b2 = 1;
byte b3 = 2;
short s1 = 1;
//short s2 = b2 + s1;//错, b2 + s1 => int
int s2 = b2 + s1;//对, b2 + s1 => int
//byte b4 = b2 + b3; //错误: b2 + b3 => int
//

5.boolean 不参与转换

boolean pass = true;
//int num100 = pass;// boolean 不参与类型的自动转换

6.自动提升原则: 表达式结果的类型自动提升为 操作数中最大的类型
 

//看一道题
byte b4 = 1;
short s3 = 100;
int num200 = 1;
float num300 = 1.1F;
double num500 = b4 + s3 + num200 + num300; //float

2 强制类型转换

自动类型转换的逆过程, 将容量大的数据类型转换为容量小的数据类型 。使用时要加上强制转换符 ( ) ,但可能造成精度降低或溢出 , 格外要注意。
public class ForceConvert {
	public static void main(String []args){
		int n1 = (int)1.9;
		System.out.println("n1="+n1);//1,造成精度损失
        int n2 = 2000;
        byte b1 = (byte)n2;
        System.out.println("b1="+b1);//造成数据溢出
	}
}

问:为什么是-48?

答:这是因为byte类型的取值范围是-128到127,而2000超出了byte类型的范围。当强制类型转换时,会将高位的数值舍弃,只保留低位,因此得到的结果是-48。具体来说,2000的二进制表示是11111010000,截取低八位后得到11010000,将其视为补码形式,转换回原码得到-48。

2.细节说明

  • 当进行数据的大小从大到小,就需要使用强制转换。
  • 强转符号只针对于最近的操作数有效,往往会使用小括号提升优先级
public class ForceConvertDetail {
//编写一个 main 方法
    public static void main(String[] args) {
    //演示强制类型转换
    //强转符号只针对于最近的操作数有效,往往会使用小括号提升优先级
    //int x = (int)10*3.5+6*1.5;//编译错误: double -> int
    int x = (int)(10*3.5+6*1.5);// (int)44.0 -> 44
        System.out.println(x);//44
    }
}
  • char类型可以保存int的常量值,但不能保存int的变量值,需要强转
public class ForceConvertDetail {
//编写一个 main 方法
    public static void main(String[] args) {
        char c1 = 100; //ok
        int m = 100; //ok
        //char c2 = m; //错误
        char c3 = (char)m; //ok
    System.out.println(c3);//100 对应的字符, d 字符
    }
}
  • byte和short,char类型在进行运算时,当做int类型处理

 3.基本数据类型和String类型的转换

1.介绍
在程序开发中,我们经常需要将基本数据类型转换成String类型,或者将String类型转换成基本数据类型。
  • 基本数据类型 ->String
 
public class StringToBasic {
//编写一个 main 方法
public static void main(String[] args) {
//基本数据类型->String
int n1 = 100;
float f1 = 1.1F;
double d1 = 4.5;
boolean b1 = true;
String s1 = n1 + "";
String s2 = f1 + "";
String s3 = d1 + "";
String s4 = b1 + "";
System.out.println(s1 + " " + s2 + " " + s3 + " " + s4);
    }
}
  • String->对应的基本数据类型
String s5 = "123";
//解读 使用 基本数据类型对应的包装类,的相应方法,得到基本数据类型
int num1 = Integer.parseInt(s5);
double num2 = Double.parseDouble(s5);
float num3 = Float.parseFloat(s5);
long num4 = Long.parseLong(s5);
byte num5 = Byte.parseByte(s5);
boolean b = Boolean.parseBoolean("true");
short num6 = Short.parseShort(s5);

System.out.println("===================");
System.out.println(num1);//123
System.out.println(num2);//123.0
System.out.println(num3);//123.0
System.out.println(num4);//123
System.out.println(num5);//123
System.out.println(num6);//123
System.out.println(b);//true

}
}

怎么把字符串转成字符 char -> 含义是指 把字符串的第一个字符得到

String S5 = "123";

解读 s5.charAt(0) 得到 s5 字符串的第一个字符 '1'

		 System.out.println(s5.charAt(0));
		 System.out.println(s5.charAt(1));
		 System.out.println(s5.charAt(2));

 .2 注意事项

  • 在将 String 类型转成 基本数据类型时, 要确保String类型可以转换成有效的数据,比如 我们可以把 "123" , 转成一个整数,但是不能把 "hello" 转成一个整数
  • 如果格式不正确,就会抛出异常,程序就会终止, 这个问题在异常处理章节中,会处理。
/**
* 演示字符串转基本数据类型的细节
*/
public class StringToBasicDetail {
//编写一个 main 方法
public static void main(String[] args) {
String str = "hello";
//转成 int
int n1 = Integer.parseInt(str);
System.out.println(n1);
}
}

 作业

 输出:n3=30

n5=8

 3.红楼梦西游记

char c1 = '男';
 		char c2 = '女';
 		System.out.println(c1+c2);

猜你喜欢

转载自blog.csdn.net/qq_45206556/article/details/131770128