Menu
Topics Index
...
`

Generics2793


If you need explanation Read this topic

If you need Answer Take test on this topic

Generics2793
What will be the output of the following program?
public class GenericsWithObjectsDemo {
    public static void main(String args[])
    {
        GenericsWithObjects<double> doubleObject = new GenericsWithObjects<double>(12.0);
        doubleObject.print();
    }
}
class GenericsWithObjects<T>
{
    T obj;
    GenericsWithObjects(T obj)
    {
        this.obj = obj;
    }
    void print()
    {
        System.out.println(obj);
    }
}

A. 12.0
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.

ans is C as  GenericsWithObjects<double> doubleObject = new GenericsWithObjects<double>(12.0);
                            ^
  required: reference
  found:    double  and the other error isGenericsWithObjects<double> doubleObject = new GenericsWithObjects<double>(12.0);
                                                                           ^
  required: reference
  found:    double

Posted by Ramya Reddy Addulla    2014-11-11 03:42:16


ans is c because error: unexpected type GenericsWithObjects<double>            required: reference   found:    double

Posted by Shubham Bansal    2014-11-11 03:42:26


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 double 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..                                                

This:

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-11 04:17:01


Ans is C...here 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 only...here we have GenericsWithObjects..in main class we are creating the Object for this class

here we are creating instance by passing the double(Primitive) here no autoboxing wil peroform..

when we are working with the Generics we must create Objects By passing the Objects Only...here Compiker 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 double to Double than it wil work fine

Posted by Uday Kumar    2014-11-11 16:47:22


This dose is now closed and the winners are Ramya Reddy Addulla,  for 'First Correct Comment', Shashanka Mogaliraju, Uday Kumar,  for 'Best Comment' and Sai Ram for the 'Popular Comment'. The 'lucky liker' is Gaurav Bansal. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2014-11-12 03:38:47


© meritcampus 2019

All Rights Reserved.

Open In App