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 "Java10/Examples"
(16 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 [https://marketplace.eclipse.org/content/java-10-support-oxygen Java 10 Support for Oxygen]. You are welcome to try out these examples. If you find bugs, please file a bug after checking for a duplicate entry [https://bugs.eclipse.org/bugs/buglist.cgi?cmdtype=dorem&list_id=17341573&namedcmd=J10.Open&remaction=run&sharer_id=152344 here]. | This is an informal page listing examples of features that are implemented by the [https://marketplace.eclipse.org/content/java-10-support-oxygen Java 10 Support for Oxygen]. You are welcome to try out these examples. If you find bugs, please file a bug after checking for a duplicate entry [https://bugs.eclipse.org/bugs/buglist.cgi?cmdtype=dorem&list_id=17341573&namedcmd=J10.Open&remaction=run&sharer_id=152344 here]. | ||
Line 13: | Line 13: | ||
|- | |- | ||
! scope="row" | Add Java 10 JRE | ! scope="row" | Add Java 10 JRE | ||
− | | Use | + | | Use Window -> Preferences-> Java -> Installed JREs -> Add... <br> |
+ | <br> | ||
[[File:FileAddJ10.jpg]] | [[File:FileAddJ10.jpg]] | ||
+ | <br> | ||
+ | [note: Eclipse -> Preferences in Mac / Window -> Preferences in Windows] | ||
| Java 10 JRE recognized as a valid JRE | | Java 10 JRE recognized as a valid JRE | ||
|- | |- | ||
! scope="row" | Project JRE | ! scope="row" | Project JRE | ||
− | | In Package Explorer Use | + | | In Package Explorer Use project's context menu and add Java 10 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 10 JRE || Modules (eg java.base etc) are listed in the package explorer view | | Go to Package Explorer and expand the Java 10 JRE || Modules (eg java.base etc) are listed in the package explorer view | ||
|- | |- | ||
− | ! colspan="3" | The First Step: | + | ! colspan="3" | The First Step: Java 10 Compliance |
|- | |- | ||
− | ! scope="row" | | + | ! scope="row" | Set Project Compliance in Package Explorer |
− | + | | Context Menu of Project -> Properties -> Set project-specific, drop down to 10 | |
− | + | ||
− | + | ||
− | | Context Menu of Project -> | + | |
<br> | <br> | ||
<br> | <br> | ||
− | [[File: | + | [[File:j10.compliance.jpg]] |
− | | | + | | No compiler errors |
|- | |- | ||
− | ! colspan="3" | Basic Necessity : Compilation | + | ! colspan="3" | Basic Necessity : Compilation and Error Reporting |
|- | |- | ||
− | ! scope="row" | | + | ! scope="row" | Positive Compilation |
− | | | + | | Use the following code: |
− | + | <source lang="java"> | |
− | + | package packvar; | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | |||
− | |||
− | |||
import java.util.ArrayList; | import java.util.ArrayList; | ||
import java.util.List; | import java.util.List; | ||
− | public class | + | public class VarUsage { |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
public static void main(String[] args) { | public static void main(String[] args) { | ||
− | + | String s = new String("Hello World"); | |
+ | |||
+ | var x = s; // var allowed in the variable declaration | ||
+ | System.out.println(x); | ||
+ | |||
List<String> l = new ArrayList<>(); | List<String> l = new ArrayList<>(); | ||
− | + | l.add("List.Hello"); | |
− | + | for (var lx : l) { // var allowed in enhanced for loop | |
+ | System.out.println(lx); | ||
+ | } | ||
} | } | ||
− | }</source> | + | } |
− | + | </source> | |
+ | |||
+ | <br> | ||
+ | <br> | ||
+ | [[File:var.compile.jpg]] | ||
+ | | Code compiles | ||
|- | |- | ||
− | ! scope="row" | | + | ! scope="row" | Compiler Error Cases |
− | | | + | | |
+ | <source lang="java"> | ||
+ | package packvar; | ||
+ | public class VarError { | ||
+ | public static void main(String[] args) { | ||
+ | String s = new String("Hello"); | ||
+ | var x; // compiler error: Cannot use 'var' on variable without initializer | ||
+ | var y = null; // compiler error: Cannot infer type for local variable initialized to 'null' | ||
+ | } | ||
+ | } | ||
+ | </source> | ||
+ | | Compiler errors are shown | ||
+ | |- | ||
+ | ! colspan="3" | Essential Utilities: Code Completion, Hover and Quick Fix. | ||
+ | |- | ||
+ | ! scope="row" | Basic context based var completion | ||
+ | | | ||
+ | <br> | ||
+ | <br> | ||
+ | <source lang="java"> | ||
+ | package packvar; | ||
− | + | public class VarComplete { | |
− | + | public static void main(String[] args) { | |
− | + | String s = new String("hello"); | |
− | + | ||
− | class | + | va // try completing here |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
} | } | ||
− | + | } | |
+ | </source> | ||
+ | [[File:var.complete.jpg]] | ||
+ | | Completes var since the place is appropriate | ||
+ | |- | ||
+ | ! scope="row" | Negative Case - no completion | ||
+ | | | ||
+ | <br> | ||
+ | <source lang="java"> | ||
+ | package packvar; | ||
− | + | public class VarComplete { | |
− | + | va // complete here - 'var' not offered as a completion candidate | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
} | } | ||
</source> | </source> | ||
− | + | <br> | |
+ | [[File:var.nocomplete.jpg]] | ||
+ | | Does not offer 'var' as completion | ||
|- | |- | ||
− | ! scope="row" | | + | ! scope="row" | Hover |
− | | | + | | |
− | + | Hover over var to see the javadoc of the associated type (in this case : String) being displayed. | |
− | + | <br> | |
<source lang="java"> | <source lang="java"> | ||
− | public class | + | package packvar; |
− | + | public class VarComplete { | |
− | + | public static void main(String[] args) { | |
− | + | String s = new String("hello"); | |
− | + | var x = s; | |
+ | System.out.println(x); | ||
+ | } | ||
} | } | ||
− | |||
</source> | </source> | ||
− | + | <br> | |
− | | | + | [[File:var.hover.jpg]] |
+ | | Shows javadoc of the type associated with var | ||
|- | |- | ||
− | ! scope="row" | | + | ! scope="row" | Quick Assist : var to type |
− | | | + | | |
− | + | <br> | |
− | + | <source lang="java"> | |
− | <source lang="java">public class | + | package packvar; |
− | + | public class VarComplete { | |
+ | public static void main(String[] args) { | ||
+ | String s = new String("hello"); | ||
+ | var x = s; | ||
+ | System.out.println(x); | ||
+ | } | ||
} | } | ||
</source> | </source> | ||
− | + | <br> | |
+ | [[File:var.vartotype.jpg]] | ||
+ | | Provides option to change from var to type | ||
|- | |- | ||
− | ! scope="row" | | + | ! scope="row" | Quick Assist: type to var |
− | | | + | | |
− | + | <br> | |
− | + | ||
<source lang="java"> | <source lang="java"> | ||
− | public | + | package packvar; |
− | + | public class VarComplete { | |
− | + | public static void main(String[] args) { | |
− | + | String s = new String("hello"); | |
− | + | String x = s; | |
− | + | System.out.println(x); | |
− | + | } | |
− | + | ||
− | + | ||
− | + | ||
} | } | ||
</source> | </source> | ||
− | + | <br> | |
+ | [[File:var.typetovar.jpg]] | ||
+ | | Provides option to change from type to var | ||
|- | |- | ||
− | ! colspan="3" | | + | ! colspan="3" | More to come in Java 11 ! |
|} | |} |
Latest revision as of 00:31, 28 March 2018
This is an informal page listing examples of features that are implemented by the Java 10 Support for Oxygen. You are welcome to try out these examples. If you find bugs, please file a bug after checking for a duplicate entry here.