Java中的#define
我开始用 Java 编程,我想知道是否存在与 C++ #define
等效的东西。
谷歌的快速搜索表明它不存在,但有人可以告诉我是否存在类似的东西 在Java中? 我正在努力使我的代码更具可读性。
我希望能够编写 myArray[PROTEINS]
而不是 myArray[0]
例子。
I'm beginning to program in Java and I'm wondering if the equivalent to the C++ #define
exists.
A quick search of google says that it doesn't, but could anyone tell me if something similar exists
in Java? I'm trying to make my code more readable.
Instead of myArray[0]
I want to be able to write myArray[PROTEINS]
for example.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
不,因为没有预编译器。但是,在您的情况下,您可以实现相同的效果,如下所示:
编译器会注意到
PROTEINS
永远不会改变,因此将内联它,这或多或少是您想要的。请注意,常量的访问修饰符在这里并不重要,因此如果您想在多个类中重用同一常量,则可以将其设置为
public
或protected
而不是 private。No, because there's no precompiler. However, in your case you could achieve the same thing as follows:
The compiler will notice that
PROTEINS
can never, ever change and so will inline it, which is more or less what you want.Note that the access modifier on the constant is unimportant here, so it could be
public
orprotected
instead of private, if you wanted to reuse the same constant across multiple classes.注释空间太小,所以这里为您提供一些有关
static final
使用的更多信息。正如我在对 Andrzej 的答案的评论中所说,只有原始和String
作为文字直接编译到代码中。为了演示这一点,请尝试以下操作:您可以通过创建三个类(在单独的文件中)来查看其实际效果:
编译这些类并运行测试,它会打印:
现在,将
Constants
更改为具有不同的值每个都只编译类Constants
。当您再次执行Test
时(无需重新编译类文件),它仍然会打印INT_VALUE
的旧值,但不会打印VALUE
。例如:运行测试而不重新编译
Test.java
:请注意,与
static final
一起使用的任何其他类型都将保留作为引用。类似于 C/C++
#if
/#endif
,常量文字或通过带有原语的static final
定义的常量文字,用于常规 Java < code>if 条件并计算为false
将导致编译器剥离if
块中语句的字节码(它们不会生成)。“...code here...”处的代码不会被编译为字节码。但如果您将
DEBUG
更改为true
那么它就会。Comment space too small, so here is some more information for you on the use of
static final
. As I said in my comment to the Andrzej's answer, only primitive andString
are compiled directly into the code as literals. To demonstrate this, try the following:You can see this in action by creating three classes (in separate files):
Compile these and run Test, which prints:
Now, change
Constants
to have a different value for each and just compile classConstants
. When you executeTest
again (without recompiling the class file) it still prints the old value forINT_VALUE
but notVALUE
. For example:Run Test without recompiling
Test.java
:Note that any other type used with
static final
is kept as a reference.Similar to C/C++
#if
/#endif
, a constant literal or one defined throughstatic final
with primitives, used in a regular Javaif
condition and evaluates tofalse
will cause the compiler to strip the byte code for the statements within theif
block (they will not be generated).The code at "...code here..." would not be compiled into the byte code. But if you changed
DEBUG
totrue
then it would be.通常,您会将“常量”放入类本身中。请注意,编译器可以优化对其的引用,因此除非重新编译所有使用的类,否则不要更改它。
编辑周期...
SIZE=4
。同时编译Bar
因为你的编译器可能刚刚在最后一个编译周期写入了“4”!You'd normally put "constants" in the class itself. And do note that a compiler is allowed to optimize references to it away, so don't change it unless you recompile all the using classes.
Edit cycle...
SIZE=4
. Also compileBar
because you compiler may have just written "4" in the last compilation cycle!Java 没有通用的
define
预处理器指令。对于常量,建议将它们声明为
static Finals
,如这样的声明将由编译器内联(如果该值是编译时常量)。
另请注意,公共静态最终常量字段是公共接口的一部分,它们的值不应更改(因为编译器内联它们)。如果您确实更改了该值,则需要重新编译引用该常量字段的所有源。
Java doesn't have a general purpose
define
preprocessor directive.In the case of constants, it is recommended to declare them as
static finals
, like inSuch declarations would be inlined by the compilers (if the value is a compile-time constant).
Please note also that public static final constant fields are part of the public interface and their values shouldn't change (as the compiler inlines them). If you do change the value, you would need to recompile all the sources that referenced that constant field.
有 Java 预处理器,它提供了 #define、#ifdef、#ifndef 等指令许多其他人,例如 PostgresJDBC 团队使用它来为不同情况生成源并且不重复代码。
There is preprocessor for Java which provides directives like #define, #ifdef, #ifndef and many others, for instance PostgresJDBC team uses it to generate sources for different cases and to not duplicate code.
最易读的解决方案是使用 静态导入。那么您将不需要需要使用
AnotherClass.constant
。编写一个类,将常量作为
public static
字段。然后只需在需要常量的地方使用静态导入即可。
要了解有关静态导入的更多信息,请参阅此堆栈溢出问题 。
Most readable solution is using Static Import. Then you will not need to use
AnotherClass.constant
.Write a class with the constant as
public static
field.Then just use Static Import where you need the constant.
To know more about Static Import please see this stack overflow question.
Manifold 预处理器 作为 javac 实现编译器插件专为 Java 源代码的条件编译而设计。它使用熟悉的 C/C++ 风格的指令:#define、#undef、#if、#elif、#else、#endif、#error。和#警告。
它有 Maven 和 Gradle 插件。
Manifold Preprocessor implemented as a javac compiler plugin is designed exclusively for conditional compilation of Java source code. It uses familiar C/C++ style of directives: #define, #undef, #if, #elif, #else, #endif, #error. and #warning.
It has Maven and Gradle plugins.
最简单的答案是“没有直接方法获取它,因为没有预编译器”
但你可以自己做。使用类,然后将变量定义为final,以便在整个程序中将其视为常量
不要忘记将final和variable用作public或protected而不是private,否则您将无法从该类外部访问它
Simplest Answer is "No Direct method of getting it because there is no pre-compiler"
But you can do it by yourself. Use classes and then define variables as final so that it can be assumed as constant throughout the program
Don't forget to use final and variable as public or protected not private otherwise you won't be able to access it from outside that class
Java Primitive Specializations Generator 支持
/* 和 */
,/* 定义 */
和/* if */ ... /* elif */ ... /* endif */
块,允许执行某种操作Java代码中的宏生成,类似于这个答案中提到的java-comment-preprocessor。JPSG 有 Maven 和 Gradle 插件。
Java Primitive Specializations Generator supports
/* with */
,/* define */
and/* if */ ... /* elif */ ... /* endif */
blocks which allow to do some kind of macro generation in Java code, similar to java-comment-preprocessor mentioned in this answer.JPSG has Maven and Gradle plugins.