×

The Diamond Operator in Java 7

The Diamond operator is a comparatively recent Java operator originally developed in JDK 7 to enhance type identification and eliminate boilerplate Java code. The Diamond operator is characterized by a closed angle bracket resembling a diamond's form (>). It may eliminate type and repetitive writing in Java when utilized appropriately, resulting in considerably simpler and understandable code, particularly when Generics are employed.

We frequently use the Diamond operator in Java when establishing and initializing structures containing several Generic types, such as a Map that contains another Map; as a result of enhanced category deduction, we need to specify the input text on the left flank of the statement.

Before Java 6, generic type inferences in Java were weak. The reasons must be stated on both the local variable's right and left sides. This makes code too complex and difficult to comprehend, particularly when hierarchical types are used, such as Map with Entry, where the key is String, and the value is a List of Integer numbers.

// the syntax of the diamond operator 
// it is used on both sides of the class while creating an instance 
List<String> list = new ArrayList<String>();

Since the introduction of the Diamond operator in Java 7, we may construct objects without specifying a generic type upon the right side of the statement, like :

List<String> lists = new ArrayList<>();

Problems due to the diamond operator are:

We may generate an object using the Diamond operator before stating the generic type here on the right side of the statement. However, it's only going to function with regular courses. If you try to execute the diamond operators on an unnamed inner class, the compiler will produce the following error.

Example: DiamondProgram.java

//this program is for the diamond operator in java
//importing the packages
import java.io.*;
import java.util.*;
abstract class Diamond<Temp> {
abstract Temp addition(Temp num1, Temp num2);
}


public class DiamondProgram {
public static void main(String[] args)
{
Diamond<Integer> object = new Diamond<>() {
Integer addition(Integer num1, Integer num2)
{
return (num1 + num2);
}
};
Integer res = object.addition(1001, 2002);
System.out.println("The sum of the 2 numbers is: " + res);
}
}

By enabling the diamond operator's usage with anonymous inner classes, Java programmers expanded the diamond operator's functionality in JDK 9. With JDK 9, the previous code will function as intended and provide the output below.

Output

The sum of the 2 numbers is: 3003

Related Topics

How to Run Applet Program in Java

An applet is a new type of program which is put in a webpage. By inserting applet into a webpage dynamic content can be created. Characteristics of an Applet The applet is...

11 minutes read.

Java String endsWith() method

Checks whether this String ends with the specified suffix or not. Syntax public boolean endsWith(String Suffix) Parameter Suffix : It takes suffix as a parameter  i.e. Sequence of characters Returns It returns true when the current...

1 minute read.

Duodecimal in Java

Duodecimal is a notation style in which a number with a base of 12 is referred to be a duodecimal number. In Java, we can use to convert duodecimal integers...

2 minutes read.

Java Stringjoiner Class

StringJoiner is a class which is used to construct a sequence of characters which are separated by a delimiter. Optionally, it starts with a provided prefix and ended with the...

5 minutes read.

Java Coding Software

Desktop and web apps are created using Java, an object-oriented programming language. Java code may be executed on any platform, making it platform-independent. A text editor, tool, or piece of...

9 minutes read.

Java Switch Keyword

In this article we are going to learn the concept of a java switch keyword. Generally, java case keyword is used with the switch statements or keyword.Switch keyword is implemented in...

3 minutes read.

Java float vs double

Java : Java is a pure object oriented language. It was introduced by James Gosling in the year 1995. The first public implementation of java was done by sun micro systems...

7 minutes read.

How to Convert Binary to Decimal in Java

How to Convert Binary to Decimal in Java There are two methods to convert binary to decimal. Using Integer.parseInt() method Using user-defined logic Using Integer.parseInt() method The parseInt() is a static method of...

2 minutes read.

Constructor Chaining and Constructor Overloading in Java

Constructor Chaining Constructor chaining and constructor overloading are two confusing terms. Let's first understand constructor chaining.Constructor chaining is the process of calling one constructor from another constructor using the same object....

2 minutes read.

Java Substring

What is a substring in Java? Here by the name  " substring " itself, we can easily come to know it is a part of a string or a subset of...

4 minutes read.

Java String equals() method

equals() method compares two string based on the their content. Syntax public boolean equals(Objects anObject) parameter anObject: Object to be compared with the current String. Returns It returns true when the current String is equivalent to...

1 minute read.

Packages in Java

Packages in Java can be defined as an assortment for grouping various classes and interfaces based on their performance. It is a catalog for holding various java files. They provide...

4 minutes read.

Hogben Numbers in Java

In this section, we will discover what the Hogben number is and develop Java programs that compute it. Java coding interviews and academic exams typically involve questions about the Hogben...

3 minutes read.

Conditional operator in Java

In Java, there are around eight operators, and among them, three operators are used to evaluate the condition and decide the Result based on the Result of the evaluated condition. Below...

4 minutes read.

Composition in Java

Composition Java uses the composition method to implement a has-a connection. Composition allows us to reuse code in the same way that Java inheritance does. The "is-a" relationship is implemented using the...

3 minutes read.

Get year from date in Java

The getYear() method of the Java date class returns a number calculated by deducting 1900 from the year that contains or starts with the instant in time represented by this...

4 minutes read.

Tetris Game in Java

The Tetris game is among the most well-known video games ever produced for computers. Today, we may engage in this game on a mobile device as well. Alexey Pajitnov conceptualized...

12 minutes read.

Java String

Definition A string is a series of characters. Consider an example, "Welcome" is a 7-character string. In Java, String is an unchangeable object. That is, the String is perpetual and cannot be replaced after it is created. This is the tutorial in which you...

4 minutes read.

Java Type Casting

Type casting is a technique or process used in Java to convert one data type into another, either manually or automatically. The compiler performs the automatic conversion, and the programmer...

3 minutes read.

Java Keywords

Java Keywords The particular words which are used in java programming language that act like a key or important words to write a code are called java keywords. Java Keywords are...

4 minutes read.