|
|
Line 82: |
Line 82: |
| <br> | | <br> |
| [[File:switch-statement-arrow.png]] | | [[File:switch-statement-arrow.png]] |
− | | Code compiles (with a preview warning) and while running prints only "Hello" (because a break is implicit after every case with an arrow. | + | | Code compiles and while running prints only "Hello" (because a break is implicit after every case with an arrow. |
| |- | | |- |
| ! scope="row" | Positive Compilation (Switch Expression) | | ! scope="row" | Positive Compilation (Switch Expression) |
Line 93: |
Line 93: |
| }; | | }; |
| | | |
− | @SuppressWarnings("preview")
| |
| public String getDay_1 (Day today) { | | public String getDay_1 (Day today) { |
| String day = switch(today) { | | String day = switch(today) { |
Line 129: |
Line 128: |
| <br> | | <br> |
| <br> | | <br> |
− | [[File:enable-preview.error.png]] | + | [[File:SWITCHEXPRE.error.png]] |
| | Compiler error is shown for preview feature. | | | Compiler error is shown for preview feature. |
| Quick fix is available to enable the preview feature in the preferences. | | Quick fix is available to enable the preview feature in the preferences. |
| |- | | |- |
− | ! scope="row" | Compiler Warning Case
| |
− | |
| |
− | <source lang="java">
| |
− | public class Test {
| |
− |
| |
− | enum Day {
| |
− | MON, TUE, WED, THUR, FRI, SAT, SUN
| |
− | };
| |
− |
| |
− | public String getDay_1 (Day today) {
| |
− | String day = switch(today) {
| |
− | case MON, TUE, WED, THUR, FRI : yield "Weekday";
| |
− | case SAT, SUN : yield "Weekend";
| |
− | };
| |
− | return day;
| |
− | }
| |
− | }
| |
− | </source>
| |
− | <br>
| |
− | <br>
| |
− | [[File:switch.preview.warning.compile.yield.png]]
| |
− | | Compiler warning is shown for preview feature
| |
− | |-
| |
− | ! scope="row" | Compiler Error Case
| |
− | |
| |
− | <source lang="java">
| |
− | public class Test {
| |
− |
| |
− | enum Day {
| |
− | MON, TUE, WED, THUR, FRI, SAT, SUN
| |
− | };
| |
− |
| |
− | @SuppressWarnings("preview")
| |
− | public String getDay_1 (Day today) {
| |
− | String day = switch(today) {
| |
− | case MON : yield "Weekday";
| |
− | case SAT, SUN : yield "Weekend";
| |
− | };
| |
− | return day;
| |
− | }
| |
− | }
| |
− | </source>
| |
− | [[File:switch.cover-all-cases.error.yield.png]]
| |
− | | Compiler error is shown
| |
| |- | | |- |
| ! colspan="3" | Preview Feature: Text Blocks | | ! colspan="3" | Preview Feature: Text Blocks |
THIS PAGE IS UNDER CONSTRUCTION -
This is an informal page listing examples of features that are implemented by the Java 14 Support. You are welcome to try out these examples. If you find bugs, please file a bug after checking for a duplicate entry here
|
Feature / Steps
|
Expected Result
|
The Pre-requisite: Java 14 JRE Support
|
Add Java 14 JRE
|
Use Window -> Preferences-> Java -> Installed JREs -> Add...
[note: Eclipse -> Preferences in Mac / Window -> Preferences in Windows]
|
Java 14 JRE recognized as a valid JRE
|
Project JRE
|
In Package Explorer Use project's context menu and add Java 14 JRE |
JRE specific (eg Object) gets resolved in the project.
|
Package Explorer
|
Go to Package Explorer and expand the Java 14 JRE |
Modules (eg java.base etc) are listed in the package explorer view
|
The First Step: Java 14 Compliance
|
Set Project Compliance in Package Explorer
|
Context Menu of Project -> Properties -> Set project-specific, drop down to 14
|
14 is shown in the drop down list.
A checkbox to enable preview features is available on the preference page.
|
Preview Feature: Switch Expressions, Enhanced Switch Statement and Multi-Label Case Statements.
|
Positive Compilation 1 (Switch Statement with multi-label case with colon)
|
Use the following code:
public class X {
public void foo(int i) {
switch (i) {
case 0, 1, 2: System.out.println("Hello");
default : System.out.println("World");
}
}
public static void main(String[] argv) {
new X().foo(2);
}
}
|
Code compiles (with a preview warning) and while running prints both "Hello" "World"
|
Positive Compilation 2 (Switch Statement with case with arrow)
|
Use the following code:
public class X {
@SuppressWarnings("preview")
public void foo(int i) {
switch (i) {
case 2 -> System.out.println("Hello");
default -> System.out.println("World");
}
}
public static void main(String[] argv) {
new X().foo(2);
}
}
|
Code compiles and while running prints only "Hello" (because a break is implicit after every case with an arrow.
|
Positive Compilation (Switch Expression)
|
Use the following code:
public class Test {
enum Day {
MON, TUE, WED, THUR, FRI, SAT, SUN
};
public String getDay_1 (Day today) {
String day = switch(today) {
case MON, TUE, WED, THUR, FRI -> "Weekday";
case SAT, SUN -> "Weekend";
};
return day;
}
}
|
Code compiles
|
Compiler error Case
|
public class Test {
enum Day {
MON, TUE, WED, THUR, FRI, SAT, SUN
};
public String getDay_1 (Day today) {
String day = switch(today) {
case MON, TUE, WED, THUR, FRI-> "Weekday";
case SAT, SUN->"Weekend";
};
return day;
}
}
File:SWITCHEXPRE.error.png
|
Compiler error is shown for preview feature.
Quick fix is available to enable the preview feature in the preferences.
|
Preview Feature: Text Blocks
|
Text Block Example
|
Compile and run the following code:
@SuppressWarnings("preview")
public class Test {
public static void main(String[] args) {
String tb = """
Hello
World
""";
System.out.println(tb);
}
}
|
Code compiles prints both "Hello" "World" as it is - notice that "World" is printed in the next line.
|
Text Block Compilation Error Example
|
Use the following code:
public class Test {
public static void main(String[] args) {
String tb = """
Hello
World
"";
}
}
|
Compilation error - text block not closed properly
|