java--数据类型

java里数据类型分为两种,基本数据类型和引用数据类型。

1.基本数据类型
整形:byte (1个字节)short(2个字节) int (4个字节)long(8个字节)
浮点型:float(4个字节) double(8个字节)
字符型: char(2个字节)
布尔型: boolean(1个字节)

2.引用数据类型
类(class)
接口(interface)
数组(array)

基本数据类型整形的存储空间以及取值范围如下,分别为byte,short,int和long

占用存储空间取值范围
1字节 = 8比特-128~127
2字节-2~15 ~ 2~15-1
4字节-2~31 ~ 2~31-1(约21亿)
8字节-2~63 ~ 2~63-1

注意:
1)java的整形常量默认为int类型,声明long类型的常量或者变量需要在值面加上“l”或者“L”

2)bit是计算机中最小的存储单位,byte是计算机中最基本的存储单元

基本数据类型浮点型的存储空间以及取值范围如下,分别为float(单精度)尾数可以精确到7位有效数字,double(双精度)精确度为float的两倍

占用存储空间取值范围
4字节-3.403E38 ~ 3.403E38
8字节-1.798E308 ~ 1.798E308

注意:
1)java的浮点型常量默认为double类型,在声明float类型的常量或者变量时要在后面加上f或者F
3.code

package com.yl.pdfdemo.day01;

/**
 * @Author wfj
 * @Date 2021/1/3
 * @Description 基本数据类型
 * @Version 1.0
 */

public class VariableTest1 {
    public static void main(String[] args) {
        //1.整形,byte(1个字节) short(2个字节) int(4个字节) long(8个字节)
        // byte范围-128 ~ 127,如果超出了这个范围,编译不会通过
        byte a = -1;
        byte b = 127;
        //byte c = 128; 编译不会通过
        System.out.println(a);
        System.out.println(b);

        short d = 789;
        int e = 123;
        //声明long类型的变量时,必须在后面加L或者l
        long f = 123456789L;
        System.out.println(d);
        System.out.println(e);
        System.out.println(f);

        //2.浮点型
        // float(4个字节)但它的取值范围比long的还要大
        //定义float类型的变量时,值后面要加上f或者F,如果不加,编译不会通过
        double g = 122.88;
        float h = 89.67F;
        System.out.println(g);
        System.out.println(h);

        //3.字符型: char(1字符=2字节)
        //定义一个char类型的变量,内部只能写一个字符
        char c1 = 'a';
//        char c2 = 'ab';编译不会通过

        //表示方式
        //1.声明一个字符 2.转义字符 3.直接使用unicode值来表示字符型常量
        char c3 = '\n'; // \n代表换行
        char c4 = '\t'; // \t代表制表符号Tab键
        System.out.print("hello" + c3);
        System.out.println("world");


        char c5 = '\u0043';
        System.out.println(c5);

        //4.布尔类型: boolean
        //只能取两个值,true或者false
        boolean b1 = true;
        System.out.println(b1);
    }
}

4.类型转换问题
1)自动类型转换

package com.yl.pdfdemo.day01;

/**
 * @Author wfj
 * @Date 2021/1/3
 * @Description  基本数据类型之间的运算规则
 * @Version 1.0
 */

public class VariableTest2 {

    public static void main(String[] args) {
        /*
        这里只讨论7种基本数据类型变量之间的运算,不包含boolean类型

        1、自动类型转换
        结论: 当容量小的数据类型的变量与容量大的数据类型的变量做运算时,结果会是自动提升到容量大的数据类型
        byte 、char 、short --》 int --》 long --》 float --> double

        特别的: 当byte、char和short三种类型的变量做运算时,结果会为int类型

        2.强制类型转换
         */

        byte a = 12;
        int b = 57;
//        byte c = a + b;编译不会通过
        int c = a + b;
        long d = a + b;
        System.out.println(c);
        System.out.println(d);
        float e = a + b;
        System.out.println(e);

        short g = 13;
        double h = g;
        System.out.println(h);

        char i = 'a';
        int j = 17;
        int l = i + j;
        System.out.println(l);

        short m = 179;
//        char n = m + i;编译不会通过
        byte o = 77;
//        char p = i + o;编译不会通过
//        short r = m + i;编译不会通过
//        short s = m + o;编译不会通过
    }
}

2)强制类型转换

package com.yl.pdfdemo.day01;

/**
 * @Author wfj
 * @Date 2021/1/9
 * @Description 强制类型转换问题
 * @Version 1.0
 */

public class VariableTest3 {

    public static void main(String[] args) {
        /**
         * 强制类型转换: 自动类型提升运算的逆运算
         * 1.使用强制符,()
         * 2.注意点:强制类型转换,可能导致精度丢失
         */

        // 精度丢失
        double d1 = 12.9;
        // 截断操作, 会显示12
        int i1 = (int)d1;
        System.out.println(i1);

        // 不会丢失
        long l1 = 123L;
        short s1 = (short)l1;
        System.out.println(s1);

        // 精度丢失
        int i2 = 128;
        byte b = (byte)i2;
        System.out.println(b); // -128


        // 编译情况1
        long l2 = 123456;
        System.out.println(l2);
        //编译失败,过大的整数
//        long l3 = 12345678976542;
        long l4 = 12345678976542L;
        System.out.println(l4);

        //**********************
        //整数常量,默认类型为int
        //浮点数常量,默认类型为double
        //**********************

        //编译失败
        //float f1 = 12.3;
        float f2 = (float)12.3;
        System.out.println(f2);

        byte b1 = 12;
        //编译失败,b1为byte类型,1为int类型,相加应该用int来接受
      //  byte b2 = b1 + 1;
        // 编译失败
       // float f3 = b1 + 12.3;





    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值