• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overloading makes Difficult using j2se 5

 
kirba devi
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class Dog3 {
static void go(long x) { System.out.print("int "); }
static void go(int x) { System.out.print("double "); }
static void go(double...x) { System.out.print("double "); }
public static void main(String[] args) {
Dog3 d = new Dog3();
double b = 5;
double s = 5;
int l = 5;
float f = 5.0f;
go(b);
go(s);
go(l);
go(f);
}
}

output ouble double double double .How?
my expected output ouble double int double
As per my knowledge,
1)first matching occurs by the use of primitives,it can be widening in some cases.
2)then boxing
3)then var-args

Regards
Kirba

Plase correct me if iam wrong.
 
Henry Wong
author
Marshal
Pie
Posts: 21490
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Maybe this has something to do with... your int version of the method, also prints double.

Henry
 
kirba devi
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Wong,
Thanks for identifying the problem.
i have read the rules
2)We can box and widen..//for this can you explain with example.
3)We cannot widen from one wrapper type to another

eg ublic static void go(Long x) {}

Integer x=5;
go(5);
Above example is what rule 3 says? Please correct if iam wrong!!!

4)We cannot widen and box:
eg ublic static void go(Long x) {}

int x=5;
go(5);


Regards
Kirba.
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the source of this question ?
 
kirba devi
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From K&B book

chpater-3 -Assignment Page NO:244


Regards
Kirba
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic