变量(三)

1、基本数据类型转换

1、自动类型转换

在这里插入图片描述

2、自动类型转换注意和细节

在这里插入图片描述

//自动类型转换细节
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
	
	//细节 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: byte,short,char 他们三者可以计算,在计算时首先转换为 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 
	
	//boolean 不参与转换
	boolean pass = true;
	//int num100 = pass;// boolean 不参与类型的自动转换
	
	//自动提升原则: 表达式结果的类型自动提升为 操作数中最大的类型 
	byte b4 = 1;
	short s3 = 100;
	int num200 = 1;
	float num300 = 1.1F;
	double num500 = b4 + s3 + num200 + num300; //float -> double 
	}
}

3、强制类型转换

3.1、介绍

自动类型转换的逆过程,将容量大的数据类型转换为容量小的数据类型。使用时要加上强制转换符 ( ),但可能造成精度降低或溢出,格外要注意。
在这里插入图片描述

4、强制类型转换细节

在这里插入图片描述

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 c1 = 100; //ok
	int m = 100; //ok
	//char c2 = m; //错误
	char c3 = (char)m; //ok 
	System.out.println(c3);//100 对应的字符, d 字符
	} 
}

5、基本数据类型和 String 类型的转换

1、介绍和使用

在这里插入图片描述

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";
	//会在 OOP 讲对象和方法的时候回详细
	
	//解读 使用 基本数据类型对应的包装类,的相应方法,得到基本数据类型 
	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 -> 含义是指 把字符串的第一个字符得到 
	//解读 s5.charAt(0) 得到 s5 字符串的第一个字符 '1' 
	System.out.println(s5.charAt(0));//1
	}
 }

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);
	} 
}

本博客引用至韩顺平老师Java课

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值