简体   繁体   English

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

[英]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.我正在浏览 Integer class 代码并注意到 MIN_VALUE 和 MAX_VALUE 带有 @native 注释。 My question is我的问题是

  1. What is the purpose of using @native annotation?使用@native 注解的目的是什么?

  2. Where can we find native code which is used by @native?我们在哪里可以找到@native 使用的本地代码?

  3. Is there any use case where we should use @native annotation in normal programming?有没有我们应该在正常编程中使用@native 注释的用例?

     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 @native 文档说

/**
 * 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.如果你的意思是 normal==Java,那么答案是否定的。

The Java 8 javadoc includes these sentences: Java 8 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." “表示可以从本机代码引用定义常量值的字段。注释可以用作生成本机 header 文件的工具的提示,以确定是否需要 header 文件,如果需要,它应该包含哪些声明。 “

So:所以:

  1. What is the purpose of using @native annotation?使用@native 注解的目的是什么?

See above.看上面。

  1. Where can we find native code which is used by @native?我们在哪里可以找到@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.生成的 header 文件以及对上述符号( MIN_VALUEMAX_VALUE )的任何本机代码引用都将位于 OpenJDK Java 源代码树中。 (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.) (我通常使用findxargsgrep的组合来追踪对源树中符号的引用,但可能有更好的方法。)

  1. Is there any use case where we should use @native annotation in normal programming?有没有我们应该在正常编程中使用@native 注释的用例?

One case would be if you are implementing your own APIs which involve native code and generated native header files.一种情况是,如果您要实现自己的 API,其中涉及本机代码和生成的本机 header 文件。

But if you mean use cases in (pure) Java code, I can't think of any 1 .但是,如果您指的是(纯)Java 代码中的用例,我想不出任何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). 1 - 除了已经提到的那些(即 JNI 等原生 header 生成器)和愚蠢的那些(例如用于列出所有@Native注释的工具)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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