Skip to main content

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.

Jump to: navigation, search

Difference between revisions of "Java18/Examples"

(Created page with "This is an informal page listing examples of features that are implemented by the Java 18 Support, which can be installed from the [https://marketplace.eclipse.org/content/jav...")
 
Line 182: Line 182:
 
at X.foo(X.java:5)<br>
 
at X.foo(X.java:5)<br>
 
at X.main(X.java:12)<br>
 
at X.main(X.java:12)<br>
|-
 
|-
 
! scope="row" | Negative compilation1 (Pattern Matching for switch Example)
 
| Use the following code:
 
<source lang="java">
 
public class X {
 
private static void foo(Object o) {
 
  switch (o) {
 
    case Integer t, String s, X x : System.out.println("Integer, String or X");
 
    default : System.out.println("Object");
 
  }
 
}
 
 
public static void main(String[] args) {
 
foo("Hello World");
 
}
 
}</source>
 
<br>
 
<br>
 
[[File:Pattern-matching-switch-negative1-Java17.png]]
 
|Code fails to compile with below error:<br>
 
A switch label may not have more than one pattern case label element
 
|-
 
|-
 
! scope="row" | Negative compilation2 (Pattern Matching for switch Example)
 
| Use the following code:
 
<source lang="java">
 
public class X {
 
private static void foo(Object o) {
 
  switch (o) {
 
    case Integer t, String s && s.length > 0, X x && x.hashCode() > 10 : System.out.println("Integer, String or X");
 
    default : System.out.println("Object");
 
  }
 
}
 
 
public static void main(String[] args) {
 
foo("Hello World");
 
}
 
}</source>
 
<br>
 
<br>
 
[[File:Pattern-matching-switch-negative2-Java17.png]]
 
|Code fails to compile with below error:<br>
 
A switch label may not have more than one pattern case label element
 
|-
 
|-
 
! scope="row" | Negative compilation3 (Pattern Matching for switch Example)
 
| Use the following code:
 
<source lang="java">
 
public class X {
 
private static void foo(Object o) {
 
  switch (o) {
 
    case Integer t, String : System.out.println("Error should be flagged for String");
 
    default : System.out.println("Object");
 
  }
 
}
 
 
public static void main(String[] args) {
 
foo("Hello World");
 
}
 
}</source>
 
<br>
 
<br>
 
[[File:Pattern-matching-switch-negative3-Java17.png]]
 
|Code fails to compile with below error:<br>
 
String cannot be resolved to a variable
 
|-
 
|-
 
! scope="row" | Negative compilation4 (Pattern Matching for switch Example)
 
| Use the following code:
 
<source lang="java">
 
public class X {
 
private static void foo(Object o) {
 
  switch (o.hashCode()) {
 
    case Integer t:
 
    default : System.out.println("Object");
 
  }
 
}
 
 
public static void main(String[] args) {
 
foo("Hello World");
 
}
 
}</source>
 
<br>
 
<br>
 
[[File:Pattern-matching-switch-negative4-Java17.png]]
 
|Code fails to compile with below error:<br>
 
Type mismatch: cannot convert from int to Integer
 
|-
 
|-
 
! scope="row" | Negative compilation5 (Pattern Matching for switch Example)
 
| Use the following code:
 
<source lang="java">
 
public class X {
 
private static void foo(Object o) {
 
switch (o.hashCode()) {
 
case default:
 
System.out.println("Default");
 
default:
 
System.out.println("Object");
 
}
 
}
 
 
public static void main(String[] args) {
 
foo("Hello World");
 
}
 
}</source>
 
<br>
 
<br>
 
[[File:Pattern-matching-switch-negative5-Java17.png]]
 
|Code fails to compile with below error:<br>
 
The default case is already defined
 
|-
 
|-
 
! scope="row" | Negative compilation6 (Pattern Matching for switch Example)
 
| Use the following code:
 
<source lang="java">
 
public class X {
 
private static void foo(Object o) {
 
  switch (o.hashCode()) {
 
    case var s : System.out.println("Error should be ANY_PATTERN");
 
    default : System.out.println("Object");
 
  }
 
}
 
 
public static void main(String[] args) {
 
foo("Hello World");
 
}
 
}</source>
 
<br>
 
<br>
 
[[File:Pattern-matching-switch-negative6-Java17.png]]
 
|Code fails to compile with below error:<br>
 
'var' is not allowed here
 
|-
 
! scope="row" | Negative compilation7 (Pattern Matching for switch Example)
 
| Use the following code:
 
<source lang="java">
 
public class X {
 
private static void foo(Object o) {
 
switch (o) {
 
case 1:
 
System.out.println("Integer");
 
break;
 
default:
 
System.out.println("Object");
 
}
 
}
 
 
public static void main(String[] args) {
 
foo("Hello World");
 
}
 
}
 
</source>
 
<br>
 
<br>
 
[[File:Pattern-matching-switch-negative7-Java17.png]]
 
|Code fails to compile with below error:<br>
 
Type mismatch: cannot convert from int to Object
 
|-
 
|-
 
! scope="row" | Negative compilation8 (Pattern Matching for switch Example)
 
| Use the following code:
 
<source lang="java">
 
public class X {
 
private static void foo(Object o) {
 
  switch (o) {
 
    case Integer I:
 
      System.out.println("Integer");
 
      System.out.println(I);
 
    case String s && s.length()>1:
 
      System.out.println("String s && s.length()>1");
 
      System.out.println(s);
 
      break;
 
    case X x:
 
      System.out.println("X");
 
      System.out.println(x);
 
      break;
 
    default : System.out.println("Object");
 
  }
 
}
 
 
public static void main(String[] args) {
 
foo("Hello World!");
 
}
 
}
 
</source>
 
<br>
 
<br>
 
[[File:Pattern-matching-switch-negative8-Java17.png]]
 
|Code fails to compile with below error:<br>
 
Illegal fall-through to a pattern
 
|-
 
|-
 
! scope="row" | Negative compilation9 (Pattern Matching for switch Example)
 
| Use the following code:
 
<source lang="java">
 
public class X {
 
private static void foo(int o) {
 
  switch (o) {
 
    case 10:
 
      System.out.println("Integer");
 
      System.out.println(o);
 
      break;
 
    case null:
 
      System.out.println("NULL");
 
      break;
 
    default : System.out.println(o);
 
  }
 
 
  public static void main(String[] args) {
 
    foo(0);
 
}
 
}
 
</source>
 
<br>
 
<br>
 
[[File:Pattern-matching-switch-negative9-Java17.png]]
 
|Code fails to compile with below error:<br>
 
Type mismatch: cannot convert from null to int
 
|-
 
|-
 
! scope="row" | Negative compilation10 (Pattern Matching for switch Example)
 
| Use the following code:
 
<source lang="java">
 
public class X {
 
public static void foo(Object o) {
 
int len = 2;
 
switch (o) {
 
case String o1 && o1.length() > len:
 
len = 0;
 
break;
 
default:
 
break;
 
}
 
}
 
}
 
</source>
 
<br>
 
<br>
 
[[File:Pattern-matching-switch-negative10-Java17.png]]
 
|Code fails to compile with below error:<br>
 
Local variable len referenced from a guard must be final or effectively final
 
|-
 
|-
 
! scope="row" | Negative compilation11 (Pattern Matching for switch Example)
 
| Use the following code:
 
<source lang="java">
 
public class X {
 
public static void foo(Object o) {
 
final int len = 2;
 
switch (o) {
 
case String o1 && len < o1.length():
 
len = 0;
 
break;
 
default:
 
break;
 
}
 
}
 
}
 
</source>
 
<br>
 
<br>
 
[[File:Pattern-matching-switch-negative11-Java17.png]]
 
|Code fails to compile with below error:<br>
 
The final local variable len cannot be assigned. It must be blank and not using a compound assignment<br>
 
|-
 
|-
 
! scope="row" | Negative compilation12 (Pattern Matching for switch Example)
 
| Use the following code:
 
<source lang="java">
 
public class X {
 
public static void main(String[] args) {
 
}
 
private static void foo1(int o) {
 
switch (o) {
 
case 20 -> System.out.println("20");
 
case null -> System.out.println("null");
 
}
 
}
 
}
 
</source>
 
<br>
 
<br>
 
[[File:Pattern-matching-switch-negative12-Java17.png]]
 
|Code fails to compile with below errors:<br>
 
- An enhanced switch statement should be exhaustive; a default label expected<br>
 
- Type mismatch: cannot convert from null to int<br>
 
|-
 
|-
 
! scope="row" | Negative compilation13 (Pattern Matching for switch Example)
 
| Use the following code:
 
<source lang="java">
 
public class X {
 
public static void main(String[] args) {
 
}
 
 
private static void foo1(Integer o) {
 
  switch (o) {
 
    case  Integer i, 30  -> System.out.println(o);
 
  }
 
}
 
}
 
</source>
 
<br>
 
<br>
 
[[File:Pattern-matching-switch-negative13-Java17.png]]
 
|Code fails to compile with below error:<br>
 
- This case label is dominated by one of the preceding case label
 
|-
 
|-
 
! scope="row" | Negative compilation14 (Pattern Matching for switch Example)
 
| Use the following code:
 
<source lang="java">
 
public class X {
 
public static void foo(Number n) {
 
int j =
 
switch (n) {
 
case Integer i -> {
 
}
 
default -> {
 
yield 1;
 
}
 
};
 
}
 
}
 
</source>
 
<br>
 
<br>
 
[[File:Pattern-matching-switch-negative14-Java17.png]]
 
|Code fails to compile with below error:<br>
 
- A switch labeled block in a switch expression should not complete normally
 
|-
 
|-
 
! scope="row" | Negative compilation15 (Pattern Matching for switch Example)
 
| Use the following code:
 
<source lang="java">
 
public class X {
 
public static void main(String[] args) {
 
foo(10);
 
}
 
 
private static void foo(Integer o) {
 
switch (o) {
 
  case  default, var k  -> System.out.println(0);
 
}
 
}
 
}
 
</source>
 
<br>
 
<br>
 
[[File:Pattern-matching-switch-negative15-Java17.png]]
 
|Code fails to compile with below errors:<br>
 
Multiple markers at this line
 
- A switch label may not have both a pattern case label element and a default case label element
 
- 'var' is not allowed here
 
 
|-
 
|-
 
! colspan="3" | Standard Feature: Restore Always-Strict Floating-Point Semantics
 
! colspan="3" | Standard Feature: Restore Always-Strict Floating-Point Semantics
Line 552: Line 196:
 
|Code compiles: strictfp applies to all methods in this class
 
|Code compiles: strictfp applies to all methods in this class
 
|-
 
|-
! scope="row" | Positive compilation2 (Restore Always-Strict Floating-Point Semantics Example)
 
| Use the following code:
 
<source lang="java">
 
strictfp interface X {// strictfp applies to all methods in this interface
 
}</source>
 
 
<br>
 
<br>
 
[[File:Restore-Always-Strict-Floating-Point-Semantics-class2-Java17.png]]
 
|Code compiles: strictfp applies to all methods in this interface
 
|-
 
! scope="row" | Positive compilation3 (Restore Always-Strict Floating-Point Semantics Example)
 
| Use the following code:
 
<source lang="java">
 
class X {
 
strictfp void foo() {// strictfp applies to method foo only
 
}
 
}</source>
 
 
<br>
 
<br>
 
[[File:Restore-Always-Strict-Floating-Point-Semantics-class3-Java17.png]]
 
|Code compiles: strictfp applies to method foo only
 
|-
 
! scope="row" | Positive compilation4 (Restore Always-Strict Floating-Point Semantics Example)
 
| Use the following code:
 
<source lang="java">
 
strictfp class X {// strictfp applies to all methods in this class
 
strictfp void foo() {// strictfp declaration redundant, but still valid
 
}
 
}</source>
 
 
<br>
 
<br>
 
[[File:Restore-Always-Strict-Floating-Point-Semantics-class4-Java17.png]]
 
|Code compiles:<br>
 
- strictfp applies to all methods in this class<br>
 
- strictfp declaration redundant, but still valid
 
|-
 
! scope="row" | Negative compilation1 (Restore Always-Strict Floating-Point Semantics Example)
 
| Use the following code:
 
<source lang="java">
 
class X {
 
strictfp float f;
 
}</source>
 
<br>
 
<br>
 
[[File:Restore-Always-Strict-Floating-Point-Semantics-Negative1-Java17.png]]
 
|Code fails to compile with below error:<br>
 
Illegal modifier for the field f; only public, protected, private, static, final, transient & volatile are permitted
 
|-
 
! scope="row" | Negative compilation2 (Restore Always-Strict Floating-Point Semantics Example)
 
| Use the following code:
 
<source lang="java">
 
class X {
 
public strictfp X() {
 
 
};
 
}</source>
 
<br>
 
<br>
 
[[File:Restore-Always-Strict-Floating-Point-Semantics-Negative2-Java17.png]]
 
|Code fails to compile with below error:<br>
 
Illegal modifier for the constructor in type X; only public, protected & private are permitted
 
|-
 
! scope="row" | Negative compilation3 (Restore Always-Strict Floating-Point Semantics Example)
 
| Use the following code:
 
<source lang="java">
 
interface X {
 
strictfp void foo();
 
}</source>
 
<br>
 
<br>
 
[[File:Restore-Always-Strict-Floating-Point-Semantics-Negative3-Java17.png]]
 
|Code fails to compile with below error:<br>
 
strictfp is not permitted for abstract interface method foo
 
|-
 
! scope="row" | Negative compilation4 (Restore Always-Strict Floating-Point Semantics Example)
 
| Use the following code:
 
<source lang="java">
 
abstract class X {
 
public abstract strictfp void test();
 
}</source>
 
<br>
 
<br>
 
[[File:Restore-Always-Strict-Floating-Point-Semantics-Negative4-Java17.png]]
 
|Code fails to compile with below error:<br>
 
The abstract method test in type X can only set a visibility modifier, one of public or protected
 
|-
 
! colspan="3" | Standard Feature: Sealed Classes
 
|-
 
! scope="row" | Syntax coloring for “sealed”, “non-sealed”, “permits”
 
|
 
[[File:Syntax-coloring.png]]
 
|New restricted keywords are colored
 
|-
 
! scope="row" | Positive compilation1 (Sealed Class Example)
 
| Use the following code:
 
<source lang="java">
 
sealed class Y permits X {
 
}
 
 
non-sealed class X extends Y {
 
public static void main(String[] args) {
 
System.out.println(0);
 
}
 
}</source>
 
 
<br>
 
<br>
 
[[File:Sealed-class1-Java17.png]]
 
|Code compiles and prints 0.
 
|-
 
! scope="row" | Positive compilation2 (Sealed Class Example)
 
| Use the following code:
 
<source lang="java">
 
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 {
 
}</source>
 
 
<br>
 
<br>
 
[[File:Sealed-class2-Java17.png]]
 
|Code compiles and prints 0.
 
|-
 
! scope="row" | Positive compilation3 (Sealed Class Example)
 
| Use the following code:
 
<source lang="java">
 
sealed class X permits Y {
 
public static void main(String[] args) {
 
System.out.println(100);
 
}
 
}
 
 
non-sealed class Y extends X {
 
}</source>
 
 
<br>
 
<br>
 
[[File:Sealed-class3-Java17.png]]
 
|Code compiles and prints 100.
 
|-
 
! scope="row" | Positive compilation4 (Sealed Class Example)
 
| Use the following code:
 
<source lang="java">
 
sealed public class X<T> {
 
public static void main(String[] args) {
 
System.out.println(100);
 
}
 
}
 
 
non-sealed class Y extends X {
 
}</source>
 
 
<br>
 
<br>
 
[[File:Sealed-class4-Java17.png]]
 
|Code compiles and prints 100.
 
|-
 
! scope="row" | Negative compilation1 (Sealed Class Example)
 
| Use the following code:
 
<source lang="java">
 
sealed public sealed class X {
 
public static void main(String[] args) {
 
System.out.println(100);
 
}
 
}</source>
 
 
<br>
 
<br>
 
[[File:Sealed-class5-Java17.png]]
 
|Code fails to compile with error "Multiple markers at this line<br>
 
- Duplicate modifier for the type X<br>
 
- Sealed class or interface lacks the permits clause and no class or interface from the same compilation unit declares X as its direct superclass or superinterface"<br>
 
|-
 
! scope="row" | "Match locations" dialog supports "Permitted type declarations"
 
|
 
[[File:Permitted-Type-Declarations.png]]
 
|"Search > Java Search > Match locations" dialog supports "Permitted type declarations" check-box option.
 

Revision as of 04:52, 21 February 2022

This is an informal page listing examples of features that are implemented by the Java 18 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 a standard features in Java 18.
  • Restore Always-Strict Floating-Point Semantics is a standard features in Java 18.
  • Pattern Matching for switch is preview feature in Java 18. 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
Postive compilation1 (Pattern Matching for switch Example) Use the following code:
public class X {
	private static void foo(Object o) {
	   switch (o) {
	     case Integer i     -> System.out.println("String:");
	     case String s     	-> System.out.println("String: Hello World!");
	     default       	-> System.out.println("Object");
	   }
	}
 
	public static void main(String[] args) {
		foo("Hello World");
	}
}



Pattern-matching-switch-positive1-Java17.png

Code compiles and prints below message:

String: Hello World!

Positive compilation2 (Pattern Matching for switch Example) Use the following code:
public class X {
	private static void foo(Object o) {
	   switch (o) {
	     case Integer I: System.out.println("Integer"); break;
	     case String s && s.length()>1: System.out.println("String > 1"); break;
	     case String s1: System.out.println("String"); break;
	     case X x: System.out.println("X"); break;
	     default : System.out.println("Object");
	   }
	}
 
	public static void main(String[] args) {
		foo("Hello World!");
		foo("H");
		foo(bar());
	}
 
	public static Object bar() {
		return new Object();
	}
}



Pattern-matching-switch-positive2-Java17.png

Code compiles and prints below message:

String > 1
String
Object

Positive compilation3 (Pattern Matching for switch Example) Use the following code:
public class X {
	private static void foo(Object o) {
	   switch (o) {
	     case Integer I: 
	       System.out.println("Integer"); 
	       System.out.println(I); 
	       break; 
	     case String s && s.length()>1: 
	       System.out.println("String s && s.length()>1"); 
	       System.out.println(s); 
	       break;
	     case String s: 
	       System.out.println("String"); 
	       System.out.println(s);
	       break; 
	     case X x:
	       System.out.println("X"); 
	       System.out.println(x);
	       break;
	     default : System.out.println("Object"); 
	   }
	}
 
	public static void main(String[] args) {
		foo("Hello World!");
		foo("H");
		foo(bar());
	}
 
	public static Object bar() {
		return new Object();
	}
}



Pattern-matching-switch-positive3-Java17.png

Code compiles and prints below message:

String s && s.length()>1
Hello World!
String
H
Object

Positive compilation4 (Pattern Matching for switch Example) Use the following code:
public class X {
	public static void main(String[] args) {
		foo(Integer.valueOf(11));
		foo(Integer.valueOf(9));
	}
 
	private static void foo(Object o) {
	   switch (o) {
	   case Integer i && i>10:
	     System.out.println("Greater than 10:" + o);
	     break;
	   case Integer j && j>0:
	     System.out.println("Greater than 0:" + o);
	     break;
	   default:
	     System.out.println("Object" + o);
	   }
	}
}



Pattern-matching-switch-positive4-Java17.png

Code compiles and prints below message:

Greater than 10:11 Greater than 0:9

Positive compilation5 (Pattern Matching for switch Example) Use the following code:
public class X {
	public static void foo(Object o) throws Exception {
		switch (o) {
			case String s1:
				throw new Exception(s1);
			default:
				break;
		}
	}
 
	public static void main(String[] args) throws Exception {
		foo("hello");
	}
}



Pattern-matching-switch-positive5-Java17.png

Code compiles and prints below exception stack trace message:

Exception in thread "main" java.lang.Exception: hello
at X.foo(X.java:5)
at X.main(X.java:12)

Standard Feature: Restore Always-Strict Floating-Point Semantics
Positive compilation1 (Restore Always-Strict Floating-Point Semantics Example) Use the following code:
strictfp public class X { // strictfp applies to all methods in this class
}



Restore-Always-Strict-Floating-Point-Semantics-class1-Java17.png

Code compiles: strictfp applies to all methods in this class

Back to the top