Skip to main content
Jump to: navigation, search

Java13/Examples

Revision as of 05:06, 28 August 2019 by Manpalat.in.ibm.com (Talk | contribs) (Created page with "=============PAGE UNDER CONSTRUCTION================ =============PAGE NOT UPDATED - PLACEHOLDER WITH JAVA 12 CONTENTS=========== This is an informal page listing examples of...")

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
=======PAGE UNDER CONSTRUCTION==========
=======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 here

NOTE:

  • Switch expression, Enhanced switch statement and Multi-constant case labels are Preview features in Java 12. 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 12 JRE Support
Add Java 12 JRE Use Window -> Preferences-> Java -> Installed JREs -> Add...


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

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



J12.compliance.png

12 is shown in the drop down list.

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

Basic Necessity : Compilation and Error Reporting
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: break "Weekday";
			case SAT, SUN: break "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: break "Weekday";
			case SAT, SUN: break "Weekend";
		};
		return day;
	}
}



Switch.preview.warning.compile.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 : break "Weekday";
			case SAT, SUN : break "Weekend";
		};
		return day;
	}
}

Switch.cover-all-cases.error.png

Compiler error is shown

Back to the top