• 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
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

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
Posts: 38906
684
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: 232
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Petra,
If needed, here's an additional reference
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!