Angle Bracket <> in Java with Examples

Angle Bracket in Java is used to define Generics. It means that the angle bracket takes a generic type, say T, in the definition and any class as a parameter during the calling. The idea is to allow type (Integer, String, … etc and user-defined types) to be a parameter to methods, classes, and interfaces. For example, classes like HashSet, ArrayList, HashMap, etc use generics very well. We can use them for any type.

Example:

<T> // of type T
<Integer> // of type Integer
<String> // of type String
<MyClass> // of type MyClass
.
.

How to use angle bracket with Class?:



We use angle brackets ‘<>’ to specify parameter types in generic class creation. To create objects of a generic class, we use the following syntax:

// To create an instance of generic class 
BaseType  obj = new BaseType ()

Note: In Parameter type,
      we can not use primitives like 
      'int', 'char' or 'double'.
filter_none

edit
close

play_arrow

link
brightness_4
code

// A Simple Java program
// to show working of user defined
// Generic classes
  
// We use < > to specify Parameter type
class Test<T> {
  
    // An object of type T is declared
    T obj;
  
    // constructor
    Test(T obj)
    {
        this.obj = obj;
    }
  
    public T getObject()
    {
        return this.obj;
    }
}
  
// Driver class to test above
class Main {
    public static void main(String[] args)
    {
        // instance of Integer type
        Test<Integer> iObj
            = new Test<Integer>(15);
        System.out.println(iObj.getObject());
  
        // instance of String type
        Test<String> sObj
            = new Test<String>("GeeksForGeeks");
        System.out.println(sObj.getObject());
    }
}

chevron_right


Output:

15
GeeksForGeeks

How to use angle bracket with Function?:

We use angle brackets ” to specify parameter types in the generic function definition. Then to call the function, we just pass the expecter type as a parameter. We can also write generic functions that can be called with different types of arguments based on the type of arguments passed to the generic method, the compiler handles each method.

// To create a generic function
public static  void func(T a, T b){}

Note: In Parameter type,
      we can not use primitives like 
      'int', 'char' or 'double'.
filter_none

edit
close

play_arrow

link
brightness_4
code

// A Simple Java program
// to show working of user defined
// Generic functions
  
class Test {
    // A Generic method example
    static <T> void genericDisplay(T element)
    {
        System.out.println(
            element
                .getClass()
                .getName()
            + " = " + element);
    }
  
    // Driver method
    public static void main(String[] args)
    {
        // Calling generic method
        // with Integer argument
        genericDisplay(11);
  
        // Calling generic method
        // with String argument
        genericDisplay("GeeksForGeeks");
  
        // Calling generic method
        // with double argument
        genericDisplay(1.0);
    }
}

chevron_right


Output:

java.lang.Integer = 11
java.lang.String = GeeksForGeeks
java.lang.Double = 1.0


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.