This week's book giveaways are in the Scala and Android forums.
We're giving away four copies each of Machine Learning Systems: Designs that scale and Xamarin in Action: Creating native cross-platform mobile apps and have the authors on-line!
See this thread and this one for details.
Win a copy of Machine Learning Systems: Designs that scale this week in the Scala forum
or Xamarin in Action: Creating native cross-platform mobile apps in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

OCPJP  RSS feed

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is the difference between var-args and an array?

1.public void myMethod(int y, String...z){}
2.public void myMethod(int y, String[] z){}

Do both methods mean the same?
 
Marshal
Posts: 59092
180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the viewpoint of the receiving method, there is no real difference. The compiler will see those two methods as override‑equivalent, so they won't both compile together. The difference is how you call them. You can call
myMethod(123, "CodeRanch", "is", "brilliant");
or
myMethod(123, new String[]{"CodeRanch", "is", "brilliant"});
But the receiving method will receive one int and one three‑element String[] in both cases. The variable arguments list is collected into an array.
You will notice a difference like this:-
 
Kajal Dusseja
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:



Method with three integer arguments will be called.
Thank You.
 
Campbell Ritchie
Marshal
Posts: 59092
180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Correct
 
I'm so happy! And I wish to make this tiny ad happy too:
Rocket Oven Kickstarter - from the trailboss
https://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!