Java constants

Summary
How to get the best benefits of at least some of the C/C++ textual preprocessor's features in Java: constants and conditional compilation. (800 words)

benefits of using the C preprocessor's facilities to define compile-time constants and conditionally compiled code.

Java has gotten rid of the entire notion of a textual preprocessor (if you take Java as a "descendent" of C/C++). We can, however, get the best benefits of at least some of the C preprocessor's features in Java: constants and conditional compilation.

One of the inarguably good features of the C preprocessor is the ability to define compile-time constants using a textual name to represent some value. This makes it easier to read and maintain. It is also faster at runtime than using a normal variable.

An arguably abused feature of the C preprocessor is the use of #define along with #ifdef and friends to conditionally compile entire blocks of code. I say it's arguable since people often use this facility to deal with platform-specific issues (and that's both the good point and the bad point).

In C, one could define some constants in a header file via:


#define MY_BDATE 10
#define     SILLY_PLATFORM

and then getting access to those constants by using #include to include them in a code file, and then using them:


fprintf (stderr, "My birthday is on the %d" "th!/n", MY_BDATE);

The equivalent in Java can be done by creating public static final variables in a Java interface:


interface ConstantStuff
{

    public static final int MY_BDATE = 10;

    public static final boolean SillyPlatform = true;


}

Then we can access them by using import to make the interface visible to us and then using the constants:


System.out.println ("My birthday is on the " + ConstantStuff.MY_BDATE + "th!");

The C preprocessor can conditionally strip out large areas of text if a given preprocessor constant was or was not defined.


#if defined(SILLY_PLATFORM)
/* Lot's of nasty code to deal with the stupidities of the

* SILLY platform.

*/
#else
/* Code to deal with other, normal platforms. */
#endif

Many folks lament that this capability is absent from Java. Remember, one of the reasons that Java is so wonderful is that the language is so much better defined, so system-specific code like that should not even be necessary.

Be that as it may, you can still get that sort of conditionally compiled code from the compiler directly! You just use public static final boolean constants as the condition for a regular if statement. The Java compiler is smart enough to recognize that as a special case and it can completely eliminate the test and the code of the appropriate conditional branch.

So just write the conditional statement as usual.


if (ConstantStuff.SillyPlatform)
    {
    // Code to be used if platform is true *at compile time*.
    }
else
    {
    // Code to be use if platform is false *at compile time*.
    }

I don't know about you, but I hate having to write that long-winded interface name before using any of those constants. So, I just have my class that is going to use those constants implement the interface. Then I can just use the name directly, assuming there are no name clashes (in which case you'll have to distinguish them using the full names).

I've put all of these fun things together in a couple of simple Java applications. Constants (http://www.javaworld.com/javatips/javatip5/Constants.java) implements the interface and uses the constants directly while Constants2 (http://www.javaworld.com/javatips/javatip5/Constants2.java) uses fully qualified names to access the constants.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值