• Post Reply Bookmark Topic Watch Topic
  • New Topic

Method Overloading  RSS feed

 
Sharmistha Sarkar
Ranch Hand
Posts: 50
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Can anyone explain what is the meaning of 2nd doIt(int... val)? It accepts only 3 dots not more or less...mmm interesting...i guess it is to accept multiple values or array !! hope so!!..and one more question-How could I pass value in this method like System.out.println(t.doIt(4, 5)); ?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sharmistha Sarkar wrote:
Can anyone explain what is the meaning of 2nd doIt(int... val)?


It's called varargs. It lets you pass any number of arguments to the method, and inside the method body, the val parameter is an array.
 
Niraj Jha
Ranch Hand
Posts: 63
Java Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is how we implement var args(feature of 1.5).
Finally it will get executed only when you pass more then two int type values or an array in doIt() method as binding is always with specific type of parameter list.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Niraj Jha wrote:it will get executed only when you pass more then two int type values or an array


There are two other cases where the varargs version will be called.
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can I suggest you change the return values in that method to "two ints" and "varargs". It will make it more obvious what is happening. Also put some more tests in; try the method with different numbers of arguments.
 
Sharmistha Sarkar
Ranch Hand
Posts: 50
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alright , Jeff , what are the 2 other cases to call varags?
Sure Campbell, tried with some test cases.
Here the improved one
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sharmistha Sarkar wrote:Alright , Jeff , what are the 2 other cases to call varags?

We've covered the two arg case and the more than two arg case. What other possible cases can you think of ?
You've already mentioned one of them in the code you posted.
 
Sharmistha Sarkar
Ranch Hand
Posts: 50
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, waiting to hear from Jeff....
Jeff, in your post did you mean to give any extra information "There are two other cases where the varargs version will be called. "?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sharmistha Sarkar wrote:Well, waiting to hear from Jeff....
Jeff, in your post did you mean to give any extra information "There are two other cases where the varargs version will be called. "?


No, I did not mean to give any more information. I wanted Niraj Jha to work it out for himself. He said:
Niraj Jha wrote:it will get executed only when you pass more then two int type values or an array


I claim that there are two other cases where the varargs version will be called, and, as Stuart points out, you've already found one of them and posted it in your code. So that leaves one more case.

(I guess what I'm calling "two cases" could also simply be rolled into "one case" for consistency if we're viewing Niraj's "more than 2 args" as "one case".)

 
Sharmistha Sarkar
Ranch Hand
Posts: 50
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alright then, Thanks to all of you for sharing your thoughts
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!