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

 
(10 intermediate revisions by 2 users not shown)
Line 8: Line 8:
 
* Virtual Threads is a preview feature in Java 19. They are not enabled by default and can by enabled using <b>--enable-preview</b>.
 
* Virtual Threads is a preview feature in Java 19. They are not enabled by default and can by enabled using <b>--enable-preview</b>.
 
* Structured Concurrency is a <b>incubator features</b> in Java 19.
 
* Structured Concurrency is a <b>incubator features</b> in Java 19.
* 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 <b>Preferences > Java > Compiler</b>. It is implicitly added while launching a java program if the feature has been enabled for the project/workspace.
  
 
{| class="wikitable"
 
{| class="wikitable"
Line 26: Line 26:
 
! colspan="3" | Preview Feature: Pattern Matching for switch
 
! colspan="3" | Preview Feature: Pattern Matching for switch
 
|-
 
|-
! scope="row" | Postive compilation1 (Pattern Matching for switch Example)
+
! scope="row" | Positive compilation1 (Pattern Matching for switch Example)
 
| Use the following code:
 
| Use the following code:
 
<source lang="java">
 
<source lang="java">
 +
package pkg1;
 
public class X {
 
public class X {
 +
@SuppressWarnings("preview")
 
private static void foo(Object o) {
 
private static void foo(Object o) {
 
  switch (o) {
 
  switch (o) {
Line 37: Line 39:
 
  }
 
  }
 
}
 
}
 
+
 
public static void main(String[] args) {
 
public static void main(String[] args) {
 
foo("Hello World");
 
foo("Hello World");
Line 44: Line 46:
 
<br>
 
<br>
 
<br>
 
<br>
[[File:Pattern-matching-switch-positive1-Java17.png]]
+
[[File:Pattern-matching-switch-positive1-Java19.png]]
 
|Code compiles and prints below message:<br>
 
|Code compiles and prints below message:<br>
String: Hello World!
+
String: Hello World!<br>
 
|-
 
|-
 
|-
 
|-
Line 52: Line 54:
 
| Use the following code:
 
| Use the following code:
 
<source lang="java">
 
<source lang="java">
 +
package pkg1;
 
public class X {
 
public class X {
private static void foo(Object o) {
+
@SuppressWarnings("preview")
  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();
+
}
+
}</source>
+
<br>
+
<br>
+
[[File:Pattern-matching-switch-positive2-Java17.png]]
+
|Code compiles and prints below message:<br>
+
String > 1<br>
+
String<br>
+
Object
+
|-
+
|-
+
! scope="row" | Positive compilation3 (Pattern Matching for switch Example)
+
| Use the following code:
+
<source lang="java">
+
public class X {
+
 
private static void foo(Object o) {
 
private static void foo(Object o) {
 
  switch (o) {
 
  switch (o) {
Line 92: Line 63:
 
      System.out.println(I);  
 
      System.out.println(I);  
 
      break;  
 
      break;  
    case String s && s.length()>1:  
+
    case String s when s.length()>1:  
 
      System.out.println("String s && s.length()>1");  
 
      System.out.println("String s && s.length()>1");  
 
      System.out.println(s);  
 
      System.out.println(s);  
Line 107: Line 78:
 
  }
 
  }
 
}
 
}
 
 
public static void main(String[] args) {
 
public static void main(String[] args) {
 
foo("Hello World!");
 
foo("Hello World!");
 
foo("H");
 
foo("H");
foo(bar());
+
foo(new Object());
}
+
 
+
public static Object bar() {
+
return new Object();
+
 
}
 
}
 
}</source>  
 
}</source>  
 
<br>
 
<br>
 
<br>
 
<br>
[[File:Pattern-matching-switch-positive3-Java17.png]]
+
[[File:Pattern-matching-switch-positive2-Java19.png]]
 
|Code compiles and prints below message:<br>
 
|Code compiles and prints below message:<br>
 
String s && s.length()>1<br>
 
String s && s.length()>1<br>
Line 129: Line 95:
 
|-
 
|-
 
|-
 
|-
! scope="row" | Positive compilation4 (Pattern Matching for switch Example)
+
! scope="row" | Positive compilation3 (Pattern Matching for switch Example)
 
| Use the following code:
 
| Use the following code:
 
<source lang="java">
 
<source lang="java">
 +
package pkg1;
 
public class X {
 
public class X {
 
public static void main(String[] args) {
 
public static void main(String[] args) {
Line 137: Line 104:
 
foo(Integer.valueOf(9));
 
foo(Integer.valueOf(9));
 
}
 
}
 
+
 +
@SuppressWarnings("preview")
 
private static void foo(Object o) {
 
private static void foo(Object o) {
 
  switch (o) {
 
  switch (o) {
  case Integer i && i>10:
+
  case Integer i when i>10:
 
    System.out.println("Greater than 10:" + o);
 
    System.out.println("Greater than 10:" + o);
 
    break;
 
    break;
  case Integer j && j>0:
+
  case Integer j when j>0:
 
    System.out.println("Greater than 0:" + o);
 
    System.out.println("Greater than 0:" + o);
 
    break;
 
    break;
Line 153: Line 121:
 
<br>
 
<br>
 
<br>
 
<br>
[[File:Pattern-matching-switch-positive4-Java17.png]]
+
[[File:Pattern-matching-switch-positive3-Java19.png]]
 
|Code compiles and prints below message:<br>
 
|Code compiles and prints below message:<br>
Greater than 10:11
+
Greater than 10:11<br>
Greater than 0:9
+
Greater than 0:9<br>
 
|-
 
|-
 
|-
 
|-
! scope="row" | Positive compilation5 (Pattern Matching for switch Example)
+
! scope="row" | Positive compilation4 (Pattern Matching for switch Example)
 
| Use the following code:
 
| Use the following code:
 
<source lang="java">
 
<source lang="java">
 +
package pkg1;
 
public class X {
 
public class X {
 +
@SuppressWarnings("preview")
 
public static void foo(Object o) throws Exception {
 
public static void foo(Object o) throws Exception {
 
switch (o) {
 
switch (o) {
Line 171: Line 141:
 
}
 
}
 
}
 
}
 
+
 
public static void main(String[] args) throws Exception {
 
public static void main(String[] args) throws Exception {
 
foo("hello");
 
foo("hello");
Line 178: Line 148:
 
<br>
 
<br>
 
<br>
 
<br>
[[File:Pattern-matching-switch-positive5-Java17.png]]
+
[[File:Pattern-matching-switch-positive4-Java19.png]]
 
|Code compiles and prints below exception stack trace message:<br>
 
|Code compiles and prints below exception stack trace message:<br>
 
Exception in thread "main" java.lang.Exception: hello<br>
 
Exception in thread "main" java.lang.Exception: hello<br>
at X.foo(X.java:5)<br>
+
at BetaJava19/pkg1.X.foo(X.java:7)<br>
at X.main(X.java:12)<br>
+
at BetaJava19/pkg1.X.main(X.java:14)<br>
 
|-
 
|-
 
|-
 
|-
Line 191: Line 161:
 
<source lang="java">
 
<source lang="java">
 
package pkg1;
 
package pkg1;
 
+
 
record Point(int x, int y) {}
 
record Point(int x, int y) {}
 
record Length(int l) {}
 
record Length(int l) {}
 
+
 
public class A {
 
public class A {
 +
@SuppressWarnings("preview")
 
static void printObject(Object obj) {
 
static void printObject(Object obj) {
if (obj instanceof Point point) {
+
if (obj instanceof Point(int x, int y)) {
System.out.println("Point.x= " + point.x() + "\nPoint.y= " + point.y());
+
System.out.println("Point.x= " + x + "\nPoint.y= " + y);
} else if (obj instanceof Length length) {
+
} else if (obj instanceof Length(int l)) {
System.out.println("Lenght.l= " + length.l());
+
System.out.println("Lenght.l= " + l);
 
} else {
 
} else {
 
System.out.println(obj);
 
System.out.println(obj);
 
}
 
}
 
}
 
}
 
+
 
public static void main(String[] args) {
 
public static void main(String[] args) {
 
printObject(new Point(10, 20));
 
printObject(new Point(10, 20));
Line 214: Line 185:
 
<br>
 
<br>
 
<br>
 
<br>
[[File:Positive_case1_RecordPatters.png]]
+
[[File:Positive_case1_RecordPatters_Java19.png]]
 
|Code compiles and prints below message:<br>
 
|Code compiles and prints below message:<br>
 
Point.x= 10<br>
 
Point.x= 10<br>

Latest revision as of 03:14, 22 October 2022

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

  • Pattern Matching for switch is a preview feature in Java 19. They are not enabled by default and can by enabled using --enable-preview.
  • Record Patterns is a preview feature in Java 19. They are not enabled by default and can by enabled using --enable-preview.
  • Virtual Threads is a preview feature in Java 19. They are not enabled by default and can by enabled using --enable-preview.
  • Structured Concurrency is a incubator features in Java 19.
  • In Eclipse, --enable-preview can be enabled from the Preferences > Java > Compiler. 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
Positive compilation1 (Pattern Matching for switch Example) Use the following code:
package pkg1;
public class X {
	@SuppressWarnings("preview")
	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-Java19.png

Code compiles and prints below message:

String: Hello World!

Positive compilation2 (Pattern Matching for switch Example) Use the following code:
package pkg1;
public class X {
	@SuppressWarnings("preview")
	private static void foo(Object o) {
	   switch (o) {
	     case Integer I: 
	       System.out.println("Integer"); 
	       System.out.println(I); 
	       break; 
	     case String s when 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(new Object());
	}
}



Pattern-matching-switch-positive2-Java19.png

Code compiles and prints below message:

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

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



Pattern-matching-switch-positive3-Java19.png

Code compiles and prints below message:

Greater than 10:11
Greater than 0:9

Positive compilation4 (Pattern Matching for switch Example) Use the following code:
package pkg1;
public class X {
	@SuppressWarnings("preview")
	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-positive4-Java19.png

Code compiles and prints below exception stack trace message:

Exception in thread "main" java.lang.Exception: hello
at BetaJava19/pkg1.X.foo(X.java:7)
at BetaJava19/pkg1.X.main(X.java:14)

Preview Feature: Record Patterns
Positive compilation1 (Record Patterns Example) Use the following code:
package pkg1;
 
record Point(int x, int y) {}
record Length(int l) {}
 
public class A {
	@SuppressWarnings("preview")
	static void printObject(Object obj) {
		if (obj instanceof Point(int x, int y)) {
			System.out.println("Point.x= " + x + "\nPoint.y= " + y);
		} else if (obj instanceof Length(int l)) {
			System.out.println("Lenght.l= " + l);
		} else {
			System.out.println(obj);
		}
	}
 
	public static void main(String[] args) {
		printObject(new Point(10, 20));
		printObject(new Length(100));
	}
}



Positive case1 RecordPatters Java19.png

Code compiles and prints below message:

Point.x= 10
Point.y= 20
Lenght.l= 100

Preview Feature: Virtual Threads
Positive compilation1 (Virtual Threads Example) Use the following code:
package pkg1;
public class B {
	static Thread currentThread;
	@SuppressWarnings("preview")
	public static void main(String[] args) {
		Runnable runnable = () -> {
			System.out.println("---Start-Run---");
			if (currentThread.isVirtual()) {
				System.out.println("Virtual Thread:\n\tJava19 feature running with VM arguments:\n\t--enable-preview");
			} else {
				System.out.println("Standard Platform Thread:");
			}
			System.out.println("isVirtual: " + currentThread.isDaemon() + "\nisDaemon : " + currentThread.isDaemon());
			System.out.println("---End---Run---");
		};
		currentThread = new Thread(runnable); // Normal thread is non-daemon by default
		currentThread.start();
		currentThread = Thread.startVirtualThread(runnable); // Virtual thread is by default daemon
		try {
			Thread.sleep(100); // Sleep for 100 ms
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}



Positive Test case1 Virtual thread.png

Code compiles and prints below message:

---Start-Run---
Standard Platform Thread:
isVirtual: false
isDaemon : false
---End---Run---
---Start-Run---
Virtual Thread:
Java19 feature running with VM arguments:
--enable-preview
isVirtual: true
isDaemon : true
---End---Run---

Positive compilation2 (Virtual Threads Example) Use the following code:
package pkg1;
public class C {
	static Thread currentThread;
	@SuppressWarnings("preview")
	public static void main(String[] args) {
		Runnable runnable = () -> {
			System.out.println("---Start-Run---");
			if (currentThread.isVirtual()) {
				System.out.println("Virtual Thread:\n\tJava19 feature running with VM arguments:\n\t--enable-preview");
			} else {
				System.out.println("Standard Platform Thread:");
			}
			System.out.println("isVirtual: " + currentThread.isDaemon() + "\nisDaemon : " + currentThread.isDaemon());
			System.out.println("---End---Run---");
		};
		currentThread = new Thread(runnable); // Normal thread is non-daemon by default
		currentThread.start();
		Thread.Builder threadBuilder = Thread.ofVirtual().name("Virtual-Thread", 1);
		currentThread = threadBuilder.start(runnable); // Virtual thread is by default daemon
		try {
			Thread.sleep(100); // Sleep for 100 ms
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}



Positive Test case2 Virtual thread.png

Code compiles and prints below message:

---Start-Run---
Standard Platform Thread:
isVirtual: false
isDaemon : false
---End---Run---
---Start-Run---
Virtual Thread:
Java19 feature running with VM arguments:
--enable-preview
isVirtual: true
isDaemon : true
---End---Run---

Positive compilation3 (Virtual Threads Example) Use the following code:
package pkg1;
 
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
 
public class D {
	static ExecutorService service;
 
	@SuppressWarnings("preview")
	public static void main(String[] args) {
		Runnable runnable = () -> {
			System.out.println("---Start-Run---");
			System.out.println(
					"VirtualThreadPerTaskExecutor:\n\tJava19 feature running with VM arguments:\n\t--enable-preview");
			System.out.println("---End---Run---");
		};
		service = Executors.newVirtualThreadPerTaskExecutor();
		service.execute(runnable);
		try {
			Thread.sleep(100); // Sleep for 100 ms
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}



Positive Test case3 Virtual thread.png

Code compiles and prints below message:

---Start-Run---
VirtualThreadPerTaskExecutor:
Java19 feature running with VM arguments:
--enable-preview
---End---Run---

Back to the top