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 doubleObject = new GenericsWithObjects(12.0);
        GenericsWithObjects stringObject = new GenericsWithObjects("MC");
class GenericsWithObjects {
    Object obj;
    GenericsWithObjects(Object obj) {
        this.obj = obj;
    void print() {

A. 12.0
B. 12.0
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 A,, Object can hold both double and string type valuess

Posted by Raju VarshnEy    2014-11-12 12:15:01

Ans is A..We can write an Method r Constructor which can receive any type of data without using the Generics by using the Object class which is Parent class for all classes in Java..Some times we need explict conversion when we use this mechanisam...

Coming to the Program here we Have class GenericsWithObjects....this class have constructor which can recevive any type of the Constructor w wrote reference of that it can receive any Class Object...Bcz it is the  parent class for the all classes in the java...
So that parent class reference can refer Child class Object

Coming to the main class we create instance for the GenericsWithObjects class by Passing the 12.0 (Which is treated as double by default) that here autoboxing will perform and double will conveted as the  Double...Autoboxing means converting primitive into Object....

Next we are calling the print method with the created object than it wil print the 12.0

next we create another instance by passing the "String" here no Autoboxing is rewuired Bcz String is Already Class....

next we are calling print() with String obj so that it will print the MC

Posted by Uday Kumar    2014-11-12 12:17:25

ans 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 double obj created.makes use of autoboxing to encapsulate the value 12.0, which is double, into an using doubleobject reference print() called.sop prints 12.0                                          2nd string object is created with generic type class ,string dnt use any wrappers bcz string non primitive type.thn print() called with string obj reference MC get printd.

Posted by Maheshwari Natarajan    2014-11-12 17:07:27


Posted by Shubham Bansal    2014-11-13 14:18:02

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

© meritcampus 2019

All Rights Reserved.

Open In App