While using the concept of overloading in java .if we call the method b. getdata(5) then which is correct | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

While using the concept of overloading in java .if we call the method b. getdata(5) then which is correct

a.getdata(int) b.getdata(byte) c.getdata(short) d.getdata(long)

25th Aug 2017, 6:52 PM
Nidhi Undirwade
Nidhi Undirwade - avatar
4 Answers
+ 2
Since the literal 5 is by default an integer type in Java the method that uses int as its parameter will be the one that is called. If you were to use a variable or constant of a different type then the method with the parameter of its corresponding type would be used. Likewise, if you declared the literal as a type long 5L then the method using long as its parameter type will be used. You can also utilize a cast to convert the literal to the desired type, such as using (short)5 as the argument, so that it will cal the method that uses the short type as its parameter type. Example: public class Program { public static void main(String[] args) { byte x = 5; getData(5); getData(5L); getData((byte)5); getData((short)5); getData(x); //Byte } static void getData(int x) { System.out.println("Integer"); } static void getData(byte x) { System.out.println("Byte"); } static void getData(short x) { System.out.println("Short"); } static void getData(long x) { System.out.println("Long"); } }
25th Aug 2017, 7:24 PM
ChaoticDawg
ChaoticDawg - avatar
+ 3
a
1st Sep 2017, 6:24 PM
Divya
Divya - avatar
+ 2
a option is correct .the all data types are belonging to int family .hence the ans is int
5th Sep 2017, 4:48 AM
Nidhi Undirwade
Nidhi Undirwade - avatar
+ 1
a
25th Aug 2017, 7:15 PM
Мг. Кнап🌠
Мг. Кнап🌠 - avatar