Skip to main content
Jump to: navigation, search

Difference between revisions of "Java13/Examples"

(Created page with "=============PAGE UNDER CONSTRUCTION================ =============PAGE NOT UPDATED - PLACEHOLDER WITH JAVA 12 CONTENTS=========== This is an informal page listing examples of...")
 
 
(12 intermediate revisions by the same user not shown)
Line 1: Line 1:
=============PAGE UNDER CONSTRUCTION================
+
This is an informal page listing examples of features that are implemented by the Java 13 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]
=============PAGE NOT UPDATED - PLACEHOLDER WITH JAVA 12 CONTENTS===========
+
This is an informal page listing examples of features that are implemented by the Java 13 Support. 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=18507270&namedcmd=J12.Open&remaction=run&sharer_id=152344 here]
+
  
 
<b>NOTE:</b>
 
<b>NOTE:</b>
* Switch expression, Enhanced switch statement and Multi-constant case labels are <b>Preview features</b> in Java 12. They are not enabled by default and can by enabled using <b>--enable-preview</b>.
+
* Switch expression, Enhanced switch statement and Multi-constant case labels are <b>Preview features</b> in Java 13. They are not enabled by default and can by enabled using <b>--enable-preview</b>.
 +
* TextBlock is also another preview feature in Java 13.
 
* 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 13: Line 12:
 
! style="width: 25%;" | Expected Result
 
! style="width: 25%;" | Expected Result
 
|-
 
|-
! colspan="3" | The Pre-requisite: Java 12 JRE Support
+
! colspan="3" | The Pre-requisite: Java 13 JRE Support
 
|-
 
|-
! scope="row" | Add Java 12 JRE
+
! scope="row" | Add Java 13 JRE
 
| Use Window -> Preferences-> Java -> Installed JREs -> Add... <br>
 
| Use Window -> Preferences-> Java -> Installed JREs -> Add... <br>
 
<br>
 
<br>
[[File:FileAddJ12.png]]
+
[[File:FileAddJ13.png]]
 
<br>
 
<br>
 
[note: Eclipse -> Preferences in Mac / Window -> Preferences in Windows]
 
[note: Eclipse -> Preferences in Mac / Window -> Preferences in Windows]
| Java 12 JRE recognized as a valid JRE
+
| Java 13 JRE recognized as a valid JRE
 
|-
 
|-
 
! scope="row" | Project JRE
 
! scope="row" | Project JRE
| In Package Explorer Use project's context menu and add Java 12 JRE  || JRE specific (eg Object) gets resolved in the project.
+
| In Package Explorer Use project's context menu and add Java 13 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 12 JRE || Modules (eg java.base etc) are listed in the package explorer view
+
| Go to Package Explorer and expand the Java 13 JRE || Modules (eg java.base etc) are listed in the package explorer view
 
|-
 
|-
! colspan="3" | The First Step: Java 12 Compliance
+
! colspan="3" | The First Step: Java 13 Compliance
 
|-
 
|-
 
! scope="row" | Set Project Compliance in Package Explorer
 
! scope="row" | Set Project Compliance in Package Explorer
| Context Menu of Project -> Properties -> Set project-specific, drop down to 12
+
| Context Menu of Project -> Properties -> Set project-specific, drop down to 13
 
<br>
 
<br>
 
<br>
 
<br>
[[File:j12.compliance.png]]
+
[[File:j13.compliance.png]]
|12 is shown in the drop down list.
+
|13 is shown in the drop down list.
 
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" | Basic Necessity : Compilation and Error Reporting
+
! colspan="3" | Preview 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 118: Line 117:
 
public String getDay_1 (Day today) {
 
public String getDay_1 (Day today) {
 
String day = switch(today) {
 
String day = switch(today) {
case MON, TUE, WED, THUR, FRI: break "Weekday";
+
case MON, TUE, WED, THUR, FRI-> "Weekday";
case SAT, SUN: break "Weekend";
+
case SAT, SUN->"Weekend";
 
};
 
};
 
return day;
 
return day;
Line 135: Line 134:
 
<source lang="java">
 
<source lang="java">
 
public class Test {
 
public class Test {
+
 
enum Day {
 
enum Day {
 
MON, TUE, WED, THUR, FRI, SAT, SUN
 
MON, TUE, WED, THUR, FRI, SAT, SUN
 
};
 
};
+
 
public String getDay_1 (Day today) {
 
public String getDay_1 (Day today) {
 
String day = switch(today) {
 
String day = switch(today) {
case MON, TUE, WED, THUR, FRI: break "Weekday";
+
case MON, TUE, WED, THUR, FRI : yield "Weekday";
case SAT, SUN: break "Weekend";
+
case SAT, SUN : yield "Weekend";
 
};
 
};
 
return day;
 
return day;
Line 151: Line 150:
 
<br>
 
<br>
 
<br>
 
<br>
[[File:switch.preview.warning.compile.png]]
+
[[File:switch.preview.warning.compile.yield.png]]
 
| Compiler warning is shown for preview feature
 
| Compiler warning is shown for preview feature
 
|-
 
|-
Line 166: Line 165:
 
public String getDay_1 (Day today) {
 
public String getDay_1 (Day today) {
 
String day = switch(today) {
 
String day = switch(today) {
case MON : break "Weekday";
+
case MON : yield "Weekday";
case SAT, SUN : break "Weekend";
+
case SAT, SUN : yield "Weekend";
 
};
 
};
 
return day;
 
return day;
Line 173: Line 172:
 
}
 
}
 
</source>
 
</source>
[[File:switch.cover-all-cases.error.png]]
+
[[File:switch.cover-all-cases.error.yield.png]]
 
| Compiler error is shown
 
| Compiler error is shown
 +
|-
 +
! colspan="3" | Preview Feature: Text Blocks
 +
|-
 +
! scope="row" | Text Block Example
 +
| Compile and run the following code:
 +
<source lang="java">
 +
@SuppressWarnings("preview")
 +
public class Test {
 +
public static void main(String[] args) {
 +
String tb = """
 +
Hello
 +
World
 +
""";
 +
System.out.println(tb);
 +
}
 +
}
 +
</source>
 +
 +
<br>
 +
<br>
 +
[[File:textblock.png]]
 +
| Code compiles prints both "Hello" "World" as it is - notice that "World" is printed in the next line.
 +
|-
 +
! scope="row" | Text Block Compilation Error Example
 +
| Use the following code:
 +
<source lang="java">
 +
public class Test {
 +
public static void main(String[] args) {
 +
String tb = """
 +
Hello
 +
World
 +
"";
 +
}
 +
}</source>
 +
 +
<br>
 +
<br>
 +
[[File:textblock.error.png]]
 +
|Compilation error - text block not closed properly
 
|-
 
|-

Latest revision as of 23:21, 15 September 2019

This is an informal page listing examples of features that are implemented by the Java 13 Support. You are welcome to try out these examples. If you find bugs, please file a bug after checking for a duplicate entry here

NOTE:

  • Switch expression, Enhanced switch statement and Multi-constant case labels are Preview features in Java 13. They are not enabled by default and can by enabled using --enable-preview.
  • TextBlock is also another preview feature in Java 13.
  • 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 13 JRE Support
Add Java 13 JRE Use Window -> Preferences-> Java -> Installed JREs -> Add...


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

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



J13.compliance.png

13 is shown in the drop down list.

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

Preview 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 (with a preview warning) and while running prints both "Hello" "World"
Positive Compilation 2 (Switch Statement with case with arrow) Use the following code:
public class X {
    @SuppressWarnings("preview")
	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 (with a preview warning) 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
	};
 
	@SuppressWarnings("preview")
	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
Compiler error Case
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;
	}
}



Enable-preview.error.png

Compiler error is shown for preview feature.

Quick fix is available to enable the preview feature in the preferences.

Compiler Warning Case
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 : yield "Weekday";
			case SAT, SUN : yield "Weekend";
		};
		return day;
	}
}



Switch.preview.warning.compile.yield.png

Compiler warning is shown for preview feature
Compiler Error Case
public class Test {
 
	enum Day {
		MON, TUE, WED, THUR, FRI, SAT, SUN
	};
 
	@SuppressWarnings("preview")
	public String getDay_1 (Day today) {
		String day = switch(today) {
			case MON : yield "Weekday";
			case SAT, SUN : yield "Weekend";
		};
		return day;
	}
}

Switch.cover-all-cases.error.yield.png

Compiler error is shown
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 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

Back to the top