简体   繁体   中英

Why MIN_VALUE and MAX_VALUE in Integer class is defined @native?

I was going through Integer class code and noticed MIN_VALUE and MAX_VALUE are annotated with @native. My question is

  1. What is the purpose of using @native annotation?

  2. Where can we find native code which is used by @native?

  3. Is there any use case where we should use @native annotation in normal programming?

     public final class Integer extends Number implements Comparable<Integer> { /** * A constant holding the minimum value an {@code int} can * have, -2<sup>31</sup>. */ @Native public static final int MIN_VALUE = 0x80000000; /** * A constant holding the maximum value an {@code int} can * have, 2<sup>31</sup>-1. */ @Native public static final int MAX_VALUE = 0x7fffffff;

@native documentation says

/**
 * Indicates that a field defining a constant value may be referenced
 * from native code.
  1. This is mostly intended for automatic native code generation. Thus such constants may be easily used by native code.

  2. The converse: a native code may use such a constant.

  3. If you mean normal==Java, then answer is no.

The Java 8 javadoc includes these sentences:

"Indicates that a field defining a constant value may be referenced from native code. The annotation may be used as a hint by tools that generate native header files to determine whether a header file is required, and if so, what declarations it should contain."

So:

  1. What is the purpose of using @native annotation?

See above.

  1. Where can we find native code which is used by @native?

The generated header files, and any native code references to the above symbols ( MIN_VALUE and MAX_VALUE ) will be in the OpenJDK Java source tree. (I usually track down references to symbols in the source tree using a combination of find , xargs and grep , but there are probably better ways.)

  1. Is there any use case where we should use @native annotation in normal programming?

One case would be if you are implementing your own APIs which involve native code and generated native header files.

But if you mean use cases in (pure) Java code, I can't think of any 1 .

1 - Apart from the ones already noted (ie JNI, etc native header generators) and fatuous ones (eg a tool for listing all @Native annotations).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM