2

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.
2
  • 1
    Read the rest of the javadoc: "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."
    – Andreas
    Oct 10, 2020 at 7:15
  • Regarding your second question, lookup the Java source code at GitHub, it also contains most of the native code.
    – Zabuzard
    Oct 10, 2020 at 10:50

2 Answers 2

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

2
  • "automatic native code generation" --- You mean JIT? Isn't that what JIT does, generate native code for faster execution? So you must be referring to JIT, right? --- Nooo.... Perhaps you are referring to JNI.... Yeah.... Maybe? Clarification would be good.
    – Andreas
    Oct 10, 2020 at 7:18
  • 1
    @Andreas No, it doesn't refer to JIT. It also doesn't refer to JNI, not exactly. Any tool, which might generate native code can sue these annotations as hints. Its not specified which tools these might be. There are a myriad of tools out there that generate glue code/native code, some of them might use JNI, some JNA.
    – Polygnome
    Oct 10, 2020 at 7:26
2

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.