Java 17 Recipes


--add-modules jdk.incubator.vector


Download 3.2 Mb.
Pdf ko'rish
bet41/245
Sana02.06.2024
Hajmi3.2 Mb.
#1839910
1   ...   37   38   39   40   41   42   43   44   ...   245
Bog'liq
Java 17 Recipes

--add-modules jdk.incubator.vector
So, you must write the following.
javac --add-modules jdk.incubator.vector VectorExample.java
After compiling the class, you can run it with the following command.
java --add-modules jdk.incubator.vector VectorExample
Chapter 2 enhanCements from Java 9 through Java 17 


66
This enhancement was proposed in Java 16 (JEP 338) to increase the number of 
computations performed by vector instructions on supported CPU architectures. In the 
preceding example, the aim is to add two vectors, each containing two integer values: 
in this case, vector hardware performs two additions in a single CPU cycle while an 
ordinary operation is allowed.
For more information on vectors, see the documentation at 
 https://openjdk.java 
.net/jeps/414
.
2-11. Avoiding Redundancy in Interface Code
 Problem
You want to implement two or more default methods within an interface with very 
similar code. Rather than copying code into each of the different default methods and 
maintaining each default method separately, you’d like to encapsulate the similar code 
into its own method for reuse.
 Solution
Use a private method in an interface to alleviate this issue. Java 9 provides the ability 
to include private methods within an interface. A private method is only available 
within that interface, and it cannot be used by any class that implements the interface. 
However, each default method implementation that is part of the interface can use the 
private method.
The following interface includes two default methods and one private method. 
The private method encapsulates functionality that can be used in each of the default 
method implementations.
public interface Pool {
/**
* Calculate volume (gal) for a fixed depth square or rectangular pool.
*/
public default double squareOrRectConstantDepth(double length, double 
width, double depth){
return volumeCalc(length, width, depth);
}
Chapter 2 enhanCements from Java 9 through Java 17 


67
/**
* Calculate volume (gal) for a variable depth square or rectangular pool.
*/
public default double squareOrRectVariableDepth(double length,
double width, double shallowDepth,
double middleDepth, double deepDepth){
double avgDepth = (shallowDepth + middleDepth + deepDepth) / 3;
return volumeCalc(length, width, avgDepth);
}
/**
* Standard square or rectangular volume calculation.
*/
private double volumeCalc(double length, double width, double depth){
return length * width * depth * 7.5;
}
}
For testing the provious code, create the main class.
public class PoolExample implements Pool {
public static void main(String args[]) {
PoolExample pe = new PoolExample();
System.out.println(pe.squareOrRectConstantDepth( 
10,10,10));
}
}
The following is the output.
7500.0

Download 3.2 Mb.

Do'stlaringiz bilan baham:
1   ...   37   38   39   40   41   42   43   44   ...   245




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling