Menu
Topics Index
...
`

Type Erasure2791


If you need explanation Read this topic

If you need Answer Take test on this topic

Type Erasure2791
What will be the output of the following program?
public class GenericsErasureDemo {
    public static void main(String args[])
    {
        GenericsErasure<Float> floatObject = new GenericsErasure<Float>(12.0F);
        System.out.println(floatObject.getClass().getName());
        floatObject.showType();
    }
}
class GenericsErasure<T> {
    T obj;
    GenericsErasure(T obj)
    {
        this.obj = obj;
    }
    void showType()
    {
        System.out.print(obj.getClass().getName());
    }
}

A. GenericsErasure
java.lang.Float
B. java.lang.Float
GenericsErasure
C. java.lang.Float
java.lang.Float
D. GenericsErasure
GenericsErasure
E. Compilation Error
Topic: Java Type Erasure

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.

a

Posted by Shubham Bansal    2014-10-28 09:01:06


Answer is A ........getClass method returns the object of type Class that represents the runtime class of the object.  and getName returns the name of the class or interface represented by this object.in the System.out.println of main method it prints the name of class corresponing to floatobject which is generic erasure and then in showType method ,Since Float is passed at runtime,it prints the  class name of the float object i.e java.lang.Float

Posted by Shashanka Mogaliraju    2014-10-28 12:29:30


Ans is A

Generics were introduced to the Java language to provide tighter type checks at compile time and to support generic programming.
To implement generics, the Java compiler applies type erasure to:

    Replace all type parameters in generic types with their bounds or Object if the type parameters are unbounded.
The produced bytecode, therefore, contains only ordinary classes, interfaces, and methods.
    Insert type casts if necessary to preserve type safety.
    Generate bridge methods to preserve polymorphism in extended generic types.

Type erasure ensures that no new classes are created for parameterized types; consequently, generics incur no runtime overhead

Here by using it replace all at the time of compilation

initially we created Object of GenericsErasure<float> the content is replaced and value is copied to T variable which is the type Float
this keyword is used referring current class object i.e.. this.obj=obj;

differtiating between local and class level variables

The java.lang.Object.getClass() method returns the runtime class of an object. This method returns the object of type Class that represents the runtime class of the object.

SOP is printed as GenericsErasure

by calling showType() method with reference to GenericsErasure Class refernce floatObject

prints the
SOP is printed as java.lang.Float

The java.io.File.getName() method returns the last name of the pathname's name sequence, that means the name of the file or directory denoted by this abstract path name is returned.
This method returns name of the file or directory or empty string if pathname's name sequence in empty

Posted by Ramesh Velpukonda    2014-10-28 13:01:26


This dose is now closed and the winners are Shashanka Mogaliraju,  for 'First Correct Comment', Ramesh Velpukonda,  for 'Best Comment' and Šâí Râm for the 'Popular Comment'. The 'lucky liker' is Urvashi Arya. 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-10-29 02:46:50


© meritcampus 2019

All Rights Reserved.

Open In App