Win a copy of Head First Go this week in the Go forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Devaka Cooray
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Tim Holloway
  • Claude Moore
  • Stephan van Hulst
Bartenders:
  • Winston Gutkowski
  • Carey Brown
  • Frits Walraven

varargs, Chapter 4, no 5  RSS feed

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the book "OCA study guide" from Boyarsky/Selikoff the 5th question in chapter 4 is:

Given the following method, which of the method calls return 2?
public int howMany(boolean b, boolean... b2){
  return b2.length;
}
...
F. howMany(true, {true, true}
...



F is a wrong answer and in eclipse it actually does not compile. But why? {true, true} is a short form to create an array and to fill it. For example I can write "boolean b = {true, true};
 
author & internet detective
Posts: 39175
724
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Petra,
Great question. The "full" syntax for creating a primitive array is



If you pass new boolean[] {true} to a method that takes varargs, all is well.

Java recognizes there is redundancy. So if you are assigning the array to a variable, it lets you omit the new boolean[] part. With method signatures, this isn't implied so isn't allowed.
 
Ranch Hand
Posts: 246
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Petra,
If needed, here's an additional reference
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!