BAEL-5069 code for article on new features in the Java 16 incremental… (#11176)

* BAEL-5069 code for article on new features in the Java 16 incremental release

* BAEL-5069 added some details around Vector API example and enable-preview in maven to allow package to build

Co-authored-by: Liam Garvie <liamgarvie@Liams-MacBook-Pro.local>
This commit is contained in:
LiamGve
2021-09-22 08:33:17 +01:00
committed by GitHub
parent 06f357f143
commit 3860b75fda
16 changed files with 261 additions and 1 deletions

View File

@@ -0,0 +1,7 @@
package com.baeldung.features;
interface HelloWorld {
default String hello() {
return "world";
}
}

View File

@@ -0,0 +1,9 @@
package com.baeldung.features;
import com.baeldung.features.record.Book;
public class OuterClass {
class InnerClass {
Book book = new Book("Title", "author", "isbn");
}
}

View File

@@ -0,0 +1,23 @@
package com.baeldung.features;
import jdk.incubator.vector.IntVector;
public class VectorExample {
public int[] scalarComputation(int[] a, int[] b) {
var c = new int[a.length];
for (int i = 0; i < a.length; i++) {
c[i] = a[i] * b[i];
}
return c;
}
public int[] vectorComputation(int[] a, int[] b) {
var c = new int[a.length];
var vectorA = IntVector.fromArray(IntVector.SPECIES_128, a, 0);
var vectorB = IntVector.fromArray(IntVector.SPECIES_128, b, 0);
var vectorC = vectorA.mul(vectorB);
vectorC.intoArray(c, 0);
return c;
}
}

View File

@@ -0,0 +1,44 @@
package com.baeldung.features.model;
import java.util.Objects;
public final class Book {
private final String title;
private final String author;
private final String isbn;
public Book(String title, String author, String isbn) {
this.title = title;
this.author = author;
this.isbn = isbn;
}
public String getTitle() {
return title;
}
public String getAuthor() {
return author;
}
public String getIsbn() {
return isbn;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Book book = (Book) o;
return Objects.equals(title, book.title) && Objects.equals(author, book.author) && Objects.equals(isbn, book.isbn);
}
@Override
public int hashCode() {
return Objects.hash(title, author, isbn);
}
}

View File

@@ -0,0 +1,4 @@
package com.baeldung.features.record;
public record Book(String title, String author, String isbn) {
}

View File

@@ -0,0 +1,4 @@
package com.baeldung.features.sealed;
public sealed interface JungleAnimal permits Monkey, Snake {
}

View File

@@ -0,0 +1,4 @@
package com.baeldung.features.sealed;
public final class Monkey implements JungleAnimal {
}

View File

@@ -0,0 +1,13 @@
package com.baeldung.features.sealed;
public class Sealed {
public static void main(String... args) {
JungleAnimal j = new Monkey();
if (j instanceof Monkey m) {
// do logic
} else if (j instanceof Snake s) {
// do logic
}
}
}

View File

@@ -0,0 +1,4 @@
package com.baeldung.features.sealed;
public non-sealed class Snake implements JungleAnimal {
}

View File

@@ -0,0 +1,4 @@
module core.java {
requires jdk.incubator.vector;
requires org.apache.commons.lang3;
}