zoukankan      html  css  js  c++  java
  • java 数据类型

    一、java的基础数据类型

    1. 内置数据类型(8种):

    数据类型 默认值 长度(位)
    byte 0 8
    short 0 16
    int 0 32
    long 0 64
    char   16
    float 0.0 32
    double 0.0 64
    boolean false 1

     

    2. 引用数据类型(2种):

      1)对象:默认值是null

      2)数组:默认值是null

     1 public class Test {
     2     static int a;
     3     static short b;
     4     static char c;
     5     static float d;
     6     static double e;
     7     static long f;
     8     static boolean g;
     9     static byte h;
    10 
    11     public static void main(String[] args) {
    12         System.out.println("int类型的默认值:" + a);
    13         System.out.println("short类型的默认值:" +b);
    14         System.out.println("char类型的默认值:" +c);
    15         System.out.println("float类型的默认值:" +d);
    16         System.out.println("double类型的默认值:" +e);
    17         System.out.println("long类型的默认值:" +f);
    18         System.out.println("boolean类型的默认值:" +g);
    19         System.out.println("byte类型的默认值:" +h);
    20     }
    21 }
    输出为:
    int类型的默认值:0 short类型的默认值:0 char类型的默认值: float类型的默认值:0.0 double类型的默认值:0.0 long类型的默认值:0 boolean类型的默认值:false byte类型的默认值:0

    二、自动类型转换

    取值范围小的可以自动转为取值范围大的类型,反之不可,但可以用强制类型转换,如

           char c1='a';//定义一个char类型

           int i1 = c1;//char自动类型转换为int

    1.对于byte,short,char,int,long类型: 

    a)在+-*/%运算中:

    当两个操作数中没有long类型时,两个操作数中非int类型会先自动转换为int类型,再参与运算,返回结果为int;

    当两个操作数中含有long类型时,两个操作数中非long类型会自动转换为long类型,再参与运算,返回结果为long;

    b).在自加和自减运算中,操作数的类型是什么就是什么,不会发生转化。

    2.对于float,double两个浮点数据类型与整型数据类型的运算:

    如果运算的操作数含有float或者double类型的,其他类型自动转化为float或double类。

     1 public class Test {
     2     public static void main(String[] args) {
     3        byte a = 1;
     4        int b = 1;
     5        long c = 1;
     6        float d = 1.0f;
     7        double e = 1.0;
     8        //等号右边的变量类型范围大于左边的变量类型,编译没错
     9        b = a;
    10        b = b + 1;
    11        b = 2 * a;
    12        c = a + b;
    13        d = c + b;
    14        a++;
    15        b++;
    16         //等号右边的变量类型范围小于左边的变量类型,编译出错
    17        //以下代码是错的!
    18        a = b;
    19        a = a + 1;
    20        a = 2 * b;
    21        b = c + 1;
    22        d = e;
    23     }
    24 }

    三、float与double使用的注意点

    1.float类型的定义

     //准确定义
     float f = 1.66f;  //直接定义float型
     float f1 = (float)3.13;  //double型强制转float型
     float f3 = 1;  //int型自动转float型
     //错误定义
     float f2 = 1.66;  //double无法转float型,报错
              

    2.float类型与double型运算,准确度不高

    System.out.println(2.0-1.9);  //输出0.10000000000000009
    System.out.println(2.0f-1.8f);  //输出0.20000005

    解决方法:用BigDecimal来转化,但要注意:BigDecimal的入参要为String类型,如果入参还是float或者double类型,不能解决问题!

            BigDecimal num1 = new BigDecimal(Double.toString(2.0));
            BigDecimal num2 = new BigDecimal(Double.toString(1.9));
            double result = num1.subtract(num2).doubleValue();
            System.out.println(result);
            BigDecimal num3 = new BigDecimal(Float.toString(2.0f));
            BigDecimal num4 = new BigDecimal(Float.toString(1.9f));
            float result1 = num3.subtract(num4).floatValue();
            System.out.println(result1);   
  • 相关阅读:
    day3 集合
    进度条
    day3 文件操作 seek tell 修改
    day3 函数
    同学满分代码,购物车。
    day2杂---三元运算 is
    模块sys os
    day2--列表/元组/字符串/字典
    一、Git配置
    四、TestNG 批量执行脚本Runner.xml
  • 原文地址:https://www.cnblogs.com/ldn1230/p/10949410.html
Copyright © 2011-2022 走看看