Notice: This Wiki is now read only and edits are no longer possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.
Difference between revisions of "Java14/Examples"
(13 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
− | + | This is an informal page listing examples of features that are implemented by the Java 14 Support, which can be installed from the [https://marketplace.eclipse.org/content/java-14-support-eclipse-2020-03-415 Marketplace]. You are welcome to try out these examples. If you find bugs, please file a bug after checking for a duplicate entry [https://bit.ly/2TRS4CO here] | |
− | 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 [https://bit.ly/2TRS4CO here] | + | |
+ | Watch out for additional examples being added soon. | ||
<b>NOTE:</b> | <b>NOTE:</b> | ||
Line 6: | Line 7: | ||
* TextBlock is also another preview feature in Java 14. They are not enabled by default and can by enabled using <b>--enable-preview</b>. | * TextBlock is also another preview feature in Java 14. They are not enabled by default and can by enabled using <b>--enable-preview</b>. | ||
* Records is also another preview feature in Java 14. They are not enabled by default and can by enabled using <b>--enable-preview</b>. | * Records is also another preview feature in Java 14. They are not enabled by default and can by enabled using <b>--enable-preview</b>. | ||
− | * Pattern | + | * Pattern instanceof is also another preview feature in Java 14. They are not enabled by default and can by enabled using <b>--enable-preview</b>. |
* 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 167: | Line 168: | ||
| Code compiles and prints 0. | | Code compiles and prints 0. | ||
|- | |- | ||
− | ! scope="row" | Positive | + | ! scope="row" | Positive compilation5 |
| Compile and run the following code: | | Compile and run the following code: | ||
<source lang="java"> | <source lang="java"> | ||
Line 184: | Line 185: | ||
} | } | ||
</source> | </source> | ||
− | | Code compiles and prints | + | | Code compiles and prints true. |
|- | |- | ||
− | ! scope="row" | Positive | + | ! scope="row" | Positive compilation6 |
| Compile and run the following code: | | Compile and run the following code: | ||
<source lang="java"> | <source lang="java"> | ||
Line 198: | Line 199: | ||
} | } | ||
</source> | </source> | ||
− | | Code compiles and prints | + | | Code compiles and prints 100. |
|- | |- | ||
! scope="row" | Negative compilation1 (Record Example) | ! scope="row" | Negative compilation1 (Record Example) | ||
Line 244: | Line 245: | ||
</source> | </source> | ||
| Code fails to compile with error "Record is a restricted identifier and hence not a valid type name" | | Code fails to compile with error "Record is a restricted identifier and hence not a valid type name" | ||
+ | |- | ||
+ | ! scope="row" | Record Creation Wizard | ||
+ | | Right Click on the Project -> New -> Record <b>or</b> Right Click on the Project -> New -> Other and search for Record <b>or</b> Right Click on the Project -> New -> Other -> Java -> Record<br> | ||
+ | <br> | ||
+ | [[File:FileAddJ14RecordCreation.png]] | ||
+ | <br> | ||
+ | <br> | ||
+ | [note: In older workspaces Record option may not appear diectly under New menu in java perspective. To resolve this, use a new workspace or relaunch eclipse with -clearPersistedState for the same workspace] | ||
+ | | Record is created | ||
|- | |- | ||
! colspan="3" | Preview Feature: Text Blocks | ! colspan="3" | Preview Feature: Text Blocks | ||
Line 265: | Line 275: | ||
<br> | <br> | ||
[[File:textblock.png]] | [[File:textblock.png]] | ||
− | | Code compiles prints both "Hello" "World" as it is - notice that "World" is printed in the next line. | + | | Code compiles and prints both "Hello" "World" as it is - notice that "World" is printed in the next line. |
|- | |- | ||
! scope="row" | Text Block Compilation Error Example | ! scope="row" | Text Block Compilation Error Example | ||
Line 283: | Line 293: | ||
[[File:textblock.error.png]] | [[File:textblock.error.png]] | ||
|Compilation error - text block not closed properly | |Compilation error - text block not closed properly | ||
+ | |- | ||
+ | ! colspan="3" | Preview Feature: Instanceof Pattern Matching | ||
+ | |- | ||
+ | ! scope="row" | Instanceof Pattern Matching Example | ||
+ | | Use the following code: | ||
+ | <source lang="java"> | ||
+ | @SuppressWarnings("preview") | ||
+ | public class X { | ||
+ | public boolean isBlank(Object o) { | ||
+ | return (o instanceof String s) && s.isBlank(); | ||
+ | } | ||
+ | }</source> | ||
+ | |||
+ | <br> | ||
+ | <br> | ||
+ | [[File:Pattern-match1.png]] | ||
+ | |The pattern variable 's' is in current scope | ||
+ | |- | ||
+ | ! scope="row" | Instanceof Pattern Matching Example | ||
+ | | Use the following code: | ||
+ | <source lang="java"> | ||
+ | @SuppressWarnings("preview") | ||
+ | public class X { | ||
+ | public int size(Object obj) { | ||
+ | if (obj instanceof String s) { | ||
+ | return s.le | ||
+ | } | ||
+ | return -1; | ||
+ | } | ||
+ | }</source> | ||
+ | |||
+ | <br> | ||
+ | <br> | ||
+ | [[File:Pattern-match2.png]] | ||
+ | |The pattern variable 's' is in current scope inside 'then' statement and completion proposes applicable methods on String, the pattern matched type. | ||
+ | |- | ||
+ | ! scope="row" | Instanceof Pattern Matching Example | ||
+ | | Use the following code: | ||
+ | <source lang="java"> | ||
+ | package p; | ||
+ | |||
+ | @SuppressWarnings("preview") | ||
+ | public class X { | ||
+ | public int size(Object obj) { | ||
+ | if (obj instanceof String s) { | ||
+ | return s.length(); | ||
+ | } | ||
+ | return s.length(); // s not in scope | ||
+ | } | ||
+ | } | ||
+ | </source> | ||
+ | |||
+ | <br> | ||
+ | <br> | ||
+ | [[File:Pattern-match3.png]] | ||
+ | |The pattern variable 's' is rejected by the compiler when not in scope outside the 'then' statement. | ||
|- | |- |
Latest revision as of 01:15, 20 March 2020
This is an informal page listing examples of features that are implemented by the Java 14 Support, which can be installed from the Marketplace. You are welcome to try out these examples. If you find bugs, please file a bug after checking for a duplicate entry here
Watch out for additional examples being added soon.
NOTE:
- Switch expression, Enhanced switch statement and Multi-constant case labels are standard features in Java 14.
- TextBlock is also another preview feature in Java 14. They are not enabled by default and can by enabled using --enable-preview.
- Records is also another preview feature in Java 14. They are not enabled by default and can by enabled using --enable-preview.
- Pattern instanceof is also another preview feature in Java 14. They are not enabled by default and can by enabled using --enable-preview.
- 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.