Java基础【变量】

为什么有变量

在一个小村庄里,有一位年轻的农夫叫小明。他勤奋努力地耕种着自己的土地,但总是忘记了每个地块上种植的作物。于是,小明决定引入变量来解决这个问题。他给每个地块取了一个独特的名字,并用标签贴在上面,如A、B、C等。每次种植时,他记录下每块地的作物种类。这样,通过查看变量,小明能够清楚地知道每个地块上种植的作物,不再混淆。随着时间的推移,他的农田变得井然有序,收成也更加丰盛。小明明白,有了变量,他能更好地管理和利用资源,让农田生机勃发。

变量是内存中的一个数据存储空间,没有固定的值。就像一块有名字的土地,你可以在上面种植任何的农作物。

变量的组成

变量 = 变量类型 + 名称 + 值

土地 = 土地类型(黑土、黄土等)+ 土地编号 + 农作物

public class Main {
    public static void main(String[] args) {
        // 定义一个整型变量
        int myNumber = 10; // 变量名:myNumber,值:10,数据类型:int

        // 输出变量的值
        System.out.println("My number is: " + myNumber);
    }
}

不同的地质适合不同的作物,不同的变量类型可以存储不同的数据,但是土地可以有任意的名称,同理变量的名称也由程序员自主决定。

为了更好的记忆,一般变量名称都有特定的规范,例如,不起“ABC”这样的无意义名称,一般起“蔬菜”,“水果”有意义名称 命名的规范也有要求例如,驼峰命名法,例如 myNumbe 好比排版中字体的要求“宋体 3号”等

变量类型(数据类型)

基本数据类型

  • 整数类型:byte short int long
  • 浮点数类型: float double
  • 字符型: char
  • 布尔类型 :bool

引用数据类型

  • 类: class
  • 接口:interface
  • 数组:array

特殊类型

  • 字符串类型:String
  • 枚举类型:Enum
  • 空类型:Void

变量使用说明书(注意事项)

  • 变量的类型不同,所占空间大小不同
  • 变量必须先声明,后使用
  • 数据/值符变量类型时可以变化
  • 变量在同一作用域内不能重名
public class Main {
    public static void main(String[] args) {
        // 变量的类型不同,所占空间大小不同
        int num = 10;
        double decimalNum = 3.14;

        // 变量必须先声明,后使用
        int x;
        x = 5;
        System.out.println("x 的值为: " + x);

        // 数据/值赋给变量时可以变化
        int y = 5;
        System.out.println("y 的值为: " + y);
        y = 10;
        System.out.println("y 的新值为: " + y);

        // 变量在同一作用域内不能重名
        int a = 1;
        {
            int b = 2;
            //int a = 3;  // 编译错误,变量名重复
        }
    }
}

数据类型之间的转换

在这里插入图片描述

图例为隐式自动类型转换

public class Main {
    public static void main(String[] args) {
        // char 可以自动转换为 int
        char charValue = 'A';
        int intValue = charValue;
        System.out.println("char 转换为 int:" + intValue);

        // int 可以自动转换为 long
        int intValue2 = 123;
        long longValue = intValue2;
        System.out.println("int 转换为 long:" + longValue);

        // long 可以自动转换为 float
        long longValue2 = 1234567890L;
        float floatValue = longValue2;
        System.out.println("long 转换为 float:" + floatValue);

        // float 可以自动转换为 double
        float floatValue2 = 3.14f;
        double doubleValue = floatValue2;
        System.out.println("float 转换为 double:" + doubleValue);
    }
}

强制类型转换

public class Main {
    public static void main(String[] args) {
        // 需要使用强制类型转换:int 转换为 char
        int intValue = 65;
        char charValue = (char) intValue; // 强制类型转换
        System.out.println("int 转换为 char:" + charValue);

        // 需要使用强制类型转换:long 转换为 int
        long longValue = 1234567890L;
        int intValue2 = (int) longValue; // 强制类型转换
        System.out.println("long 转换为 int:" + intValue2);

        // 需要使用强制类型转换:double 转换为 int
        double doubleValue = 3.1415926;
        int intValue3 = (int) doubleValue; // 强制类型转换
        System.out.println("double 转换为 int:" + intValue3);

        // 需要使用强制类型转换:int 转换为 byte
        int intValue4 = 200;
        byte byteValue = (byte) intValue4; // 强制类型转换
        System.out.println("int 转换为 byte:" + byteValue);
    }
}

数据类型和String之间的转换

基本数据类型转化为String

public class Main {
    public static void main(String[] args) {
        int intValue = 42;
        double doubleValue = 3.14;
        boolean boolValue = true;

        // 使用 String.valueOf 方法将基本数据类型转换为字符串
        String intStr = String.valueOf(intValue);
        String doubleStr = String.valueOf(doubleValue);
        String boolStr = String.valueOf(boolValue);

        System.out.println("int 转换为 String: " + intStr);
        System.out.println("double 转换为 String: " + doubleStr);
        System.out.println("boolean 转换为 String: " + boolStr);
    }
}

String转换为基本数据类型

public class Main {
    public static void main(String[] args) {
        String intStr = "42";
        String doubleStr = "3.14";
        String boolStr = "true";

        // 使用相应的包装类的 parseXxx 静态方法将字符串转换为基本数据类型
        int intValue = Integer.parseInt(intStr);
        double doubleValue = Double.parseDouble(doubleStr);
        boolean boolValue = Boolean.parseBoolean(boolStr);

        System.out.println("String 转换为 int: " + intValue);
        System.out.println("String 转换为 double: " + doubleValue);
        System.out.println("String 转换为 boolean: " + boolValue);
    }
}

不同的变量类型适用于什么样的业务场景?

  • 整型(int、long等):适用于存储整数值,如计数器、年龄等。

  • 浮点型(float、double等):适用于存储带有小数点的数值,如货币金额、科学计算等。

  • 字符型(char):适用于存储单个字符,如字母、数字或符号等。

  • 布尔型(boolean):适用于存储逻辑值,表示真(true)或假(false)。

  • 字符串(String):适用于存储文本数据,如名称、描述等。

  • 数组(Array):适用于存储一组相同类型的数据,用于批量处理数据。

  • 对象(Object):适用于存储复杂的数据结构,如用户信息、产品信息等。

  • 枚举(Enum):适用于定义一组常量值,如季节、星期等。

  • 33
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值