Advertisement
Guest User

Untitled

a guest
Apr 21st, 2014
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. public class OverloadingTest {
  2. public static int sum(int ...a){
  3. int sum=0;
  4. for(int i : a){
  5. sum += i;
  6. }
  7. System.out.print("this is variant args//");
  8. return sum;
  9. }
  10.  
  11. public static double sum(double a, double b) {
  12. return a + b;
  13. }
  14.  
  15. public static void main(String[] args) {
  16. System.out.println(sum(1.5, 2.5));
  17. System.out.println(sum(10, 20));
  18. System.out.println(sum(10, 20,30));
  19. }
  20. }
  21.  
  22. 4.0
  23. this is variant args//30
  24. this is variant args//60
  25.  
  26. 4.0
  27. 30.0
  28. this is variant args//60
  29.  
  30. int x = sum(10, 20);
  31.  
  32. System.out.println(sum(10, 20));
  33.  
  34. Primitive widening uses the smallest method argument possible
  35. Wrapper type cannot be widened to another Wrapper type
  36. You can Box from int to Integer and widen to Object but no to Long
  37. Widening beats Boxing, Boxing beats Var-args.
  38. You can Box and then Widen (An int can become Object via Integer)
  39. You cannot Widen and then Box (An int cannot become Long)
  40. You cannot combine var-args, with either widening or boxing
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement