Topics Index


If you need explanation Read this topic

If you need Answer Take test on this topic

What will be the output of the following program?
public class GenericsWithObjectsDemo {
    public static void main(String args[])
        GenericsWithObjects<int> integerObject = new GenericsWithObjects<int>(12);
class GenericsWithObjects<T>
    T obj;
    GenericsWithObjects(T obj)
        this.obj = obj;
    void print()

A. 12
B. Some other output
C. Compilation Error
D. Runtime Error
Topic: Java Generics

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

Answer is C......because In the main program we are trying to create an object of a generic class GenericWithObjects by passing the primtive type int as parameter...But java generics dont support the primitive types as generic type argument because Generics in Java are an entirely compile-time construct - the compiler turns all generic uses into casts to the right type. This is to maintain backwards compatibility with previous JVM runtimes..


List<ClassA> list = new ArrayList<ClassA>();
list.add(new ClassA());
ClassA a = list.get(0);
gets turned into (roughly):

List list = new ArrayList();
list.add(new ClassA());
ClassA a = (ClassA)list.get(0);

So, anything that is used as generics has to be convertable to Object (in this example get(0) returns an Object), and the primitive types aren't. So they can't be used in generics.

Posted by Shashanka Mogaliraju    2014-11-12 08:35:09

Ans is we have generics Concept which help ful to reduce the code...using this we can write single which will work for all type of Objects...and this also Type safe..

in Generics Evrything in the form of objects we have main class we are creating the Object for this class
Generics Totally based on the Compile time it will create Possible methods lik
fun(int x),fun(float x) etc...
here we are creating instance by passing the float(Primitive) here no autoboxing wil peroform..

when we are working with the Generics we must create Objects By passing the Objects Compiler will raise an error Saying Required Object..

Tak an Example When we are working with Collections we always use the Objects...if we want use int than we will create Integer...

if we change int to Integer  than it wil work Integer is An Wrapper class..

Posted by Uday Kumar    2014-11-12 12:20:31

ans c.Compilation error.,in this program we have 2 classes,main class and one generic class.generics, which operates only on objects.With generics, you can define an algorithm once, independently of any specific
type of data, and then apply that algorithm to a wide variety of data types without any additional
effort.parameterized type class is called generic class. Here, T is the name of a type parameter. This name is used as a placeholder for the actual
type that will be passed to GenericTypesdiffDemo when an object is created. in this program we are trying to create generic class with primitive int value. It is important to understand that Java has always given you the ability to create generalized
classes, interfaces, and methods by operating through references of type Object. Because Object
is the superclass of all other classes, an Object reference can refer to any type object. When declaring an instance of a generic type, the type argument passed to the type parameter
must be a class type. You cannot use a primitive type, such as int or is possible to pass any class type to T, but you cannot pass a primitive type to a type integerobject declaration is illegal in this program.

Posted by Maheshwari Natarajan    2014-11-12 17:22:32

This dose is now closed and the winners are Shashanka Mogaliraju,  for 'First Correct Comment', Uday Kumar, Maheshwari Natarajan,  for 'Best Comment' and Sai Ram for the 'Popular Comment'. The 'lucky liker' is Varun Kumar. Please login into Merit Campus using facebook, to claim your recharge. Go to to raise the recharge.

Posted by Merit Campus    2014-11-14 02:15:07

© meritcampus 2019

All Rights Reserved.

Open In App