为什么Integer类中的MIN_VALUE和MAX_VALUE被定义为@native?

huangapple go评论65阅读模式
英文:

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

问题

我浏览了整数类的代码,并注意到 MIN_VALUE 和 MAX_VALUE 都用 @native 进行了注解。我的问题是:

  1. 使用 @native 注解的目的是什么?
  2. 我们在哪里可以找到被 @native 使用的本地代码?
  3. 是否有任何在正常编程中应该使用 @native 注解的用例?
public final class Integer extends Number implements Comparable<Integer> {
    /**
     * A constant holding the minimum value an {@code int} can
     * have, -2^31.
     */
    @Native public static final int   MIN_VALUE = 0x80000000;

    /**
     * A constant holding the maximum value an {@code int} can
     * have, 2^31-1.
     */
    @Native public static final int   MAX_VALUE = 0x7fffffff;
}

@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&lt;Integer&gt; {
         /**
          * A constant holding the minimum value an {@code int} can
          * have, -2&lt;sup&gt;31&lt;/sup&gt;.
          */
         @Native public static final int   MIN_VALUE = 0x80000000;
    
         /**
          * A constant holding the maximum value an {@code int} can
          * have, 2&lt;sup&gt;31&lt;/sup&gt;-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

得分: 2

  1. 这主要用于自动生成本地代码。因此,本地代码可以轻松地使用这些常量。

  2. 反之亦然:本地代码可以使用这样的常量。

  3. 如果您指的是normal==Java,那么答案是否定的。

英文:
  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

得分: 2

  1. 使用@native注解的目的是什么?

请参见上文。

  1. 我们可以在哪里找到由@native使用的本机代码?

生成的头文件以及对上述符号(MIN_VALUEMAX_VALUE)的任何本机代码引用将位于OpenJDK Java源代码树中。(我通常使用findxargsgrep的组合来查找源代码树中的符号引用,但可能还有更好的方法。)

  1. 是否存在应该在正常编程中使用@native注解的用例?

一个用例是,如果您正在实现涉及本机代码和生成的本机头文件的自己的API。

但是,如果您指的是(纯)Java代码中的用例,我想不出任何情况1

1 - 除了已经注意到的用例(例如JNI等本机头文件生成器)和愚蠢的用例(例如列出所有@Native注解的工具)之外。

英文:

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.

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

> 3. 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<sup>1</sup>.

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

huangapple
  • 本文由 发表于 2020年10月10日 14:59:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/64290866.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定