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

 
(29 intermediate revisions by the same user not shown)
Line 1: Line 1:
===== NOTE: THE PAGE IS UNDER CONSTRUCTION ====
 
 
 
This is an informal page listing examples of features that are implemented by the Java 12 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]
 
This is an informal page listing examples of features that are implemented by the Java 12 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>
 +
* 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>.
 +
* 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.
  
 
{| class="wikitable"
 
{| class="wikitable"
Line 15: Line 16:
 
| Use Window -> Preferences-> Java -> Installed JREs -> Add... <br>
 
| Use Window -> Preferences-> Java -> Installed JREs -> Add... <br>
 
<br>
 
<br>
[[File:FileAddJ11.jpg]]
+
[[File:FileAddJ12.png]]
 
<br>
 
<br>
 
[note: Eclipse -> Preferences in Mac / Window -> Preferences in Windows]
 
[note: Eclipse -> Preferences in Mac / Window -> Preferences in Windows]
Line 32: Line 33:
 
<br>
 
<br>
 
<br>
 
<br>
[[File:j11.compliance.jpg]]
+
[[File:j12.compliance.png]]
|12 is shown in the drop down list
+
|12 is shown in the drop down list.
 +
A checkbox to enable preview features is available on the preference page.
 
|-
 
|-
 
! colspan="3" | Basic Necessity : Compilation and Error Reporting
 
! colspan="3" | Basic Necessity : Compilation and Error Reporting
 
|-
 
|-
! scope="row" | Positive Compilation
+
! scope="row" | Positive Compilation 1 (Switch Statement with multi-label case with colon)
 
| Use the following code:
 
| Use the following code:
 
<source lang="java">
 
<source lang="java">
package pack1;
+
public class X {
public class VarLambda {
+
    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);
 +
    }
 +
}</source>
  
public static void main(String[] args) {
+
<br>
  I lam = (var  x) -> { // var compiles here
+
<br>
  System.out.println(x);
+
[[File:switch-statement-multi.png]]
  };
+
| Code compiles (with a preview warning) and while running prints both "Hello" "World"
  lam.apply(20);
+
|-
}  
+
! scope="row" | Positive Compilation 2 (Switch Statement with case with arrow)
}
+
| Use the following code:
 +
<source lang="java">
 +
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);
 +
    }
 +
}</source>
  
interface I {
+
<br>
public void apply(Integer k);
+
<br>
 +
[[File: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.
 +
|-
 +
! scope="row" | Positive Compilation (Switch Expression)
 +
| Use the following code:
 +
<source lang="java">
 +
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;
 +
}
 
}
 
}
 
</source>  
 
</source>  
Line 58: Line 102:
 
<br>
 
<br>
 
<br>
 
<br>
[[File:var11.compile.jpg]]
+
[[File:switch-exp.compile.png]]
 
| Code compiles
 
| Code compiles
 
|-
 
|-
! scope="row" | Compiler Error Case 1
+
! scope="row" | Compiler error Case
 
|  
 
|  
 
<source lang="java">
 
<source lang="java">
package pack1;
+
public class Test {
public class VarLambdaErr {
+
 
+
enum Day {
public static void main(String[] args) {
+
MON, TUE, WED, THUR, FRI, SAT, SUN
  I lam = (var  x, z) -> { // should not mix with type-elided params
+
};
  System.out.println(x);
+
  };
+
public String getDay_1 (Day today) {
  lam.apply(20);
+
String day = switch(today) {
}  
+
case MON, TUE, WED, THUR, FRI: break "Weekday";
}
+
case SAT, SUN: break "Weekend";
+
};
interface I {
+
return day;
public void apply(Integer k, Integer z);
+
}
 
}
 
}
 
</source>
 
</source>
| Compiler errors are shown
+
<br>
 +
<br>
 +
[[File:enable-preview.error.png]]
 +
| Compiler error is shown for preview feature.
 +
Quick fix is available to enable the preview feature in the preferences.
 
|-
 
|-
! scope="row" | Compiler Error Case 2
+
! scope="row" | Compiler Warning Case
 
|  
 
|  
 
<source lang="java">
 
<source lang="java">
package pack1;
+
public class Test {
public class VarLambdaErr {
+
 
+
enum Day {
public static void main(String[] args) {
+
MON, TUE, WED, THUR, FRI, SAT, SUN
  I lam = (var  x, Integer z) -> { // should not mix with non-var params
+
};
  System.out.println(x);
+
  };
+
public String getDay_1 (Day today) {
  lam.apply(20, 200);
+
String day = switch(today) {
}  
+
case MON, TUE, WED, THUR, FRI: break "Weekday";
}
+
case SAT, SUN: break "Weekend";
+
};
interface I {
+
return day;
public void apply(Integer k, Integer z);
+
}
 
}
 
}
 
</source>
 
</source>
|
 
Compiler errors shown
 
|-
 
! colspan="3" | Hover and Navigation
 
|-
 
! scope="row" | Hover
 
| Use the following code:
 
<source lang="java">
 
package pack1;
 
public class VarLambda {
 
 
public static void main(String[] args) {
 
  I lam = (var  x) -> { // hover over var
 
  System.out.println(x);
 
  };
 
  lam.apply(20);
 
}
 
}
 
 
interface I {
 
public void apply(Integer k);
 
}
 
</source>
 
 
 
<br>
 
<br>
 
<br>
 
<br>
[[File:var11.hover.jpg]]
+
[[File:switch.preview.warning.compile.png]]
| Hover and Navigation
+
| Compiler warning is shown for preview feature
 
|-
 
|-
! colspan="3" | Nestmates
+
! scope="row" | Compiler Error Case
|-
+
! scope="row" | Basic Nesting Principles
+
 
|  
 
|  
    <b>Description: Nest Based Access - A JVM Concept.</b>
 
    A top level class and all inner classes form a single unit for access, a "nest".JVM
 
    specification added two attributes in the classfile NestHost and NestMember. The top
 
    most class will have the NestMember attribute listing all the members while each of
 
    the inner classes will have a NestHost attribute listing the top level class. Using
 
    this, some of the synthetic bridge methods are elided transparent to the programmer.
 
    Note that this feature is relevant only for tools that process byte code and hence,
 
    in general, this feature would be "transparent" to a "normal" programmer. This feature
 
    is applicable for byte code processors, for eg our Disassembler has been enhanced to
 
    show these attributes. For further info please read JEP 181.
 
<br>
 
 
<source lang="java">
 
<source lang="java">
public class X {  
+
public class Test {
  private class A {
+
    class B {}
+
enum Day {
  }
+
MON, TUE, WED, THUR, FRI, SAT, SUN
  private class Y extends A {
+
};
  }
+
 +
@SuppressWarnings("preview")
 +
public String getDay_1 (Day today) {
 +
String day = switch(today) {
 +
case MON : break "Weekday";
 +
case SAT, SUN : break "Weekend";
 +
};
 +
return day;
 +
}
 
}
 
}
 
</source>
 
</source>
<br>
+
[[File:switch.cover-all-cases.error.png]]
| In X.class (Disassembled)
+
| Compiler error is shown
Nest Members:
+
|-
  #21 X$A,
+
  #24 X$A$B,
+
  #27 X$Y
+
 
+
In each of X$A, X$A$B, X$Y the attribute:
+
Nest Host: #22 X
+

Latest revision as of 00:23, 20 March 2019

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