What will be the output of the following program?
class Testing
{
public static void main(String ar[])
{
Testing t = new Testing();
t.show(1);
}
void show(byte b) {System.out.println("Byte");}
void show(short s) {System.out.println("Short");}
void show(int i) {System.out.println("Int");}
void show(long l) {System.out.println("Long");}
}
(a) What will be the output when we remove the
void show(int i)
{System.out.println("Int");}
from the class.
(b) What will be the output when we remove last two methods from the class?
Explain the output.
class Testing
{
public static void main(String ar[])
{
Testing t = new Testing();
t.show(1);
}
void show(byte b) {System.out.println("Byte");}
void show(short s) {System.out.println("Short");}
void show(int i) {System.out.println("Int");}
void show(long l) {System.out.println("Long");}
}
(a) What will be the output when we remove the
void show(int i)
{System.out.println("Int");}
from the class.
(b) What will be the output when we remove last two methods from the class?
Explain the output.
Output Will be 1 if Nothing is Removed
ReplyDeletea.) long
b.)short
First answer is correct but the second one is not correct.
DeleteThe main thing is to understand the concept. Why "Long" will be the output of the first (a) part.
And output will never be 1 in any case in the above program.
DeleteThe Output of the above program is:
ReplyDeleteInt
(a) Long
(b) Compile Time Error
Now you have to identify the concept behind it.
DeleteHow Compile Time Error Sir????
DeleteTwo concepts will be applied to this question:
Delete1. All the numbers are by default integer type.
2. Upcasting is done automatically in java but downcasting must be done explicitly.
We are passing a number which is by default integer. If we have a method with long type argument then integer will be upcasted to long but if we have only short and byte type methods then downcasting will not be done. Therefore the error will be at compile time saying that no suitable method found.
Good Post! Thank you so much for sharing the nice post, it was so good to read and useful to improve
ReplyDeleteJava Training in Electronic City