了解有关在Java中使用常量的信息

There are many values in the real world which will never change. A square will always have four sides, PI to three decimal places will always be 3.142, and a day will always have 24 hours. These values remain constant. When writing a program it makes sense to represent them in the same way - as values that will not be modified once they have been assigned to a variable. These variables are known as constants.

现实世界中有许多永远不变的价值观。 一个正方形将始终具有四个边,PI到小数点后三个位将始终为3.142,一天将始终具有24小时。 这些值保持不变。 编写程序时,以相同的方式表示它们是有意义的-因为一旦将值分配给变量,这些值将不会被修改。 这些变量称为常量。

将变量声明为常量 ( Declaring a Variable As a Constant )

In declaring variables we showed that it’s easy to assign a value to an int variable:

声明变量时,我们表明将值赋给int变量很容易:

 int numberOfHoursInADay = 24; 

We know this value is never going to change in the real world so we make sure it doesn’t in the program. This is done by adding the keyword modifier

我们知道这个值在现实世界中永远不会改变,因此我们确保它不会在程序中出现。 这是通过添加关键字修饰符来完成的

final

In addition to the

final
keyword you should have noticed that the case of the variable name has changed to be uppercase as per the standard Java naming convention. This makes it far easier to spot which variables are constants in your c

If we now try and change the value of

we will get the following error from the compiler:

The same goes for any of the other primitive data type variables. To make them into constants just add the

final
keyword to their declarat

Where to Declare Constants

As with normal variables you want to limit the scope of constants to where they are used. If the value of the constant is only needed in a method then declare it there:

 {
 return days * NUMBER_OF_HOURS_IN_A_DAY;

If it’s used by more than one method then declare it at the top of the class definition:

public class AllAboutHours{
 public int calculateHoursInDays(int days)
 return days * NUMBER_OF_HOURS_IN_A_DAY;
 public int calculateHoursInWeeks(int weeks)
 final int NUMBER_OF_DAYS_IN_A_WEEK = 7;
 }

Notice how I’ve also added the keyword modifiers

private
and
NUMBER_OF_HOURS_IN_A_DAY
. This means that the constant can only be used by its class (hence the
public
constant if you want other classes to have access to it. The one instance<

Using the Final Keyword with Objects

It’s very important to realize that when it comes to objects, Java does not support constants as you might expect. If you assign a variable to an object using the

final
keyword it means the variable will only ever hold the reference to that object. It cannot be changed to reference another object. However, it does not mean that the contents of the object cannot cha

A Brief Note on the Const Keyword

You may have noticed in the reserved words list that there is a keyword called

Java language<
final

In addition to the

final
keyword you should have noticed that the case of the variable name has changed to be uppercase as per the standard Java naming convention . This makes it far easier to spot which variables are constants in your c

If we now try and change the value of

we will get the following error from the compiler:

The same goes for any of the other primitive data type variables. To make them into constants just add the

final
keyword to their declarat

Where to Declare Constants

As with normal variables you want to limit the scope of constants to where they are used. If the value of the constant is only needed in a method then declare it there:

 {
 return days * NUMBER_OF_HOURS_IN_A_DAY;

If it’s used by more than one method then declare it at the top of the class definition:

public class AllAboutHours{
 public int calculateHoursInDays(int days)
 return days * NUMBER_OF_HOURS_IN_A_DAY;
 public int calculateHoursInWeeks(int weeks)
 final int NUMBER_OF_DAYS_IN_A_WEEK = 7;
 }

Notice how I’ve also added the keyword modifiers

private
and
NUMBER_OF_HOURS_IN_A_DAY
. This means that the constant can only be used by its class (hence the
public
constant if you want other classes to have access to it. The one instance<

Using the Final Keyword with Objects

It’s very important to realize that when it comes to objects, Java does not support constants as you might expect. If you assign a variable to an object using the

final
keyword it means the variable will only ever hold the reference to that object. It cannot be changed to reference another object. However, it does not mean that the contents of the object cannot cha

A Brief Note on the Const Keyword

You may have noticed in the reserved words list that there is a keyword called

Java language<

翻译自: https://www.thoughtco.com/using-constants-2034317

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值