(05)Java入门-基础语法:变量详解


Java变量详解

01.变量里的数据在计算机中的底层原理



在这里插入图片描述

package com.itheima.variable;

public class VariableDemo1 {
    public static void main(String[] args) {
        // 目标:搞清楚字符在计算机中的存储原理,存储的是对应字符编号的二进制。
        System.out.println('a' + 1);  // 97 + 1 = 98

        System.out.println('A' + 1); // 65 + 1 = 66

        System.out.println('0' + 1); // 48 + 1 = 49

        System.out.println("----------------------------------------");

        // 二进制、八进制、十六进制的表示方案如下:
        int a1 = 0B01100001; // 二进制
        System.out.println(a1);

        int a2 = 0141; // 八进制
        System.out.println(a2);

        int a3 = 0xFA; // 十六进制
        System.out.println(a3);
    }
}

02.二进制转十进制、八进制、十六进制、数据单位



03.数据类型


package com.itheima.variable;

public class VariableDemo2 {
    public static void main(String[] args) {
        // 目标:掌握8种基本数据类型。
        // 1、整型
        byte age = 21;
        byte age2 = 127;
        // byte age3 = 128; // 越界了

        short number = 4244;
        int money = 324244;
        long lg = 424244535;

        // 注意:随便写一个整数字面量默认是int类型,
        // 4324325255534虽然没有超过long的范围,但是它超过了本身int类型的范围。所以报错
        // 如果希望随便写一个整数字面量默认是long类型的,应该在后面加上L / l
        long lg2 = 4324325255534L;

        // 2、浮点型(小数)
        // 随便写小数字面量默认是double,如果希望是float加上F / f
        float score = 34.9F;
        double score2 = 99.5;

        // 3、字符型
        char ch = 'a';
        char ch2 = '中';

        // 4、布尔型
        boolean flag = true;
        boolean flag2 = false;

        // 拓展一个引用数据类型(除了8种基本数据类型,就全部是引用数据类型)
        String s = "黑马程序员";
        System.out.println(s);
    }
}

04.类型转换

自动类型转换

package com.itheima.type;

public class TypeConversionDemo1 {
    public static void main(String[] args) {
        // 目标:理解自动类型转换。
        byte a = 12;
        int b = a; // 自动类型转换: 小范围类型的变量可以直接赋值类型范围大的变量
        System.out.println(a); // 12
        System.out.println(b); // 12

        int number = 999;
        double db = number;  // 自动类型转换
        System.out.println(number); // 999
        System.out.println(db); // 999.0

        char ch = 'a';   // ch = 00000000 01100001
        int it = ch; // 自动类型转换  it =  00000000 00000000 00000000 01100001
        System.out.println(ch);  // 'a'
        System.out.println(it);  // 97
    }
}

强制类型转换

package com.itheima.type;

public class TypeConversionDemo2 {
    public static void main(String[] args) {
        // 目标:搞清楚什么是强制类型转换。
        int a = 20;
        // byte b = a; // 问题:类型范围大的变量不能直接赋值给类型范围小的变量,会报错
        byte b = (byte) a; // 强制类型转换。
        System.out.println(a);
        System.out.println(b);

        int i = 1500;
        byte j = (byte) i;
        System.out.println(i);
        System.out.println(j); // -36 数据溢出!

        double score = 99.5;
        int it = (int) score;   // ALT + ENTER(万能提示键)
        System.out.println(it); // 99  浮点型强制转换给整型,直接丢掉小数部分,返回整数部分!
    }
}

表达式的自动类型转换

package com.itheima.type;

public class TypeConversionDemo3 {
    public static void main(String[] args) {
        // 目标:搞清楚表达式的自动类型转换
        byte a = 10;
        int b = 20;
        long c = 1000;
        long result = a + b + c;  // 最高类型是long,结果类型肯定是long
        System.out.println(result);

        byte i = 23;
        char j = 'a';
        double k = 0.1;
        double result2 = i + j  + k; // 最高类型是double,结果类型一定是double
        System.out.println(result2);

        byte m = 2;
        short s = 3;
        char t = 'b';
        int result3 = m + s + t;   // 在表达式中,byte、short、char 是直接转换成int类型参与运算的。
        System.out.println(result3);

        // 常见的笔试题
        byte b1 = 112;
        byte b2 = 113;
        // byte b3 = b1 + b2; // 报错!!
        // byte b3 = (byte) (b1 + b2);  // 编译通过,但结果可能失真
        int b3 = b1 + b2;
        System.out.println(b3);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Ayx小蕴

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值