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'.
// 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()); } } |
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'.
// 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); } } |
java.lang.Integer = 11 java.lang.String = GeeksForGeeks java.lang.Double = 1.0
Recommended Posts:
- Java.util.LinkedList.poll(), pollFirst(), pollLast() with examples in Java
- Java.util.concurrent.RecursiveAction class in Java with Examples
- Java lang.Long.lowestOneBit() method in Java with Examples
- Java lang.Long.reverse() method in Java with Examples
- Java.util.Collections.disjoint() Method in java with Examples
- Java.lang.Short toString() method in Java with Examples
- Java.util.concurrent.Phaser class in Java with Examples
- Java.util.BitSet class methods in Java with Examples | Set 2
- Java lang.Long.numberOfLeadingZeros() method in Java with Examples
- Java.util.concurrent.RecursiveTask class in Java with Examples
- Java lang.Long.highestOneBit() method in Java with Examples
- Java lang.Long.byteValue() method in Java with Examples
- Java.util.function.LongPredicate interface in Java with Examples
- Java.util.function.DoublePredicate interface in Java with Examples
- Java.util.function.BiPredicate interface in Java with Examples
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.



