• java后端学习-第一部分java基础:数据类型转换


    • 自动类型转换
      当java程序在进行赋值或运算时,精度小的类型自动转换为精度大的数据类型,这就是自动类型转换。
      数据类型按精度(容量)大小排序为
    
    public class AutoConvert{
    	public static void main(String[] args){
    		//演示自动类型转换
    		int a = 'a';//对 char -> int
    		double b = 80;//对 int -> double
    		System.out.println(a);
    		System.out.println(b);
    	}
    }
    

    • 自动类型转换注意和细节
      1. 有很多类型的数据混合运算时,系统自动将所有数据转换成容量最大的那种数据类型,然后再进行计算。
      2. 当我们把精度(容量)大的数据类型赋值给精度(容量)小的数据类型时,就会报错,反之就会进行自动类型转换。
      3. (byte,short)和char之间不会相互自动转换。
      4. byte,short,char 他们三者可以计算,在计算时首先转换为int类型。
      5. boolean 不参与转换。
      6. 自动提升原则:表达式结果的类型自动提升为操作数中最大的类型。
    //自动类型转换细节
    public class AutoConvertDetail{
    	public static void main(String[] args){
    		// 有很多类型的数据混合运算时,系统自动将所有数据转换成容量最大的那种数据类型,然后再进行计算。
    		int n1 = 10;
    		// float d1 = n1 + 1.1;//错误, n1 + double => 结果类型是double
    		double d1 = n1 + 1.1;//正确
    		float d2 = n1 + 1.1F;//正确
    		//当我们把精度(容量)大 的数据类型赋值给精度(容量)小 的数据类型时,就会报错,反之就会进行自动类型转换。
    		//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
    		// 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
    	}
    }
    
    • 强制类型转换
      自动类型转换的逆过程,将容量大的数据类型转换为容量小的数据类型。使用时要加上强制转换符(),但可能造成精度降低或溢出,要格外注意。
    
    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,造成数据溢出
    	}
    }
    

    • 强制类型转换细节说明
      1. 当进行数据的大小 从 大 -> 小 ,就需要使用到强制转换。
      2. 强转符号只针对于最近的操作数有效,往往会使用小括号提升优先级。
      3. char类型可以保存int的常量值,但不能保存int的变量值,需要强转。
      4. byte和short类型在进行运算时,当做int类型处理。
    
    public class ForceConvertDetail{
    	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 字符
    	}
    }
    

    • 基本数据类型和String类型的转换
      • 基本类型转String类型:将基本类型的值+""即可。
      • String类型转基本数据类型:通过基本类型的包装类调用parseXXX方法即可。
    
    public class StringToBasic{
    	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);
    		float num2 = Float.parseFloat(s5);
    		double num3 = Double.parseDouble(s5);
    		long num4 = Long.parseLong(s5);
    		byte num5 = Byte.parseByte(s5);
    		boolean num6 = Boolean.parseBoolean("true");
    		short num7 = Short.parseShort(s5); 
    
    		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);//true
    		System.out.println(num7);//123
    
    		//怎么把字符串转成字符 char -> 含义是指 把字符串的第一个字符得到
    		//解读 s5.charAt(0) 得到 s5 字符串的第一个字符 '1'
    		System.out.println(s5.charAt(0));
    	}
    }
    

    • 注意事项
      1. 在将 String 类型转成 基本数据类型时,要确保String类型能够转成有效的数据 ,比如 我们可以把 "123" , 转成一 个整数,但是不能把 "hello" 转成一个整数。
      2. 如果格式不正确,就会抛出异常,程序就会终止。
    
    public class StringToBasicDetail{
    	public static void main(String[] args) {
    		String str = "hello";
    		//转成int
    		int n1 = Integer.parseInt(str);
    		System.out.println(n1);
    	}
    }
    

  • 相关阅读:
    hdu 2881 Jack's struggle(DP)
    [置顶] DataGridView控件---绑定数据方法
    Computational Geometry Template_Polygon
    Max retries exceeded with url
    Linux /proc 的意义
    [置顶] 生成树协议介绍
    StringBuffer与StringBuilder的异同
    linux 下Eclipse for C/C++的不常见设置
    [Elasticsearch] 分布式搜索
    oracle递归函数
  • 原文地址:https://www.cnblogs.com/yeyueweiliang/p/14530750.html
Copyright © 2020-2023  润新知