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

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.
java
asked on Stack Overflow Oct 10, 2020 by Sandeep Thaker

2 Answers

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

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

answered on Stack Overflow Oct 10, 2020 by Stephen C • edited Oct 10, 2020 by Stephen C

User contributions licensed under CC BY-SA 3.0