• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

overloading example from examlab

 
S Ali
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



My doubt is how come capture(s1+=20000) evaluate to short !! isn't 20000 considered an integer literal therefore producing the output as an int?
 
S Ali
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok then s1+=20000 is different from s1 = s1 + 20000
 
Dejan Miler
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


First thing that is happens here is that JVM pass reference value s1 which is 6 to the method and when that method execution
is over then JVM add value of 20000 to the value of s1.

Dejan
 
Chandana Garlapati
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about capture();?? why it prints "F"??
I dont know why it is considering as varargs i.e; Long... x in this program..

can anyone help me??
 
Siva Masilamani
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
var...arg means zero or more argument.

In the above program the only function that can take zero argument is capture and hence the output F.

But if the function call has arguments then compiler will choose var...arg method as the last option to select from.
 
Chandana Garlapati
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Siva Masilamani
 
Francisco Montes
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dejan,

I´m sorry but it does not work the way you mentioned. Try this:



This is the output:
Value received in capture method: 20006
Value after calling capture: 20006

So in fact, the expression is completely evaluated before passing its result into the method.
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
compiler converts this


to this



and s+=20000; works differently then s=s+2000; as
for s+=2000; implicit conversion is done from int to short.
and for s=s+2000; we need to do explicit conversion.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic