|NO.Z.00028|——————————|^^ 笔试 ^^|——|Java&变量数据类型.V12|——|Java.v12|数据类型.v07|整数类型编程|

一、整数类型概念
### --- 数据类型

~~~     ——>Java语言中描述整数数据的类型有:# byte、short、int、long、荐int类型
~~~     ——>其中byte类型在内存空间中占1个字节,表示范围:-2^7 ~ 2^7-1
~~~     ——>其中short类型在内存空间中占2个字节,表示范围是:-2^15 ~ 2^15-1
~~~     ——>其中int类型在内存空间中占4个字节,表示范围是:-2^31 ~ 2^31-1
~~~     ——>其中long类型在内存空间中占8个字节,表示范围是:-2^63 ~ 2^63-1
~~~     ——>在Java程序中直接写出的整数数据叫做直接量/字面值/常量,默认为int类型,若希望表达更大的直接量,则在直接量的后面加上l或者L,推荐L
二、整数类型的编程使用
### --- 编程实现整数类型的使用

/*
    编程实现整数类型的使用
*/
public class IntTest {
    
    public static void main(String[] args) {
        
        // 1.声明一个byte类型的变量并初始化
        byte b1 = 25;
        //byte b1 = 250;     // 错误: 不兼容的类型: 从int转换到byte可能会有损失  250这样直接写出的整数数据叫做直接量/常量/字面值 默认为int类型 
        // 2.打印变量的数值
        System.out.println("b1 = " + b1); // b1 = 25
        
        System.out.println("---------------------------------------------");
        // 3.声明一个short类型的变量并初始化
        short s1 = 250;
        //short s1 = 250250;  // 错误:不兼容的类型:从int转换到short可能会有损失
        System.out.println("s1 = " + s1); // s1 = 250
        
        System.out.println("---------------------------------------------");
        // 4.声明一个int类型的变量并初始化
        int i1 = 250250;
        //int i1 = 2502505006; // 错误: 整数太大   默认为int类型,这个数据自身已经出错,无法表示
        //int i1 = 2502505006L;  // 错误:不兼容的类型:从long转换到int可能会有损失
        System.out.println("i1 = " + i1); // i1 = 250250
        
        System.out.println("---------------------------------------------");
        // 5.声明一个long类型的变量并初始化,若描述比long类型还大的数据则使用java.math.BigInteger类型
        long g1 = 2502505006L;
        System.out.println("g1 = " + g1); // g1 = 2502505006
        
        System.out.println("---------------------------------------------");
        // 6.请问下面的代码是否有错误?若有请指出并说明原因
        //int i2 = 25;
        //byte b2 = i2;  // 错误: 不兼容的类型: 从int转换到byte可能会有损失
        //System.out.println("b2 = " + b2);
        
    }
}
### --- 编译运行

~~~     # 编译
C:\Users\Administrator\Desktop\project>javac IntTest.java
~~~     # 打印输出

C:\Users\Administrator\Desktop\project>java IntTest
b1 = 25
---------------------------------------------
s1 = 250
---------------------------------------------
i1 = 250250
---------------------------------------------
g1 = 2502505006
---------------------------------------------

附录一:整数类型笔试考点
### --- 声明变量:数字不能开头

~~~     ——>到底是标识符还是直接量,就比较容易混淆。
        long g1 = 2502505006L;
        System.out.println("g1 = " + g1); // g1 = 2502505006
### --- 判断代码的错误
~~~     请问下面的代码是否有错误?若有请指出并说明原因
~~~     变量和直接量之间的区别

        //int i2 = 25;
        //byte b2 = i2;                         // 错误: 不兼容的类型: 从int转换到byte可能会有损失
        //System.out.println("b2 = " + b2);

 
 
 
 
 
 
 
 
 

Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
                                                                                                                                                   ——W.S.Landor

 

 

posted on   yanqi_vip  阅读(23)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示