- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What are raw types in generics in Java?
Generics is a concept in Java where you can enable a class, interface and, method, accept all (reference) types as parameters. In other words it is the concept which enables the users to choose the reference type that a method, constructor of a class accepts, dynamically. By defining a class as generic you are making it type-safe i.e. it can act up on any datatype.
To define a generic class you need to specify the type parameter you are using in the angle brackets “<>” after the class name and you can treat this as datatype of the instance variable an proceed with the code.
Example − Generic class
class Person<T>{ T age; Person(T age){ this.age = age; } public void display() { System.out.println("Value of age: "+this.age); } }
Usage − While instantiating the generic class you need to specify the object name after the class within the angular brackets. Thus, choose the type of the type parameter dynamically and pass the required object as a parameter.
public class GenericClassExample { public static void main(String args[]) { Person<Float> std1 = new Person<Float>(25.5f); std1.display(); Person<String> std2 = new Person<String>("25"); std2.display(); Person<Integer> std3 = new Person<Integer>(25); std3.display(); } }
Raw types
While creating an object of the generic class or interface if you do not mention the type arguments they are known as raw types.
For example, if you observe the above example while instantiating the Person class you need to specify type for the type parameter (type of a class) in angle brackets.
If you avoid specifying any type parameters within angular brackets and create an object of a generic class or, interface they bare known as raw types.
public class GenericClassExample { public static void main(String args[]) { Person per = new Person("1254"); std1.display(); } }
Warning
These will compile without error but generates a warning as shown below.
Note: GenericClassExample.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details
Following are some notable points about raw types −
- You can assign a parameterized (generic) type to its raw type.
public class GenericClassExample { public static void main(String args[]) { Person per = new Person(new Object()); per = new Person<String>("25"); per.display(); } }
Output
Value of age: 25
- A warning will be generated if you assign a raw type to a parameterized type.
public class GenericClassExample { public static void main(String args[]) { Person<String> obj = new Person<String>(""); obj = new Person(new Object()); obj.display(); } }
Warning
Note: GenericClassExample.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details.
If your generic class contains a generic method and if you try to invoke it using a raw type a warning will be generated at the compile time.
public class GenericClassExample { public static void main(String args[]) { Student std = new Student("Raju"); System.out.println(std.getValue()); } }
Warning
Note: GenericClassExample.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details.
- Related Articles
- Bounded-types in generics in Java?
- Bounded types with generics in Java\n
- What are wildcards arguments in Generics In Java?
- What are Unbounded wildcard w.r.t Generics method in Java?
- What are upper-bounded wildcard w.r.t Generics method in Java?
- What are lower-bounded wildcard w.r.t Generics method in Java?
- Generics in Java
- What is Java Generics?
- What are covariant return types in Java?
- What are primitive data types in Java?
- What are reference data types in Java?
- Templates in C++ vs Generics in Java
- What are the types of arrays in Java?
- Why do we need generics in Java?
- Generics vs non-generics in C#
