Java Reference ยท Keywords and Statements
Java native and strictfp modifiers
Reference for the rarely used Java modifiers native and strictfp: what JNI methods are, why strictfp is redundant since Java 17, and when to ignore both.
Two modifiers in Java are rarely written by application developers. native marks a method whose body is not Java but is implemented in another language, usually C or C++, and loaded from a shared library through the Java Native Interface (JNI). strictfp once forced floating-point calculations to follow strict IEEE 754 rules; since Java 17 all floating-point arithmetic is strict, and the modifier has no effect.
You will meet native mostly when reading JDK source (Object.hashCode(), System.arraycopy(), Thread.start0()), and strictfp mostly in old code.
public class Fast { static { System.loadLibrary("fast"); } // loads libfast.so / fast.dll public static native int checksum(byte[] data); // no body, ends with ; } public strictfp class Old { // legal, redundant since Java 17 strictfp double half(double x) { return x / 2; } }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Syntax
[access] [static] native ReturnType name(Type param, ...) [throws ...]; strictfp class Name { ... } strictfp ReturnType method(...) { ... }
| Modifier | Applies to | Rules |
|---|---|---|
native |
methods | No body (a semicolon instead). Cannot be combined with abstract. May be static, final, synchronized, or have any access level. |
strictfp |
classes, interfaces, methods | Cannot be used on constructors, fields, or abstract methods. Redundant since Java 17. |
Usage notes
- How
nativeworks: the compiler only records the signature. At run time the JVM looks up a function whose name is built from the class and method names (for exampleJava_Fast_checksum), or one registered explicitly byRegisterNatives.System.loadLibrary(orSystem.loadwith an absolute path) must run first, otherwise calling the method throwsUnsatisfiedLinkError. - When to avoid it: native code can crash the whole JVM, bypasses memory safety, is platform specific, and is hard to ship. In new projects, prefer the Foreign Function and Memory API (
java.lang.foreign, final in Java 22) to call C libraries from pure Java without writing JNI glue. - Cost: a JNI call is slower than a normal method call, and the JIT cannot inline across it, though some JDK methods are intrinsified by the JIT and avoid the overhead.
nativeis not part of the method contract. A subclass can override a native method with an ordinary Java one and vice versa; callers cannot tell.strictfphistory: before Java 17 a method could use extended exponent ranges on some processors (the old x87 FPU), so the same code could give slightly different results on different machines unless markedstrictfp. Java 17 (JEP 306) made strict semantics the only behavior. The modifier still compiles, but javac prints a[strictfp]warning saying it is not required (you can see it in the second example's error output). Existing class files that carry it keep working.- No need to add it to new code. Removing it from old code is safe on Java 17 and later, but remember a library that still targets older runtimes.
MathversusStrictMath:strictfpwas never about these classes.StrictMathmust give bit-identical results using the fdlibm algorithms;Mathmethods may use faster platform intrinsics within a small error bound. UseStrictMathwhen results must match across platforms.Modifier.isNativeon aMethodtells you reliably whether a method is native, as the first example shows.
Related
final,static,abstract, Reserved words- Lessons: Primitive types, Math, How Java works