• 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:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

varargs, Chapter 4, no 5  RSS feed

 
Greenhorn
Posts: 3
  • 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
Marshal
Posts: 38506
653
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: 217
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Petra,
If needed, here's an additional reference
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!