Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class OverloadingTest {
- public static int sum(int ...a){
- int sum=0;
- for(int i : a){
- sum += i;
- }
- System.out.print("this is variant args//");
- return sum;
- }
- public static double sum(double a, double b) {
- return a + b;
- }
- public static void main(String[] args) {
- System.out.println(sum(1.5, 2.5));
- System.out.println(sum(10, 20));
- System.out.println(sum(10, 20,30));
- }
- }
- 4.0
- this is variant args//30
- this is variant args//60
- 4.0
- 30.0
- this is variant args//60
- int x = sum(10, 20);
- System.out.println(sum(10, 20));
- Primitive widening uses the smallest method argument possible
- Wrapper type cannot be widened to another Wrapper type
- You can Box from int to Integer and widen to Object but no to Long
- Widening beats Boxing, Boxing beats Var-args.
- You can Box and then Widen (An int can become Object via Integer)
- You cannot Widen and then Box (An int cannot become Long)
- You cannot combine var-args, with either widening or boxing
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement