java学习笔记(09)_类型的转换

类型转换

介绍

  1. 当java程序进行赋值或运算时,精度小的类型自动转换到精度大的数据类型,这就是自动类型转换

  2. 数据类型按精度(容量)大小排序为(背,规则)

    image-20230705161906628

数据类型自动转换

  1. 有多类型的数据混合运算时,系统首先自动将所有数据转换成容量最大的那种数据类型,然后再计算
  2. 当我们把精度(容量)大的数据类型赋值给精度(容量)小的数据类型时,就会报错,反之就会进行自动类型转换
  3. (byte,short)和char之间不会相互自动转换
  4. byte,short,char他们三者可以计算,在计算时首先转换为int类型
  5. boolean 不参与转换
  6. 自动提升原则:表达式结果的类型自动提升为操作数中最大的类型
public class AutoConvertDetail {
	public static void main(String[] args){
		// 1. 有很多种类型的数据混合运算时
		// 系统首先自动将所有数据转换成容量最大的那种数据类型,然后进行计算
		int a = 2;
		// float a1 = a + 1.1; // 错误的,原因: a + 1.1 => 结果是double
		// double a1 = a + 1.1; // 对的
		float a1 = a + 1.1F; // 对的
		// 2. 当我们把精度(容量)大的数据类型赋值给精度(容量)小的数据类型时,就会报错,反之就会进行自动类型转换
		// int c = 1;
		// int c = 1.1; //错误的
		// byte a = c;	// 错误的	
		// 3. (byte,short)和char之间不会相互自动转换
		// 将值赋给byte时,先看值的是否在该类型的范围内
		byte c10 = 10;	// 对的
		// 如果是按变量赋值给byte时,先看类型
		int c9 = 1;
		// byte c11 = c9;
		char c2 = 1;
		// byte c3 =  c2; // 错误的
		// short c3 = c2; // 错误的	
		// 4.byte、short、char他们三者可以计算,在计算时首先转换为int类型
		byte a2 = 1;
		short a3 = 3;
		int a4 = a2 + a3;
		// 5. boolean不参与转换
		boolean a5 = false;
		// int a7 = a5;
		// 6.自动提升原则:表达式结果的类型自动提升为操作数中最大的类型
		double a6 = 6;
		byte a7 = 1;
		double a8 = a7 + a6;
	}
}	

强制类型转换

  1. 定义

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

  2. 细节

    1. 当进行数据的大小从 大 ---》 小,需要使用强制转换
    2. 强制符号只针对于最近的操作数有效,往往会使用小括号提升优先级
    3. char类型可以保存int的常量值,但不能保存int的变量值,需要强转
    4. byte和short,char类型在进行运算时,当做int类型处理
    public class ForceConvertDetail{
    	public static void main(String[] args){
    		// 1.强制符号只针对于最近的操作数有效,往往会使用小括号提升优先级
    		// int x = (int)10*3.5 + 1.6*6; // 错误,double -> int
    		int x = (int)(10*3.5+1.6*6);
    		// 2.char类型可以保存int的常量值,但不能保存int 的变量,需要强转
    		char a1 = 100;
    		int a2 = 200;
    		// char a3 = a2; //错误的
    		char a3  = (char)a2; // 正确的
    		System.out.println(a3);	
    	}
    }
    

基本类型和String的转换

  1. 在程序开发中,我们经常需要将基本数据类型转成String类型,或者将String类型转成基本数据类型

  2. 用法

    1. 基本类型转String类型

      1. 语法

        将基本类型的值+""即可

      public class StringBasic{
      	public static void main(String[] args){
      		int c1 = 100;
      		float c2 = 1.1F;
      		double c3 = 34.4;
      		boolean c4 = false;
      		String c5 = c1 + "";
      		String c6 = c2 + "";
      		String c7 = c3 + "";
      		String c8 = c4 + "";
      		System.out.println(c5);
      		System.out.println(c6);
      		System.out.println(c7);
      		System.out.println(c8);
      	}
      }
      
    2. String类型转基本数据类型

      1. 语法

        通过基本类型的包装类调用parseXX方法即可

      public class StringBasic{
      	public static void main(String[] args){
      		// String转基本类型
      		String c9 = "123";
      		int c10 = Integer.parseInt(c9);
      		double c11 = Double.parseDouble(c9);
      		float c12 = Float.parseFloat(c9);
      		byte c13 = Byte.parseByte(c9);
      		short c14 = Short.parseShort(c9);
      		boolean c15 = Boolean.parseBoolean("true");
      		System.out.println(c10);
      		System.out.println(c11);
      		System.out.println(c12);
      		System.out.println(c13);
      		System.out.println(c14);
      		System.out.println(c15);			
      		// 如何String转char
      		System.out.println(c9.charAt(0));
      	}
      }
      
    3. 细节

      1. 在将Sting类型转成基本数据类型时,要确保String类型能够转成有效的数据,比如我们可以把“123”转成一个整数,但是不能把"hello"转成一个整数
      2. 如果格式不正确,就会抛出异常,程序就会终止
posted @ 2023-07-05 18:50  xiaowei123456  阅读(11)  评论(0)    收藏  举报