Topics Index

Overloading of int and Integer

If you need explanation Read this topic

If you need Answer Take test on this topic

Overloading of int and Integer
What will be the output of the following program?
public class TheBest
    public void sameClass(Integer i)
        System.out.println("Integer sameClass");
    public void sameClass(int i)
        System.out.println("int sameClass");
    public static void main(String args[])
        new TheBest().sameClass(8);

A. Integer sameClass
B. int sameClass
C. int sameClass
Integer sameClass
D. Integer sameClass
int sameClass
E. Compilation Error
Topic: Creating Objects for Primitive Data Types (Byte, Short)

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:B .... the 'int' type is a primitive , whereas the 'Integer' type is an object

Posted by Ashok Reddi    2014-03-03 04:28:48

answer is B because when two overriding methods one with primitive type and reference type of same datatype then primitive type is called. If there is no primitive type then wrapper class reference is called...

Posted by Ashok Kumar Paritala    2014-03-03 05:32:11

Answer is B. Java uses the closest matching method for the declared types of the argument expressions when choosing between different overloads. In this case, sameClass(int i) is closer than sameClass(Integer i) {because sameClass(Integer i) requires auto-boxing}. If we want this to be invoked, then we should pass ---> sameClass(new Integer(8))

Posted by Shaileshwar Sharma    2014-03-03 11:28:17


Posted by Vinay Jogu    2014-03-03 16:24:54

Ans is B it will print int same as 8 is type int

Posted by Sai Abhishek    2014-03-04 01:30:47

Congratulations Ashok Reddi. You are this dose winner. We will send you the link using which you can claim your recharge.

Posted by Merit Campus    2014-03-04 04:23:31

© meritcampus 2019

All Rights Reserved.

Open In App