The Angle Bracket in Java

Sheeraz Gul Oct 12, 2023
  1. Angle Bracket(<>) in Java
  2. Usage of Angle Bracket(<>) in Built-In Data Types in Java
The Angle Bracket in Java

The Angle Bracket is used to define the Generics in Java. Generic is a way to parameterize a class, method, or interface.

For example, the types like Arraylist, HashMap and Hashset use generics. Generics can also be used for any type.

This tutorial demonstrates what is an Angle Bracket(<>) in Java and how to use it.

Angle Bracket(<>) in Java

Let’s have an example; we have a class named Delftstack which will accept unspecified object types. Then there is a field EmployeeSalary, which will also accept any object type.

Finally, we declare a parameterized constructor to print the employee salary. That is on the user to decide if the salary is an integer, float, double or string etc.

Code:

package delftstack;

public class Delftstack<T> {
  T EmployeeSalary;

  public Delftstack(T EmployeeSalary) {
    this.EmployeeSalary = EmployeeSalary;
  }

  public void print() {
    System.out.println("The Employee Salary is: " + this.EmployeeSalary);
  }
  public static void main(String[] args) {
    int EmployeeSalary = 1000;
    Delftstack Demo = new Delftstack(EmployeeSalary);
    Demo.print();
  }
}

The code above uses the <T> with the class name. We can use anything instead of T here.

Output:

The Employee Salary is: 1000

As mentioned, <T> is used for any type. There are also some other generic terms for a specific type, and these terms are described in the following.

  1. <T> is referred to as any type.
  2. <E> is referred to as element type.
  3. <N> is referred to as number type.
  4. <V> as referred to as value.
  5. <K> as referred to as a key.

A few important benefits of using generics <> in Java are stated below.

  • Reusability: We can use the generic method, class of interface multiple times because we apply the object type based on the task we are trying to achieve.
  • Better output: If we use an Object type that is different from the one we specified, the compiler will tell us on time.
  • Great with Data Structures: The generics work great with data structures, for example, the Arraylist or Hashmap.

Usage of Angle Bracket(<>) in Built-In Data Types in Java

As mentioned above <> is also used in built-in classes like ArrayList, HashMap, and HashSet. Let’s try an example with these three data types.

Code:

package delftstack;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;

public class Delftstack<T> {
  public static void main(String[] args) {
    ArrayList<String> Demo_List = new ArrayList<String>();
    Demo_List.add("Delftstack1");
    Demo_List.add("Delftstack2");
    Demo_List.add("Delftstack3");
    System.out.println(Demo_List);

    HashMap<String, Integer> Demo_Map = new HashMap<String, Integer>();
    Demo_Map.put("Jack", 1000);
    Demo_Map.put("Mike", 1500);
    Demo_Map.put("Michelle", 1800);
    System.out.println(Demo_Map);

    HashSet<String> Demo_Set = new HashSet<String>();
    Demo_Set.add("Delftstack1");
    Demo_Set.add("Delftstack2");
    Demo_Set.add("Delftstack3");
    System.out.println(Demo_Set);
  }
}

The Angle Bracket(<>) defines data types for ArrayList, HashMap, and HashSet.

Output:

[Delftstack1, Delftstack2, Delftstack3]
{Michelle=1800, Mike=1500, Jack=1000}
[Delftstack1, Delftstack2, Delftstack3]
Author: Sheeraz Gul
Sheeraz Gul avatar Sheeraz Gul avatar

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.

LinkedIn Facebook

Related Article - Java Generics