+ 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)
4 Réponses
+ 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");
}
}
+ 3
a
+ 2
a option is correct .the all data types are belonging to int family .hence the ans is int
+ 1
a