- 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
Are Generics applied at compile time or run time?
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.
Example
class Student<T>{ T age; Student(T age){ this.age = age; } public void display() { System.out.println("Value of age: "+this.age); } } public class GenericsExample { public static void main(String args[]) { Student<Float> std1 = new Student<Float>(25.5f); std1.display(); Student<String> std2 = new Student<String>("25"); std2.display(); Student<Integer> std3 = new Student<Integer>(25); std3.display(); } }
Output
Value of age: 25.5 Value of age: 25 Value of age: 25
- Related Articles
- Explain Compile time and Run time initialization in C programming?
- What is the difference between compile time errors and run time errors in Java?
- What happens in Java Program at compile time?
- What are Java JVM Run-time Data Areas?
- How to check grant permission at run-time in android?
- How to request Location permission at run time in Android?
- C Program on two-dimensional array initialized at run time
- What is compile time polymorphism in C#?
- Is JIT compiler a part of JVM or run time interpreter?
- Difference between compile-time polymorphism and runtime polymorphism
- What is Java run time environment?
- Using run-time polymorphism in Java
- Run-time Stack mechanism in Java
- How to determine the size of an android view at run time?
- RTTI (Run-time type Information) in C++

Advertisements