Difference between Color.red and Color.RED

前端 未结 3 727
臣服心动
臣服心动 2020-11-30 03:54

What\'s the real difference between definitions for setXxx(Color.red) and setXxx(Color.RED)?

I\'ve found the following explanation on the w

相关标签:
3条回答
  • 2020-11-30 04:08

    Java defined some color constant names in lowercase, which violated the naming rule of using uppercase for constants. Heres the code for the color red:

    public final static Color red = new Color(255, 0, 0); 
    

    Later on they made the same colors but in uppercase.

    public final static Color RED = red;
    

    So there is really no difference. They are all the same, as you can tell by the code.

    public final static Color red = new Color(255, 0, 0);
    public final static Color RED = red;
    

    Hope this helps!

    0 讨论(0)
  • 2020-11-30 04:11

    There's the code itself:

    public final static Color red = new Color(255, 0, 0);
    
    public final static Color RED = red;
    

    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).


    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).

    0 讨论(0)
  • 2020-11-30 04:20

    There is really no difference. See the Color class:

    /**
     * The color red.  In the default sRGB space.
     */
    public final static Color red       = new Color(255, 0, 0);
    
    /**
     * The color red.  In the default sRGB space.
     * @since 1.4
     */
    public final static Color RED = red;
    
    0 讨论(0)
提交回复
热议问题