-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Manoel Campos <[email protected]>
- Loading branch information
1 parent
6362dbb
commit 4461f75
Showing
4 changed files
with
80 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package samples.jdk22; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
|
||
class SampleClass{ | ||
private final String jdkVersion; | ||
|
||
SampleClass(final String jdkVersion) { | ||
this.jdkVersion = Objects.requireNonNull(jdkVersion); | ||
} | ||
|
||
public String getJdkVersion() { | ||
return jdkVersion; | ||
} | ||
} | ||
|
||
public class Jdk22 extends SampleClass { | ||
/** | ||
* Instance main method. A simplified version of the main method that can be used in a class, | ||
* which doesn't require to be static and don't need a String array if you aren't going | ||
* to take command line parameters. | ||
* @see <a href="https://openjdk.org/jeps/463"></a> | ||
*/ | ||
void main() { | ||
new Jdk22(); | ||
} | ||
|
||
/** | ||
* A constructor showing how to use statements before calling the super constructor. | ||
* @see <a href="https://openjdk.org/jeps/447">https://openjdk.org/jeps/447</a> | ||
*/ | ||
public Jdk22(){ | ||
// Calling any statements before a super or this construtor was not allowed before. | ||
unamedVar(); | ||
stringTemplate(); | ||
super("JDK 22"); | ||
} | ||
|
||
/** | ||
* Shows how to use string template with the new {@link StringTemplate#STR} processor, | ||
* so that you can interpolate variables directly into a string, | ||
* following the pattern: {@snippet : STR."My message here: \{variableX}"} | ||
* | ||
* You don't need to import the {@link StringTemplate} class to use it. | ||
* The processor is automatically available in the classpath as if it was directly inside java.lang package. | ||
* | ||
* @see <a href="https://openjdk.org/jeps/459">https://openjdk.org/jeps/459</a> | ||
*/ | ||
private static void stringTemplate(){ | ||
final String name = "Manoel"; | ||
final int age = 44; | ||
final String role = "Software Engineer"; | ||
final String msg = STR."Hello, my name is \{name}, I'm \{age} years old and I'm a \{role}."; | ||
System.out.println(msg); | ||
} | ||
|
||
/** | ||
* Shows how to use underscore to represent an unused variable/parameter, | ||
* so that it is ignored and can't be used. | ||
* @see <a href="https://openjdk.java.net/jeps/456">https://openjdk.java.net/jeps/456</a> | ||
*/ | ||
private static void unamedVar(){ | ||
//Heights initially in centimeters | ||
final var personHeightMap = new HashMap<>( | ||
Map.of("John", 180.0, "Mary", 160.0, "Peter", 175.0, "Lucy", 165.0, "Paul", 190.0, "Alice", 155.0, | ||
"Mark", 170.0, "Sandra", 175.0, "David", 185.0, "Eva", 160.0)); | ||
|
||
// Converts the heights to meters | ||
// The name key is not being used in the lambda expression, so it can be replaced by an underscore | ||
personHeightMap.replaceAll((_, height) -> height/100.0); | ||
personHeightMap.forEach((name, height) -> System.out.printf("Name: %s | Height %.2fm\n", name, height)); | ||
System.out.println(); | ||
} | ||
} |