java中常量和字面常量_如何在Java中使用常量

java中常量和字面常量

A constant is a variable whose value cannot change once it has been assigned. Java doesn't have built-in support for constants, but the variable modifiers static and final can be used to effectively create one.

常数是一个变量,其值一旦分配便无法更改。 Java没有对常量的内置支持,但是可以使用变量修饰符staticfinal来有效地创建一个常量。

Constants can make your program more easily read and understood by others. In addition, a constant is cached by the JVM as well as your application, so using a constant can improve performance. 

常量可以使您的程序更易于他人阅读和理解。 此外,JVM和您的应用程序还会缓存一个常量,因此使用常量可以提高性能。

静态修饰符 ( Static Modifier )

This allows a variable to be used without first creating an instance of the class; a static class member is associated with the class itself, rather than an object. All class instances share the same copy of the variable.

这允许在不首先创建实例的情况下使用变量; 静态类成员与类本身而不是对象相关联。 所有类实例共享相同的变量副本。

This means that another application or main() can easily use it.

这意味着另一个应用程序或main()可以轻松使用它。

For example, class myClass contains a static variable days_in_week:

例如,类myClass包含静态变量days_in_week:


public class myClass {
  static int days_in_week = 7;
}

Because this variable is static, it can be used elsewhere without explicitly creating a myClass object:

由于此变量是static ,因此可以在其他位置使用它,而无需显式创建myClass对象:


public class myOtherClass {  
  static void main(String[] args) {
      System.out.println(myClass.days_in_week);
  }
}

最终修饰符 ( Final Modifier )

The final modifier means that the variable's value cannot change. Once the value is assigned, it cannot be reassigned. 

最终修饰符表示变量的值不能更改。 分配值后,将无法重新分配。

Primitive data types (i.e., int, short, long, byte, char, float, double, boolean) can be made immutable/unchangeable using the final modifier.

原始数据类型 (即int,short,long,byte,char,float,double,boolean)可以使用final修饰符设置为不可变/不可更改。

Together, these modifiers create a constant variable.

这些修饰符一起创建一个常量。


static final int DAYS_IN_WEEK = 7;

Note that we declared DAYS_IN_WEEK in all caps once we added the final modifier. It's a long-standing practice among Java programmers to define constant variables in all caps, as well as to separate words with underscores.

请注意,添加最终修饰符后,我们在所有大写字母中都声明了DAYS_IN_WEEK。 在Java程序员中,定义所有大写形式的常量变量以及用下划线分隔单词是一种长期的惯例。

Java doesn't require this formatting but it makes it easier for anyone reading the code to immediately identify a constant

Java不需要这种格式,但是它使任何阅读代码的人都可以立刻识别一个常量

常数变量的潜在问题 ( Potential Problems With Constant Variables )

The way the final keyword works in Java is that the variable's pointer to the value cannot change. Let's repeat that: it's the pointer that cannot change the location to which it's pointing.

final关键字在Java中的工作方式是变量的指向值的指针不能更改。 让我们重复一遍:它是指针,无法更改其指向的位置。

There's no guarantee that the object being referenced will stay the same, only that the variable will always hold a reference to the same object. If the referenced object is mutable (i.e. has fields that can be changed), then the constant variable may contain a value other than what was originally assigned. 

不能保证被引用的对象会保持不变,只是不能保证变量始终包含对同一对象的引用。 如果引用的对象是可变的(即,具有可以更改的字段),则常量变量可以包含最初分配的值以外的其他值。

翻译自: https://www.thoughtco.com/constant-2034049

java中常量和字面常量

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值