Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.
Difference between revisions of "Java13/Examples"
(11 intermediate revisions by one other user not shown) | |||
Line 1: | Line 1: | ||
− | |||
− | |||
This is an informal page listing examples of features that are implemented by the Java 13 Support. You are welcome to try out these examples. If you find bugs, please file a bug after checking for a duplicate entry [http://eclip.se/fP here] | This is an informal page listing examples of features that are implemented by the Java 13 Support. You are welcome to try out these examples. If you find bugs, please file a bug after checking for a duplicate entry [http://eclip.se/fP here] | ||
<b>NOTE:</b> | <b>NOTE:</b> | ||
* Switch expression, Enhanced switch statement and Multi-constant case labels are <b>Preview features</b> in Java 13. They are not enabled by default and can by enabled using <b>--enable-preview</b>. | * Switch expression, Enhanced switch statement and Multi-constant case labels are <b>Preview features</b> in Java 13. They are not enabled by default and can by enabled using <b>--enable-preview</b>. | ||
+ | * TextBlock is also another preview feature in Java 13. | ||
* In Eclipse, <b>--enable-preview</b> can be enabled from the Preferences. It is implicitly added while launching a java program if the feature has been enabled for the project/workspace. | * In Eclipse, <b>--enable-preview</b> can be enabled from the Preferences. It is implicitly added while launching a java program if the feature has been enabled for the project/workspace. | ||
Line 13: | Line 12: | ||
! style="width: 25%;" | Expected Result | ! style="width: 25%;" | Expected Result | ||
|- | |- | ||
− | ! colspan="3" | The Pre-requisite: Java | + | ! colspan="3" | The Pre-requisite: Java 13 JRE Support |
|- | |- | ||
− | ! scope="row" | Add Java | + | ! scope="row" | Add Java 13 JRE |
| Use Window -> Preferences-> Java -> Installed JREs -> Add... <br> | | Use Window -> Preferences-> Java -> Installed JREs -> Add... <br> | ||
<br> | <br> | ||
− | [[File: | + | [[File:FileAddJ13.png]] |
<br> | <br> | ||
[note: Eclipse -> Preferences in Mac / Window -> Preferences in Windows] | [note: Eclipse -> Preferences in Mac / Window -> Preferences in Windows] | ||
− | | Java | + | | Java 13 JRE recognized as a valid JRE |
|- | |- | ||
! scope="row" | Project JRE | ! scope="row" | Project JRE | ||
− | | In Package Explorer Use project's context menu and add Java | + | | In Package Explorer Use project's context menu and add Java 13 JRE || JRE specific (eg Object) gets resolved in the project. |
|- | |- | ||
! scope="row" | Package Explorer | ! scope="row" | Package Explorer | ||
− | | Go to Package Explorer and expand the Java | + | | Go to Package Explorer and expand the Java 13 JRE || Modules (eg java.base etc) are listed in the package explorer view |
|- | |- | ||
− | ! colspan="3" | The First Step: Java | + | ! colspan="3" | The First Step: Java 13 Compliance |
|- | |- | ||
! scope="row" | Set Project Compliance in Package Explorer | ! scope="row" | Set Project Compliance in Package Explorer | ||
− | | Context Menu of Project -> Properties -> Set project-specific, drop down to | + | | Context Menu of Project -> Properties -> Set project-specific, drop down to 13 |
<br> | <br> | ||
<br> | <br> | ||
− | [[File: | + | [[File:j13.compliance.png]] |
− | | | + | |13 is shown in the drop down list. |
A checkbox to enable preview features is available on the preference page. | A checkbox to enable preview features is available on the preference page. | ||
|- | |- | ||
− | ! colspan="3" | | + | ! colspan="3" | Preview Feature: Switch Expressions, Enhanced Switch Statement and Multi-Label Case Statements. |
|- | |- | ||
! scope="row" | Positive Compilation 1 (Switch Statement with multi-label case with colon) | ! scope="row" | Positive Compilation 1 (Switch Statement with multi-label case with colon) | ||
Line 118: | Line 117: | ||
public String getDay_1 (Day today) { | public String getDay_1 (Day today) { | ||
String day = switch(today) { | String day = switch(today) { | ||
− | case MON, TUE, WED, THUR, FRI | + | case MON, TUE, WED, THUR, FRI-> "Weekday"; |
− | case SAT, SUN | + | case SAT, SUN->"Weekend"; |
}; | }; | ||
return day; | return day; | ||
Line 135: | Line 134: | ||
<source lang="java"> | <source lang="java"> | ||
public class Test { | public class Test { | ||
− | + | ||
enum Day { | enum Day { | ||
MON, TUE, WED, THUR, FRI, SAT, SUN | MON, TUE, WED, THUR, FRI, SAT, SUN | ||
}; | }; | ||
− | + | ||
public String getDay_1 (Day today) { | public String getDay_1 (Day today) { | ||
String day = switch(today) { | String day = switch(today) { | ||
− | case MON, TUE, WED, THUR, FRI: | + | case MON, TUE, WED, THUR, FRI : yield "Weekday"; |
− | case SAT, SUN: | + | case SAT, SUN : yield "Weekend"; |
}; | }; | ||
return day; | return day; | ||
Line 151: | Line 150: | ||
<br> | <br> | ||
<br> | <br> | ||
− | [[File:switch.preview.warning.compile.png]] | + | [[File:switch.preview.warning.compile.yield.png]] |
| Compiler warning is shown for preview feature | | Compiler warning is shown for preview feature | ||
|- | |- | ||
Line 166: | Line 165: | ||
public String getDay_1 (Day today) { | public String getDay_1 (Day today) { | ||
String day = switch(today) { | String day = switch(today) { | ||
− | case MON : | + | case MON : yield "Weekday"; |
− | case SAT, SUN : | + | case SAT, SUN : yield "Weekend"; |
}; | }; | ||
return day; | return day; | ||
Line 173: | Line 172: | ||
} | } | ||
</source> | </source> | ||
− | [[File:switch.cover-all-cases.error.png]] | + | [[File:switch.cover-all-cases.error.yield.png]] |
| Compiler error is shown | | Compiler error is shown | ||
+ | |- | ||
+ | ! colspan="3" | Preview Feature: Text Blocks | ||
+ | |- | ||
+ | ! scope="row" | Text Block Example | ||
+ | | Compile and run the following code: | ||
+ | <source lang="java"> | ||
+ | @SuppressWarnings("preview") | ||
+ | public class Test { | ||
+ | public static void main(String[] args) { | ||
+ | String tb = """ | ||
+ | Hello | ||
+ | World | ||
+ | """; | ||
+ | System.out.println(tb); | ||
+ | } | ||
+ | } | ||
+ | </source> | ||
+ | |||
+ | <br> | ||
+ | <br> | ||
+ | [[File:textblock.png]] | ||
+ | | Code compiles prints both "Hello" "World" as it is - notice that "World" is printed in the next line. | ||
+ | |- | ||
+ | ! scope="row" | Text Block Compilation Error Example | ||
+ | | Use the following code: | ||
+ | <source lang="java"> | ||
+ | public class Test { | ||
+ | public static void main(String[] args) { | ||
+ | String tb = """ | ||
+ | Hello | ||
+ | World | ||
+ | ""; | ||
+ | } | ||
+ | }</source> | ||
+ | |||
+ | <br> | ||
+ | <br> | ||
+ | [[File:textblock.error.png]] | ||
+ | |Compilation error - text block not closed properly | ||
|- | |- |
Latest revision as of 00:21, 16 September 2019
This is an informal page listing examples of features that are implemented by the Java 13 Support. You are welcome to try out these examples. If you find bugs, please file a bug after checking for a duplicate entry here
NOTE:
- Switch expression, Enhanced switch statement and Multi-constant case labels are Preview features in Java 13. They are not enabled by default and can by enabled using --enable-preview.
- TextBlock is also another preview feature in Java 13.
- In Eclipse, --enable-preview can be enabled from the Preferences. It is implicitly added while launching a java program if the feature has been enabled for the project/workspace.