Menu
Topics Index
...
`

Generics2779


If you need explanation Read this topic

If you need Answer Take test on this topic

Generics2779
What will be the output of the following program?
public class GenericTypesDifferDemo {
    public static void main(String args[])
    {
        GenericsWithObjects<Integer> integerObject = new GenericsWithObjects<Integer>(12);
        GenericsWithObjects<String> stringObject = new GenericsWithObjects<String>("MeritCampus");
        integerObject = stringObject; // LINE A
        integerObject.print(); // LINE B
    }
}
class GenericsWithObjects<T>
{
    T obj;
    GenericsWithObjects(T obj)
    {
        this.obj = obj;
    }
    void print()
    {
        System.out.println(obj);
    }
}

A. 12
B. MeritCampus
C. Compilation Error at LINE A
D. Compilation Error at LINE B
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...here we have generics class which can receive any type of the Objects.....here we create Instance for that class with Integer Object...by passing the int value...here AutoBoxing will perform..primitive (int) will converted into Integer....

next we are creating another object with String by passing an String literal

here both these are different Objects...one is Integer and Another is String....
in next line we are assigning StringObj to IntObj so that compiler will raise an error saying Incompatble types...

if we create second object noramlly without specifying any type than we can assign lik this...

Ex:GenericsWithObjects stringObject = new GenericsWithObjects("MeritCampus");

if we create lik above than we can assign lik this

Posted by Uday Kumar    2014-11-12 12:48:30


ans c.Unresolved compilation problem                               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.                                                                                 refernce for the integer obj created.makes use of autoboxing to encapsulate the value 12, which is an int, into an Integer.simiarly refernce for the string obj created .                                                                      LINE A--integer object reference is assigned with Stringobject .                                                            LINE A throws compilation error. because the type of the object to which the reference is
being assigned is of type GenericTypeDiffDemo<Integer>.,but here we are trying to assign stringobject.Akey point to understand about generic types is that a reference of one specific type of a
generic type is not type compatible with another type of the same generic type.

Posted by Maheshwari Natarajan    2014-11-12 16:53:27


This dose is now closed and the winners are Uday Kumar,  for 'First Correct Comment', Uday Kumar, Maheshwari Natarajan,  for 'Best Comment' and Uday Kumar for the 'Popular Comment'. The 'lucky liker' is Sai Ram. 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-14 02:29:23


© meritcampus 2019

All Rights Reserved.

Open In App