Vector API in Java 25 (Tenth Incubator)
The Vector API lets Java code express calculations on several values at once, which the JIT compiles to SIMD instructions on supported CPUs. It lives in the incubator module jdk.incubator.vector and is in its tenth incubator release in Java 25 (JEP 508).
This tutorial explains the core concepts, shows two loops written with vectors, and lists the flags needed to compile and run incubator code.
1. Why a Vector API?
Modern CPUs can add or multiply 4, 8 or 16 numbers in a single instruction. The JIT sometimes vectorises simple loops automatically, but it cannot always prove that it is safe or worthwhile. The Vector API lets you state the vector computation explicitly and portably, so performance-critical code such as image processing, machine learning or financial maths can rely on SIMD.
The API has stayed in incubation because it is waiting for value classes from Project Valhalla before it can be finalised.
2. Key Concepts and Flags
| Concept | Meaning |
|---|---|
VectorSpecies | element type plus vector size, e.g. FloatVector.SPECIES_PREFERRED |
fromArray / intoArray | load lanes from and store lanes to an array |
add, mul, fma | lane-wise arithmetic |
reduceLanes(ADD) | combine all lanes into one value |
loopBound(length) | largest index that a full vector can process |
javac --add-modules jdk.incubator.vector VectorAdd.java
java --add-modules jdk.incubator.vector VectorAdd
3. Practical Examples
Example 1: Lane-wise multiplication
import jdk.incubator.vector.FloatVector;
import jdk.incubator.vector.VectorSpecies;
// Incubator in Java 25 (JEP 508):
// javac --add-modules jdk.incubator.vector VectorAdd.java
// java --add-modules jdk.incubator.vector VectorAdd
public class VectorAdd {
static final VectorSpecies<Float> SPECIES = FloatVector.SPECIES_PREFERRED;
// total[i] = price[i] * qty[i], several lanes per CPU instruction
static void multiply(float[] price, float[] qty, float[] total) {
int i = 0;
int bound = SPECIES.loopBound(price.length);
for (; i < bound; i += SPECIES.length()) {
FloatVector p = FloatVector.fromArray(SPECIES, price, i);
FloatVector q = FloatVector.fromArray(SPECIES, qty, i);
p.mul(q).intoArray(total, i);
}
for (; i < price.length; i++) { // scalar tail for leftover elements
total[i] = price[i] * qty[i];
}
}
public static void main(String[] args) {
float[] price = { 20, 60, 899, 145, 35, 12, 250, 99, 15, 400 };
float[] qty = { 3, 2, 1, 4, 10, 6, 1, 2, 5, 1 };
float[] total = new float[price.length];
multiply(price, qty, total);
System.out.println("Lanes on this CPU: " + (SPECIES.length() > 1));
System.out.println(java.util.Arrays.toString(total));
}
}
WARNING: Using incubator modules: jdk.incubator.vector Lanes on this CPU: true [60.0, 120.0, 899.0, 580.0, 350.0, 72.0, 250.0, 198.0, 75.0, 400.0]
Compile and run: javac --add-modules jdk.incubator.vector VectorAdd.java && java --add-modules jdk.incubator.vector VectorAdd
The main loop handles full vectors; the second loop processes the remaining elements one by one. The JVM prints a warning whenever an incubator module is used.
Example 2: A reduction with fused multiply-add
import jdk.incubator.vector.FloatVector;
import jdk.incubator.vector.VectorOperators;
import jdk.incubator.vector.VectorSpecies;
// Incubator in Java 25 (JEP 508): add --add-modules jdk.incubator.vector
public class VectorReduce {
static final VectorSpecies<Float> SPECIES = FloatVector.SPECIES_PREFERRED;
// Sum of squares, a common building block in statistics and ML
static float sumOfSquares(float[] values) {
FloatVector acc = FloatVector.zero(SPECIES);
int i = 0;
for (; i < SPECIES.loopBound(values.length); i += SPECIES.length()) {
FloatVector v = FloatVector.fromArray(SPECIES, values, i);
acc = v.fma(v, acc); // acc + v * v
}
float sum = acc.reduceLanes(VectorOperators.ADD);
for (; i < values.length; i++) {
sum += values[i] * values[i];
}
return sum;
}
public static void main(String[] args) {
float[] data = new float[1000];
for (int i = 0; i < data.length; i++) {
data[i] = i % 10; // 0..9 repeated
}
System.out.println("Sum of squares: " + sumOfSquares(data)); // 100 * (0+1+4+...+81) = 28500
}
}
WARNING: Using incubator modules: jdk.incubator.vector Sum of squares: 28500.0
Compile and run: java --add-modules jdk.incubator.vector VectorReduce
4. How a Vector Loop Works
5. Important Points and Best Practices
- Incubator APIs can change in every release; expect to update code when upgrading.
- Use
SPECIES_PREFERREDso the code adapts to the CPU it runs on. - Always handle the tail with
loopBoundor masks. - Benchmark with JMH before and after; vectorisation helps only for large, regular data.
6. Summary
- The Vector API is in its tenth incubator release in Java 25 (JEP 508).
- It expresses SIMD computations with species, vectors and lane-wise operations.
- Compile and run with
--add-modules jdk.incubator.vector. - It will be finalised after Project Valhalla delivers value classes.
Related Tutorials
7. References
Frequently Asked Questions (FAQ)
Q1. What is the Java Vector API?
An incubator API in jdk.incubator.vector for expressing SIMD computations that the JIT compiles to vector instructions.
Q2. How do I compile code that uses the Vector API?
Add --add-modules jdk.incubator.vector to both javac and java.
Q3. Why is the Vector API still incubating?
It is waiting for value classes from Project Valhalla, which will shape its final design.
Q4. Will Vector API code work on every CPU?
Yes. On CPUs without suitable SIMD support, the operations still work, but more slowly, through a fallback implementation.