Java"ANSI常量" [英] Java "ANSI constants"

查看:291
本文介绍了Java"ANSI常量"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在浏览Java代码约定时碰到了这个小窍门:

I hit this little tidbit while browsing the Java Code Conventions:

声明为类常量的变量的名称和ANSI常量的名称均应全部大写,且单词之间用下划线(_)分隔. (应避免使用ANSI常数,以方便调试.)

The names of variables declared class constants and of ANSI constants should be all uppercase with words separated by underscores ("_"). (ANSI constants should be avoided, for ease of debugging.)

(摘自此处.)

该文档所说的这些"ANSI常数"是什么?以及它们如何使调试变得更加困难?

What are these "ANSI constants" this document speaks of? And how do they make debugging harder?

文本听起来似乎在变量声明的类常量"(我将其解释为普通的static final变量)和这些"ANSI常量"之间存在二分法,但是我不知道有任何方法可以声明Java中的常量,而不是使它们成为static final变量.

The text makes it sound as if there is a dichotomy between "variables declared class constants" (which I interpret as ordinary static final variables) and these "ANSI constants", but I'm not aware of any way to declare constants in Java other than to make them static final variables.

推荐答案

它们最有可能是指ANSI C常量

They are most likely referring to ANSI C constants, defined as follows

在ANSI C中,可以通过两种方式定义常量:通过#define语句以及通过使用const修饰符.例如,以下两个语句是等效的:

In ANSI C constants can be defined two ways: through the #define statement and through use of the const modifier. For example, the following two statement, are equivalent:

#define LENGTH 10       /* Length of the square in inches */

const int length = 10;  /* Length of the square in inches */

现在Java中显然没有C常量,因为Java不是C:-)(这不是官方编码约定中最奇怪的部分,但是我离题了).

Now there are obviously no C constants in Java, because Java is not C :-) (that's not the strangest part of the official coding conventions, but I digress).

那么为什么他们要编写ANSI常量呢?这很可能只是引用最终图元和不可变对象的一种便捷方式.请记住,在C中,const struct的字段在初始化后无法更新,并且没有相应的Java术语表示常量"(final不能很好地描述不变性的概念).

So why did they write ANSI constants? This is most likely just a convenient way of referring to final primitives and immutable objects. Remember that in C the fields of a const struct can't be updated after initialization, and there's no corresponding Java term for such "constant" (final doesn't capture the notion of immutability very well).

这篇关于Java"ANSI常量"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆