Color.red 和 Color.RED 的区别 [英] Difference between Color.red and Color.RED
问题描述
setXxx(Color.red)
和 setXxx(Color.RED)
的定义之间的真正区别是什么?
What's the real difference between definitions for setXxx(Color.red)
and setXxx(Color.RED)
?
我在网上找到了以下解释.都是关于命名约定的吗?
I've found the following explanation on the web. Is it all about naming conventions?
Java 最初定义了一些小写的颜色常量名称,这违反了常量使用大写的命名规则.它们适用于所有 Java 版本:Color.black、Color.darkGray、Color.gray、Color.lightGray、Color.white、Color.magenta、Color.red、Color.pink、Color.orange、Color.yellow、Color.green, Color.cyan, Color.blue
Java originally defined a few color constant names in lowercase, which violated the naming rule of using uppercase for constants. They are available in all versions of Java: Color.black, Color.darkGray, Color.gray, Color.lightGray, Color.white, Color.magenta, Color.red, Color.pink, Color.orange, Color.yellow, Color.green, Color.cyan, Color.blue
Java 1.4 为常量添加了正确的大写名称:Color.BLACK、Color.DARK_GRAY、Color.GRAY、Color.LIGHT_GRAY、Color.WHITE、Color.MAGENTA、Color.RED、Color.PINK、Color.ORANGE、Color.黄色、颜色.绿色、颜色.青色、颜色.蓝色
Java 1.4 added the proper uppercase names for constants: Color.BLACK, Color.DARK_GRAY, Color.GRAY, Color.LIGHT_GRAY, Color.WHITE, Color.MAGENTA, Color.RED, Color.PINK, Color.ORANGE, Color.YELLOW, Color.GREEN, Color.CYAN, Color.BLUE
推荐答案
代码本身:
public final static Color red = new Color(255, 0, 0);
public final static Color RED = red;
大写字母是在 JDK 1.4 中引入的(为了符合其命名约定,声明常量必须为大写).
The upper case letters were introduced in JDK 1.4 (to conform to its naming convention, stating that constants must be in upper-case).
本质上没有任何区别(字母大小写除外).
In essence, there are no difference at all (except letter casing).
如果我真的想勇敢一点,Oracle 可能会疯狂并删除小写的常量,但这会破坏在 JDK 1.4 之前编写的所有其他代码.你永远不知道,我建议坚持使用大写字母作为常量.不过,它首先必须被弃用(如 Andrew Thompson 所述).
If I want to really be brave, Oracle might go wild and remove constants that is lower-cased, but then that would break all other code that's written pre-JDK 1.4. You never know, I would suggest sticking to uppercase letters for constants. It first has to be deprecated though (as mentioned by Andrew Thompson).
这篇关于Color.red 和 Color.RED 的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!