Java 变量介绍

变量的定义 

变量是指内存里的一段区域

变量使用时的注意

Java里变量必须先声明,后使用

使用变量名来访问内存中的数据

变量的作用域,其定义所在的一对{}里

变量只有在作用域才生效

同一个作用域不能定义重名的变量

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
class VariableTest1
{
    public static void main(String[] args)
    {
        //System.out.println("Hello World!");   单行注释           
                //声明变量类型并赋值
                int  myAge = 12;
                //单行打印变量值
        System.out.println(myAge);
    }
}
或者
class VariableTest1
{
    public static void main(String[] args)
    {
        //System.out.println("Hello World!");
        int myAge = 12;
        System.out.println(myAge);
        //声明变量类型
        int chenXi;
        //赋值变量
        chenxi = 13;
        //单行打印变量
        System.out.println(chenXi);
    }
}

变量的类型:基本数据类型、引用数据类型

基本数据类型:数值型(整数型(byte、short、int、long)、浮点数型(flote、double))、字符型(char)、布尔型(boolean)

引用数据类型: 类(class)字符串也属于这个类型、接口(interface)、数组([])

基本数据类型的使用

整数类型:Java的整数有固定表述范围和长度,不收具体os的影响,保证Java的可移植性;整数类型通常默认是int型,声明long型变量值后面通常加‘l'或者‘L’,Java声明整数数据类型时通常默认int类型,除非明显不足以保证较大的数时使用long

类型 占用存储空间 表示范围
byte 1字节=8byte -128~127
short 2字节 -2的15次方~2的15方-1
int 4字节 -2的31次方~2的31次方
long 8字节 -2的63次方~2的63次方
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
class VariableTest1
{
    public static void main(String[] args)
    {
        //System.out.println("Hello World!");
        int myAge = 12;
        System.out.println(myAge);
        //声明变量类型
        int chenXi;
        //赋值变量
        chenXi = 13;
        //单行打印变量
        System.out.println(chenXi);
        byte b1 = 12;
        byte b2 = -128;
//     编译出错
        b2 = 128;
        System.out.println(b1);
        System.out.println(b2);
    }
}
---------- javac ----------
VariableTest1.java:24: 错误: 不兼容的类型: 从int转换到byte可能会有损失
        b2 = 128;
             ^
1 个错误
 
输出完成 (耗时 0 秒) - 正常终止

  测试其它类型的

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
32
33
34
35
36
37
38
class VariableTest1
{
    public static void main(String[] args)
    {
        //System.out.println("Hello World!");
        int myAge = 12;
        System.out.println(myAge);
        //声明变量类型
        int chenXi;
        //赋值变量
        chenXi = 13;
        //单行打印变量
        System.out.println(chenXi);
        byte b1 = 12;
        byte b2 = -128;
        //b2 = 128;
        System.out.println(b1);
        System.out.println(b2);
        short s1 = 128;
        int i1 = 12345;
        long l1 = 54321987698765421L;
        System.out.println(l1);
        System.out.println(i1);
        System.out.println(s1);
    }
}
 
 
---------- java ----------
12
13
12
-128
54321987698765421
12345
128
 
输出完成 (耗时 0 秒) - 正常终止

  浮点型常量:与整数类型类似Java浮点型数也有固定的数表示范围和字段长度,不受系统影响;

浮点型常量有两种表示是方式:1进制数型如3.14;2科学计数法形式如5.21E

float:单精度尾数可以精确到7位有效数字,很多情况精度不够,难以满足需求

double: 双精度,是单精度的2倍通常采用双精度

java:浮点型常量默认double型,声明float型常量,和面必须加f或F;注意E10倍

类型 占用空间 表示范围
单精度 4字节 -3.403E38~3.403E38
双精度 8字节 -1.798E308~1.798E308

 测试

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
32
33
34
35
36
37
38
39
40
41
42
43
class VariableTest1
{
    public static void main(String[] args)
    {
        //System.out.println("Hello World!");
        int myAge = 12;
        System.out.println(myAge);
        //声明变量类型
        int chenXi;
        //赋值变量
        chenXi = 13;
        //单行打印变量
        System.out.println(chenXi);
        byte b1 = 12;
        byte b2 = -128;
        //b2 = 128;
        System.out.println(b1);
        System.out.println(b2);
        short s1 = 128;
        int i1 = 12345;
        long l1 = 54321987698765421L;
        System.out.println(l1);
        System.out.println(i1);
        System.out.println(s1);、
//单精度加f或F
        float f1 = 12.3f;
        double d1 = 123.3;
        System.out.println(f1+2);
        System.out.println(d1+15);
    }
}
---------- java ----------
12
13
12
-128
54321987698765421
12345
128
14.3
138.3
 
输出完成 (耗时 0 秒) - 正常终止

  字符类型char两个字节声明格式通常‘’单引号字符内部只能一个字符

代码

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
class VariableTest1
{
    public static void main(String[] args)
    {
        //System.out.println("Hello World!");
        int myAge = 12;
        System.out.println(myAge);
        //声明变量类型
        int chenXi;
        //赋值变量
        chenXi = 13;
        //单行打印变量
        System.out.println(chenXi);
        byte b1 = 12;
        byte b2 = -128;
        //b2 = 128;
        System.out.println(b1);
        System.out.println(b2);
                // 变量声明及定义
        char c1 = 'x';
        System.out.println(c1);
    }
}
---------- java ----------
12
13
12
-128
x
 
输出完成 (耗时 0 秒) - 正常终止

  布尔型变量类型

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
32
33
34
35
36
37
class VariableTest1
{
    public static void main(String[] args)
    {
        //System.out.println("Hello World!");
        int myAge = 12;
        System.out.println(myAge);
        //声明变量类型
        int chenXi;
        //赋值变量
        chenXi = 13;
        //单行打印变量
        System.out.println(chenXi);
        byte b1 = 12;
        byte b2 = -128;
        //b2 = 128;
        System.out.println(b1);
        System.out.println(b2);
        char c1 = 'x';
        System.out.println(c1);
        //布尔型只有两个值true;false
        boolean B = true;
        boolean b = false;
        System.out.println(B);
        System.out.println(b);
    }
}
---------- java ----------
12
13
12
-128
x
true
false
 
输出完成 (耗时 0 秒) - 正常终止

  

 

posted @   烟雨楼台,行云流水  阅读(350)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 葡萄城 AI 搜索升级:DeepSeek 加持,客户体验更智能
· 什么是nginx的强缓存和协商缓存
· 一文读懂知识蒸馏
点击右上角即可分享
微信分享提示