+ 4

This s a question n d challenge. I dont understand how it works.How ans 1 came?Can any1 plz explain??

whats d o/p of this code? static<T>void f(T x){ System.out.println(1); } static void f(final int i){ system.out.println(2); } public static void main(String[] args){ integer i=8; f(i); } ans:1

2nd Jul 2017, 4:14 PM
Rose
Rose - avatar
2 Respostas
+ 2
static <T> void f(T x){ System.out.println(1); } This is a generic method. It will take any type, including implicitly wrapped primitive types. So this method will match the call of f(i) and will shadow the other method that takes an int for a parameter.
2nd Jul 2017, 6:11 PM
ChaoticDawg
ChaoticDawg - avatar
+ 2
Integer store references to Integer objects, just as with any other reference (object) type. And in your program it accepts a parameter of primitive type. in the second function your are passing an object of integer class and Java handles this with generics and gives output 1
2nd Jul 2017, 6:15 PM
Nityanand Katare
Nityanand Katare - avatar