zoukankan      html  css  js  c++  java
  • 基本数据类型转换

    基本转换

    类型等级如图:


    1.自动转换。表数范围小可以向表数范围大进行自动类似那个转换(byte与char,short与char不能互转,可以实现强转)
     2.基本类型值转为字符串类型,用"+"号与字符串连接
     3.强制类型转换。强制类型转换会造成溢出,从而数据丢失

    public static void main(String[] args) {
            int a=6;
            float b=a;
            System.out.println(b);//已经实现自动转行
            
            byte c=98;
            char d=(char) c;
            System.out.println(d);//只能强转
            
            float f=3.5f;
            System.out.println(f+"");//输出为字符串类型
            
            //加法运算和连接运算优先级相等,遵循从左往右计算
            System.out.println(3+4+"Hello!");
            System.out.println("Hello!"+3+4);
            
            int iValue=156;
            byte iVulue2=(byte) iValue;
            System.out.println(iVulue2);//数据丢失
            
            double dValue=3.888;//小数默认double类型
            int dValue2=(int) dValue;
            System.out.println(dValue);
            System.out.println(dValue2);
    
        }

    输出:

    6.0
    b
    3.5
    7Hello!
    Hello!34
    -100
    3.888
    3

    表达式类型自动提升

       1.当一个算术表达式包含多个基本类型时,整个算术表达式会将所有的基本数据会发生提升
           1)所有的byte类型,short类型,和char类型提升到interesting类型
           2)整个算术表达式的数据类型提高到表达式中最高等级操作数的数据类型
           3)整数类型进行除法运算时,即使无法除尽,也会得到int类型的结果
           4)表达式中包含了字符串,如果+号在左边且不与字符串相邻,进行加法运算,如果在右边,表示连接符

    public static void main(String[] args) {
            short sValue=5;
            sValue=(short) (sValue-2);//sValue-2的结果提升为int类型
            System.out.println(sValue);
            
            byte b = 40;
            char c = 'a';
            int a = 23;
            double d = .314;
            double sum=a+b+c+d;//最高等级操作数为d,故所有数据类型为double
            System.out.println(sum);
            
            //整数类型进行除法运算时,即使无法除尽,也会得到int类型的结果
            int e=23/3;
            double f=23/3;
            double g=23/3;
            System.out.println(e);
            System.out.println(f);
            System.out.println(g);
            
            System.out.println("Hello!"+'a'+23);
            System.out.println('a'+23+"hello!");
            
        }

    输出:

    3
    160.314
    7
    7.0
    7.0
    Hello!a23
    120hello!

  • 相关阅读:
    Gevent高并发网络库精解
    python多线程参考文章
    python多线程
    进程与线程
    golang 微服务以及相关web框架
    微服务实战:从架构到发布
    python 常用库收集
    总结数据科学家常用的Python库
    20个最有用的Python数据科学库
    自然语言处理的发展历程
  • 原文地址:https://www.cnblogs.com/yumiaoxia/p/8846567.html
Copyright © 2011-2022 走看看