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 "Java14/Examples"

 
(22 intermediate revisions by 4 users not shown)
Line 1: Line 1:
THIS PAGE IS UNDER CONSTRUCTION -
+
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 [http://eclip.se/fP 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-InstanceOf is also another preview feature in Java 14. They are not enabled by default and can by enabled using <b>--enable-preview</b>.
+
* 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 41: Line 42:
 
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" | Preview Feature: Switch Expressions, Enhanced Switch Statement and Multi-Label Case Statements.
+
! colspan="3" | Standard 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 61: Line 62:
 
<br>
 
<br>
 
[[File:switch-statement-multi.png]]
 
[[File:switch-statement-multi.png]]
| Code compiles (with a preview warning) and while running prints both "Hello" "World"
+
| Code compiles and while running prints both "Hello" "World"
 
|-
 
|-
 
! scope="row" | Positive Compilation 2 (Switch Statement with case with arrow)
 
! scope="row" | Positive Compilation 2 (Switch Statement with case with arrow)
Line 67: Line 68:
 
<source lang="java">
 
<source lang="java">
 
public class X {
 
public class X {
    @SuppressWarnings("preview")
 
 
public void foo(int i) {
 
public void foo(int i) {
 
     switch (i) {
 
     switch (i) {
Line 108: Line 108:
 
| Code compiles
 
| Code compiles
 
|-
 
|-
! scope="row" | Compiler error Case
+
|-
|  
+
! colspan="3" | Preview Feature: Records
 +
|-
 +
! scope="row" | Postive compilation1 (Record Example)
 +
| Compile and run the following code:
 
<source lang="java">
 
<source lang="java">
public class Test {
+
@SuppressWarnings("preview")
+
record Point(int x, int y) {
enum Day {
+
}
MON, TUE, WED, THUR, FRI, SAT, SUN
+
public class X1 {
};
+
public static void main(String[] args) {
+
                Point p = new Point(100, 200);
public String getDay_1 (Day today) {
+
System.out.println(p.x());
String day = switch(today) {
+
}
case MON, TUE, WED, THUR, FRI-> "Weekday";
+
}
case SAT, SUN->"Weekend";
+
</source>
};
+
| Code compiles and prints 100.
return day;
+
|-
 +
! scope="row" | Positive compilation2 (Nested Record Example)
 +
| Compile and run the following code:
 +
<source lang="java">
 +
class X2 {
 +
public static void main(String[] args) {
 +
System.out.println(0);
 +
}
 +
@SuppressWarnings("preview")
 +
record Point(int x, int y) {
 +
}
 +
}
 +
</source>
 +
| Code compiles and prints 0.
 +
|-
 +
! scope="row" | Positive compilation3 (Record Example)
 +
| Compile and run the following code:
 +
<source lang="java">
 +
class X3 {
 +
public static void main(String[] args) {
 +
System.out.println(0);
 +
}
 +
}
 +
@SuppressWarnings("preview")
 +
final record Point(int x, int y) {
 +
}
 +
</source>
 +
| Code compiles and prints 0. Though a record declaration is implicitly final, it is permitted for the declaration of a record type to redundantly specify the final modifier
 +
|-
 +
! scope="row" | Positive compilation4
 +
| Compile and run the following code:
 +
<source lang="java">
 +
@SuppressWarnings("preview")
 +
record R() {
 +
}
 +
class X4 {
 +
public static void main(String[] args) {
 +
System.out.println(new R().hashCode());
 +
}
 +
}
 +
</source>
 +
| Code compiles and prints 0.
 +
|-
 +
! scope="row" | Positive compilation5
 +
| Compile and run the following code:
 +
<source lang="java">
 +
import java.lang.annotation.Target;
 +
import java.lang.annotation.ElementType;
 +
@Target({ ElementType.PARAMETER })
 +
@interface MyAnnot {
 +
}
 +
@SuppressWarnings("preview")
 +
record R(@MyAnnot()int i, int j) {
 +
}
 +
class X5 {
 +
public static void main(String[] args) {
 +
System.out.println(new R(100, 200).hashCode() != 0);
 +
}
 +
}
 +
</source>
 +
| Code compiles and prints true.
 +
|-
 +
! scope="row" | Positive compilation6
 +
| Compile and run the following code:
 +
<source lang="java">
 +
class X6 {
 +
@SuppressWarnings("preview")
 +
public static void main(String[] args) {
 +
record R(int x,int y){}
 +
R r = new R(100, 200);
 +
System.out.println(r.x());
 
}
 
}
 
}
 
}
 
</source>
 
</source>
 +
| Code compiles and prints 100.
 +
|-
 +
! scope="row" | Negative compilation1 (Record Example)
 +
| Compile and run the following code:
 +
<source lang="java">
 +
@SuppressWarnings("preview")
 +
abstract record Point(int x, int y){
 +
}
 +
class X7 {
 +
public static void main(String[] args){
 +
System.out.println(0);
 +
}
 +
}
 +
</source>
 +
| Code fails to compile with error "Illegal modifier for the record Point; only public, final and strictfp are permitted"
 +
|-
 +
! scope="row" | Negative compilation2 (Record Example)
 +
| Compile and run the following code:
 +
<source lang="java">
 +
@SuppressWarnings("preview")
 +
record Point1(int myInt, char myChar) implements I {
 +
public Point1 {
 +
this.myInt = myInt;
 +
this.myChar = myChar;
 +
}
 +
}
 +
public class X8 {
 +
public static void main(String[] args) {
 +
System.out.println(0);
 +
}
 +
}
 +
interface I {
 +
}
 +
</source>
 +
| Code fails to compile with error "The canonical constructor Point1 of a record declaration must be declared public."
 +
|-
 +
! scope="row" | Negative compilation3 (Record Example)
 +
| Compile and run the following code:
 +
<source lang="java">
 +
class record {
 +
public static void main(String[] args) {
 +
System.out.println(0);
 +
}
 +
}
 +
</source>
 +
| 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>
 
<br>
 +
[[File:FileAddJ14RecordCreation.png]]
 
<br>
 
<br>
[[File:SWITCHEXPRE.error.png]]
+
<br>
| Compiler error is shown for preview feature.
+
[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]
Quick fix is available to enable the preview feature in the preferences.
+
| Record is created
|-
+
 
|-
 
|-
 
! colspan="3" | Preview Feature: Text Blocks
 
! colspan="3" | Preview Feature: Text Blocks
Line 153: 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 171: 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.
Feature / Steps Expected Result
The Pre-requisite: Java 14 JRE Support
Add Java 14 JRE Use Window -> Preferences-> Java -> Installed JREs -> Add...


FileAddJ14.png
[note: Eclipse -> Preferences in Mac / Window -> Preferences in Windows]

Java 14 JRE recognized as a valid JRE
Project JRE In Package Explorer Use project's context menu and add Java 14 JRE JRE specific (eg Object) gets resolved in the project.
Package Explorer Go to Package Explorer and expand the Java 14 JRE Modules (eg java.base etc) are listed in the package explorer view
The First Step: Java 14 Compliance
Set Project Compliance in Package Explorer Context Menu of Project -> Properties -> Set project-specific, drop down to 14



J14.compliance.png

14 is shown in the drop down list.

A checkbox to enable preview features is available on the preference page.

Standard Feature: Switch Expressions, Enhanced Switch Statement and Multi-Label Case Statements.
Positive Compilation 1 (Switch Statement with multi-label case with colon) Use the following code:
public class X {
    public void foo(int i) {
    	switch (i) {
    		case 0, 1, 2: System.out.println("Hello");
    		default :  System.out.println("World");
    	}
    }
    public static void main(String[] argv) {
        new X().foo(2);
    }
}



Switch-statement-multi.png

Code compiles and while running prints both "Hello" "World"
Positive Compilation 2 (Switch Statement with case with arrow) Use the following code:
public class X {
	public void foo(int i) {
    	switch (i) {
    		case  2 -> System.out.println("Hello");
    		default ->  System.out.println("World");
    	}
    }
    public static void main(String[] argv) {
        new X().foo(2);
    }
}



Switch-statement-arrow.png

Code compiles and while running prints only "Hello" (because a break is implicit after every case with an arrow.
Positive Compilation (Switch Expression) Use the following code:
public class Test {
 
	enum Day {
		MON, TUE, WED, THUR, FRI, SAT, SUN
	};
 
	public String getDay_1 (Day today) {
		String day = switch(today) {
			case MON, TUE, WED, THUR, FRI -> "Weekday";
			case SAT, SUN -> "Weekend";
		};
		return day;
	}
}



Switch-exp.compile.png

Code compiles
Preview Feature: Records
Postive compilation1 (Record Example) Compile and run the following code:
@SuppressWarnings("preview")
record Point(int x, int y) {
}
public class X1 {
	public static void main(String[] args) {
                Point p = new Point(100, 200);
		System.out.println(p.x());
	}
}
Code compiles and prints 100.
Positive compilation2 (Nested Record Example) Compile and run the following code:
class X2 {
	public static void main(String[] args) {
		System.out.println(0);
	}
	@SuppressWarnings("preview")
	record Point(int x, int y) {
	}
}
Code compiles and prints 0.
Positive compilation3 (Record Example) Compile and run the following code:
class X3 {
	public static void main(String[] args) {
		System.out.println(0);
	}
}
@SuppressWarnings("preview")
final record Point(int x, int y) {
}
Code compiles and prints 0. Though a record declaration is implicitly final, it is permitted for the declaration of a record type to redundantly specify the final modifier
Positive compilation4 Compile and run the following code:
@SuppressWarnings("preview")
record R() {
}
class X4 {
	public static void main(String[] args) {
		System.out.println(new R().hashCode());
	}
}
Code compiles and prints 0.
Positive compilation5 Compile and run the following code:
import java.lang.annotation.Target;
import java.lang.annotation.ElementType;
@Target({ ElementType.PARAMETER })
@interface MyAnnot {
}
@SuppressWarnings("preview")
record R(@MyAnnot()int i, int j) {
}
class X5 {
	public static void main(String[] args) {
		System.out.println(new R(100, 200).hashCode() != 0);
	}
}
Code compiles and prints true.
Positive compilation6 Compile and run the following code:
class X6 {
	@SuppressWarnings("preview")
	public static void main(String[] args) {
		record R(int x,int y){}
		R r = new R(100, 200);
		System.out.println(r.x());
	}
}
Code compiles and prints 100.
Negative compilation1 (Record Example) Compile and run the following code:
@SuppressWarnings("preview")
abstract record Point(int x, int y){
}
class X7 {
	public static void main(String[] args){
		System.out.println(0);
	}
}
Code fails to compile with error "Illegal modifier for the record Point; only public, final and strictfp are permitted"
Negative compilation2 (Record Example) Compile and run the following code:
@SuppressWarnings("preview")
record Point1(int myInt, char myChar) implements I {
	public Point1 {
		this.myInt = myInt;
		this.myChar = myChar;
	}
}
public class X8 {
	public static void main(String[] args) {
		System.out.println(0);
	}
}
interface I {
}
Code fails to compile with error "The canonical constructor Point1 of a record declaration must be declared public."
Negative compilation3 (Record Example) Compile and run the following code:
class record {
	public static void main(String[] args) {
		System.out.println(0);
	}
}
Code fails to compile with error "Record is a restricted identifier and hence not a valid type name"
Record Creation Wizard Right Click on the Project -> New -> Record or Right Click on the Project -> New -> Other and search for Record or Right Click on the Project -> New -> Other -> Java -> Record


FileAddJ14RecordCreation.png

[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
Preview Feature: Text Blocks
Text Block Example Compile and run the following code:
@SuppressWarnings("preview")
public class Test {
	public static void main(String[] args) {
		String tb = """
		Hello
		World
				""";
		System.out.println(tb);
	}
}



Textblock.png

Code compiles and prints both "Hello" "World" as it is - notice that "World" is printed in the next line.
Text Block Compilation Error Example Use the following code:
public class Test {
	public static void main(String[] args) {
		String tb = """
		Hello
		World
				"";
	}
}



Textblock.error.png

Compilation error - text block not closed properly
Preview Feature: Instanceof Pattern Matching
Instanceof Pattern Matching Example Use the following code:
@SuppressWarnings("preview") 
public class X {
	public boolean isBlank(Object o) {
		return (o instanceof String s) && s.isBlank();
	}
}



Pattern-match1.png

The pattern variable 's' is in current scope
Instanceof Pattern Matching Example Use the following code:
@SuppressWarnings("preview") 
public class X {
	public int size(Object obj) {
		if (obj instanceof String s) {
			return s.le
		}
		return -1;
	}
}



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.
Instanceof Pattern Matching Example Use the following code:
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
	}
}



Pattern-match3.png

The pattern variable 's' is rejected by the compiler when not in scope outside the 'then' statement.

Back to the top