zoukankan      html  css  js  c++  java
  • 基本数据类型间的运算(不包括boolean)

    一 基本数据类型的间的运算  (不包括boolean)

      1.自动类型提升 : 小容量的变量和大容量的变量做运算结果用大容量的变量的类型来接收.

        byte , short , char -> int -> long -> float -> double

        注意 : 容量指的是表数范围.

      2.强制类型转换 : 自动类型提升的逆过程   (将大容量 赋值给小容量)  

        需要使用强制类型转换符 : (强转的类型)

        注意 :强制类型转可能会损失精度

    二 String与基本数据类型间的运算

    public class VarTest5{
    
        public static void main(String[] args){
        
            //用什么样的类型的变量来接收数据?
            int a = 10;
            long num = 20L;
            long b = a + num;
            System.out.println(b);
    
            System.out.println("---------------------------");
    
            byte nu = 20;
            int nu2 = 30;
            int nu3 = nu + nu2;
            System.out.println(nu3);
    
            System.out.println("---------------------------");
    
            long c1 = 12L; //8字节
            float c2 = 12.3f;//4字节
            float c3 = c2 + c1;
            System.out.println(c3);
    
            System.out.println("---------------------------");
            //byte,short,char三者之间做运算自身都先提升为int再运算
            byte d1 = 12;
            short d2 = 20;
            int d3 = d1 + d2;
    
            System.out.println("---------------------------");
    
            char ch = 'A';
            int number = ch + 1; //a = 97  b = 98  c = 99  A = 65 B = 66
            System.out.println(number);
        
        }
    }
    public class VarTest6{
    
        public static void main(String[] args){
        
            int a = 12;
            byte b = (byte)a; //精度没有损失
            System.out.println(b);
    
            System.out.println("----------------------------------");
    
            float f = 12.3f;
            int a2 = (int)f;  //精度会损失
            System.out.println(a2);
    
            System.out.println("----------自动类型提升前也可加强制类型转换符-------------");
    
            short s = 123;
            int number = (int)s; //会自动类型提升
            System.out.println(number);
    
            System.out.println("---------------强转时注意的细节-------------------");
    
            int num = 20;
            int num2 = 30;
            byte num3 = (byte)(num + num2);
            System.out.println(num3);
        }
    }

    三 String与基本数据类型(8种)间的运算

      说明:
        1.String只能与基本数据类型做连接运算(+ 连接符)。
        2.String与基本数据类型做运算符结果还为String

    public class VarTest7{
    
        public static void main(String[] args){
            
            //声明一个String类型的变量并赋值
            String s = "aaa";
            //先了解即可(面向对象的时候再说)
            String s2 = new String("bbb"); //String是一个引用数据类型属于类。
            System.out.println(s);
    
            System.out.println("----------------------------------");
    
            String ss = "hello";
            String ss2 = "java";
            String ss3 = ss + ss2;
            System.out.println(ss3);
    
            String ss4 = ss + 10;
            System.out.println(ss4);
    
            boolean boo = true;
            String ss5 = ss + boo;
            System.out.println(ss5);
    
            String ss6 = ss + 'a';
            System.out.println(ss6);
            System.out.println(ss + 'a');
    
            System.out.println("---------------练习-------------------");
            
            char c = 'a'; //a = 97
            int num = 1;
            String str = "志玲姐姐";
    
            System.out.println(str + c + num); //志玲姐姐a1
            System.out.println(c + num + str);//98志玲姐姐
            System.out.println(str + num + c);//志玲姐姐1a
            System.out.println(c + str + num);//a志玲姐姐1
        }
    }
    /*
    
        常量 :
    
    */
    public class VarTest8{
    
        public static void main(String[] args){
        
            //Java 的浮点型常量默认为double型
            double d = 12.3;
            float f = 12.3f;
            System.out.println(f);
    
            //java的整型常量默认为 int 型
            long lon = 111111111111L;
            System.out.println(lon);
        
        }
    }
  • 相关阅读:
    RabbitMQ消息队列-高可用集群部署实战
    python+rabbitMQ实现生产者和消费者模式
    RabbitMQ Connection Channel 详解
    Linux中安装Erlang
    基于 CentOS 搭建 Python 的 Django 环境
    redis单机安装
    CentOS7安装RabbitMQ(单机)
    iptables 规则行号,删除及插入规则
    jQuery前端生成二维码
    MailKit使用IMAP读取邮件找不到附件Attachments为空的解决方法
  • 原文地址:https://www.cnblogs.com/zmy-520131499/p/11041518.html
Copyright © 2011-2022 走看看