Skip to main content

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.

Jump to: navigation, search

Difference between revisions of "Java17/Examples"

Line 49: Line 49:
 
| Use the following code:
 
| Use the following code:
 
<source lang="java">
 
<source lang="java">
@SuppressWarnings("preview")
 
 
sealed interface I extends SI {
 
sealed interface I extends SI {
 
}
 
}
  
@SuppressWarnings("preview")
 
 
non-sealed class X implements SI {
 
non-sealed class X implements SI {
 
public static void main(String[] args) {
 
public static void main(String[] args) {
Line 60: Line 58:
 
}
 
}
  
@SuppressWarnings("preview")
 
 
sealed interface SI permits X,I {
 
sealed interface SI permits X,I {
 
}
 
}
  
@SuppressWarnings("preview")
 
 
non-sealed interface I2 extends I {
 
non-sealed interface I2 extends I {
 
}</source>
 
}</source>
Line 70: Line 66:
 
<br>
 
<br>
 
<br>
 
<br>
[[File:Sealed-class2.png]]
+
[[File:Sealed-class2-Java17.png]]
 
|Code compiles and prints 0.
 
|Code compiles and prints 0.
 
|-
 
|-

Revision as of 06:32, 23 August 2021

This is an informal page listing examples of features that are implemented by the Java 17 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:

  • Sealed Classes is standard features in Java 17.
  • Restore Always-Strict Floating-Point Semantics is a standard features in Java 17.
  • Pattern Matching for switch is preview feature in Java 17. 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.
Feature / Steps Expected Result
Standard Java/JRE setup with Eclipse More details can be found on dedicated Java/JRE setup page. Setting up Java/JRE in Eclipse.
Overview of eclipse.ini More details can be found on dedicated eclipse.ini page. Specifying the JVM in eclipse.ini
Preview Feature: Pattern Matching for switch
Standard Feature: Sealed Classes
Postive compilation1 (Sealed Class Example) Use the following code:
sealed class Y permits X {
}
 
non-sealed class X extends Y {
	public static void main(String[] args) {
		System.out.println(0);
	}
}



Sealed-class1-Java17.png

Code compiles and prints 0.
Postive compilation2 (Sealed Class Example) Use the following code:
sealed interface I extends SI {
}
 
non-sealed class X implements SI {
	public static void main(String[] args) {
		System.out.println(0);
	}
}
 
sealed interface SI permits X,I {
}
 
non-sealed interface I2 extends I {
}



Sealed-class2-Java17.png

Code compiles and prints 0.
Postive compilation3 (Sealed Class Example) Use the following code:
sealed class X permits Y {
	public static void main(String[] args) {
		System.out.println(100);
	}
}
 
non-sealed class Y extends X {
}



Sealed-class3-Java17.png

Code compiles and prints 100.
Postive compilation4 (Sealed Class Example) Use the following code:
@SuppressWarnings("preview")
sealed public class X<T> {
	public static void main(String[] args) {
		System.out.println(100);
	}
}
 
@SuppressWarnings({ "preview", "rawtypes" })
non-sealed class Y extends X {
}



Sealed-class4.png

Code compiles and prints 100.
Negative compilation1 (Sealed Class Example) Use the following code:
@SuppressWarnings("preview")
sealed public sealed class X {
	public static void main(String[] args) {
		System.out.println(100);
	}
}



Sealed-class5 BetaJava16.png

Code fails to compile with error "Multiple markers at this line

- Sealed class lacks the permits clause and no top level or nested class from the same compilation unit declares X as its direct superclass

- Duplicate modifier for the type X"

Syntax coloring for “sealed”, “non-sealed”, “permits”

Syntax-coloring.png

New restricted keywords are colored
Quick fixes support to add sealed/final/non-sealed modifier on a permitted class declaration.

Quick-fixes-3.png

Three quick fix modifier options:

- sealed

- non-sealed

- final

Quick fixes support to add sealed/non-sealed modifier on a permitted interface declaration.

Quick-fixes-2.png

Two quick fix modifier options:

- sealed

- non-sealed

"Match locations" dialog supports "Permitted type declarations"

Permitted-Type-Declarations.png

"Search > Java Search > Match locations" dialog supports "Permitted type declarations" check-box option.

Back to the top