The intersection type introduced in Java 10 is amazing (?)

In Java 10, it is possible to declare variables by type inference called var. For example var url = new URL(""); Then, url is type inferred and becomes a URL type variable.

Up to this point, the story ends with "Hey. It's syntactic sugar that can omit the description of variables." However, in Java 10 type inference, "inexpressible types can also be inferred." I will.

.. .. .. I don't know what you're talking about, but I don't know what you're talking about either.

Here is the sample code.

public class Sample{
  public static void main(String[] args) {
    var ins = (Foo & Bar) ()->{};;;
    ins.forFunctionalInterfaceMethod(); //☓ You can call it without implementing it → Corrected by receiving comments. 3 lines above()->{}Is implemented.
    ins.toString(); //It is also an Object type.

  interface Base {
    void forFunctionalInterfaceMethod();

  interface Foo extends Base {
    default void foo() {

  interface Bar extends Base {
    default void bar() {

If you compile and run this in Java 10, you will get the following output.

$ javac  && java Sample

In other words var ins = (Foo & Bar) ()->{}; Ins is a Foo type, a Bar type, a Base type that is the parent of the interface, and an Object type. This is the intersection type, which is an "inexpressible type". Now you can do something like a dynamic mixin.

.. .. .. In what cases can it be used conveniently? .. .. If you have any insights, please comment.

Recommended Posts

The intersection type introduced in Java 10 is amazing (?)
What is the main method in Java?
Type determination in Java
Organized memo in the head (Java --Data type)
[Java] Something is displayed as "-0.0" in the output
Which is better, Kotlin or Java in the future?
The story that .java is also built in Unity 2018
Try functional type in Java! ①
Java is the 5th day
[Java] "T" is included in date type JSON in API response
Access the network interface in Java
Guess the character code in Java
Specify the java location in eclipse.ini
Where is the Java () timezone?
Unzip the zip file in Java
Parsing the COTOHA API in Java
Is there no type in Ruby?
Call the super method in Java
This is the Excel output in Java! "JXLS" official document translation
The milliseconds to set in /lib/ of Java jre is UTC
[Android / Java] Understand the description type in listener settings (button installation)
How to write the view when Vue is introduced in Rails?
What is a class in Java language (3 /?)
Get the result of POST in Java
Regarding String type equivalence comparison in Java
Spring Autowired is written in the constructor
[Easy-to-understand explanation! ] Reference type type conversion in Java
[Java] Display the bit string stored in the byte type variable on the console
Java reference to understand in the figure
What is the right migration data type? ??
Try using the Stream API in Java
Call the Windows Notification API in Java
[Personal memo] Java data type is annoying
I tried the new era in Java
[Java] Introducing the Generics Boundary Wildcard Type
Memo: [Java] If a file is in the monitored directory, process it.
[Java] Use cryptography in the standard library
Organized memo in the head (Java --Array)
What is the best file reading (Java)
What is a class in Java language (1 /?)
Try calling the CORBA service in Java 11+
[Java] Write the programming quiz SET INTERSECTION in one line with StreamAPI
What is a class in Java language (2 /?)
The Java EE Security API is here!
Reason to add L to the number to be put in Java long type
How to get the date in java
The story of writing Java in Emacs
Console input in Java (understanding the mechanism)
What is CHECKSTYLE: OFF found in the Java source? Checkstyle to know from
[Java] Difference between equals and == in a character string that is a reference type
[Java] Where is the implementation class of annotation that exists in Bean Validation?
About the meaning of type variables, E, T, etc. used in generics used in Java
[Java] Is it unnecessary to check "identity" in the implementation of the equals () method?
What is ... (3 dots) found in the Java source? Variadic arguments to know from
What is Pullback doing in The Composable Architecture
Regarding the transient modifier and serialization in Java
[Java] Handling of JavaBeans in the method chain
About the confusion seen in startup Java servers
The story of making ordinary Othello in Java
About the idea of anonymous classes in Java
A story about the JDK in the Java 11 era