JAVA 9
Modular System
JAVA 17
license
몇년 전부터 유료화가 되었었던 자바가 다시 무료화되었다.
Oracle No-Fee Terms and Conditions (NFTC) free-java-license
- docker도 얼른 다시 무료화되었으면!
Whats new?
JEP
1. strictfp 복원
- strictfp modifier는 java 1.2에 등장했으나 사라진 키워드
- class, method, interface 에 사용가능하며, variable에 불가하다. ```java // Java program to illustrate strictfp modifier // Usage in Classes
// Main class class GFG {
// Method 1
// Calculating sum using strictfp modifier
public strictfp double sum()
{
double num1 = 10e+10;
double num2 = 6e+08;
// Returning the sum
return (num1 + num2);
}
// Method 2
// Main driver method
public static void main(String[] args)
{
// Creating object of class in main() method
GFG t = new GFG();
// Here we have error of putting strictfp and
// error is not found public static void main method
System.out.println(t.sum());
// = 1.006E11
} }
[from](https://www.geeksforgeeks.org/strictfp-keyword-java/)
### 2. psuedo-random number generator (PRNG)
- 랜덤을 생성하는 인터페이스가 추가되었다.
- 기존 java.util.Random 등은 deprecated
- stream 호환성이 좋아졌다.
```java
public IntStream getPseudoInts(String algorithm, int streamSize) {
// returns an IntStream with size @streamSize of random numbers generated using the @algorithm
// where the lower bound is 0 and the upper is 100 (exclusive)
return RandomGeneratorFactory.of(algorithm)
.create()
.ints(streamSize, 0,100);
}
3. NEW Mac OS Rendering pipeline
4. MacOS/AArch64 port
- m1같은 arm64 실리콘칩 에서 jdk가 돌아가도록 한다.
그럼 기존에는? rosseta 에서 돌아가서, 성능문제가 있었음.
5. Applet API deprecated, 삭제예정
6. JDK 내부를 강하게 캡슐화하였다.
- 사용자가 internal API에 접근하기 어려워졌음.
7. switch 키워드의 pattern matching
```java // before public String checkObject(Object obj){ if (obj == null) return “It is null”; switch(obj) { case (obj instanceof Human) { Human h = (Human)obj; return String.format(“Name: %s, age: %s and profession: %s”,h.name(), h.age(), h.profession()) } }
} // after public String checkObject(Object obj) { return switch (obj) { // type matching case Human h -> “Name: %s, age: %s and profession: %s”.formatted(h.name(), h.age(), h.profession()); // pattern check case Circle c && (c.radius() > 100) -> “This is big circle”; case Shape s -> “It is just a shape”; // null check case null -> “It is null”; default -> “It is an object”; }; }
- instance 확인이 바로 가능해졌고,
- nullable해져서 null체크도 가능하다.
- 바로 람다식 사용도 가능하다.
- 체크문에서 꺼내서 쓸 수도 있다.
- 자세한건 여기에 [JEP 406](https://openjdk.java.net/jeps/406)
### 8.RMI activation 삭제
### 9.Sealed class
JDK 15부터 나온건데 좀더 알아보자.
위에서 발전된 switch문으로 응용될수도 있음.
```java
int getNumberOfSides(Shape shape) {
return switch (shape) {
case WeirdTriangle t -> t.getNumberOfSides();
case Circle c -> c.getNumberOfSides();
case Triangle t -> t.getNumberOfSides();
case Rectangle r -> r.getNumberOfSides();
case Square s -> s.getNumberOfSides();
};
}
10.AOP(Ahead-of-time) , JIT(Just-In-Time) Compiler 삭제
jdk 9부터 소개된 기능이나, 유지비용이커 deprecated
11. SecurityManager Deprecated
12. Foreign Function & Memory API
Foreign Function : 힙메모리 밖에있는 다른 JDK 내의 function을 사용가능하다. ex) C라이브러리 코드 사용가능.
private static final SymbolLookup libLookup;
static {
// loads a particular C library
var path = JEP412.class.getResource("/print_name.so").getPath();
System.load(path);
libLookup = SymbolLookup.loaderLookup();
}
public String getPrintNameFormat(String name) {
var printMethod = libLookup.lookup("printName");
if (printMethod.isPresent()) {
var methodReference = CLinker.getInstance()
.downcallHandle(
printMethod.get(),
MethodType.methodType(MemoryAddress.class, MemoryAddress.class),
FunctionDescriptor.of(CLinker.C_POINTER, CLinker.C_POINTER)
);
try {
var nativeString = CLinker.toCString(name, newImplicitScope());
var invokeReturn = methodReference.invoke(nativeString.address());
var memoryAddress = (MemoryAddress) invokeReturn;
return CLinker.toJavaString(memoryAddress);
} catch (Throwable throwable) {
throw new RuntimeException(throwable);
}
}
throw new RuntimeException("printName function not found.");
}
13. Vector API
이미지,산술 등에 사용됨.
public void newVectorComputation(float[] a, float[] b, float[] c) {
for (var i = 0; i < a.length; i += SPECIES.length()) {
var m = SPECIES.indexInRange(i, a.length);
var va = FloatVector.fromArray(SPECIES, a, i, m);
var vb = FloatVector.fromArray(SPECIES, b, i, m);
var vc = va.mul(vb);
vc.intoArray(c, i, m);
}
}
public void commonVectorComputation(float[] a, float[] b, float[] c) {
for (var i = 0; i < a.length; i ++) {
c[i] = a[i] * b[i];
}
}
출처