Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring 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
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

arrays and varargs

 
Ranch Hand
Posts: 145
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public int howMany(boolean b, boolean... b2) {
return b2.length;
}

in the call to the above method : why is this not allowed - howMany(true, {true, true});

the answer justifies that it is not the proper way to declare arrays,

but when boolean[] a={true,false}; is valid why not this call ?
 
author & internet detective
Posts: 40035
809
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This does compile:


The problem isn't that you can't create an inline array. The problem was that the syntax is wrong. Whereas when creating an array and storing it in a variable, you don't need to specify the type; it is implied form the context.
 
blossom belle
Ranch Hand
Posts: 145
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

howMany(true,{true, true}); in this too, the second argument is expected to be a boolean array. So the context is quite clear. Then why doesn't it work ?

howMany(true,true, true); in this statement, the second and third parameters are boolean which  matches the required argument type. We don't directly mention any context here in the method call. And it works fine !

Why isn't a similar logic used in howMany(true,{true, true}) ?
 
blossom belle
Ranch Hand
Posts: 145
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's just that , whatever values we pass in the method call, it's going to be assigned to the corresponding argument in the method definition's signature.

So what we are actually passing in the method call,  is the right hand side of a variable assignment right ?
And what we have in the signature of the method (i.e. in the argument part of the method definition, is the left hand side of the variable assignment) right ?

In that case, why doesn't it work ?
 
author
Posts: 23878
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

shambhavi sham wrote:
howMany(true,{true, true}); in this too, the second argument is expected to be a boolean array. So the context is quite clear. Then why doesn't it work ?



It doesn't work because the Java Language Specification says so...

Perhaps, in the future, this syntax will be allowed... but, currently, array initializers are only allowed in declarations.

shambhavi sham wrote:
howMany(true,true, true); in this statement, the second and third parameters are boolean which  matches the required argument type. We don't directly mention any context here in the method call. And it works fine !



This works fine because the JLS defines the rules for variable arity methods, how they are resolved, etc. etc.

shambhavi sham wrote:
Why isn't a similar logic used in howMany(true,{true, true}) ?



Same answer.... because the JLS says so.  If you need a more detailed response, unfortunately, you will have to contact Oracle (and the stakeholders that are part of the Java Community Process).

Henry
 
blossom belle
Ranch Hand
Posts: 145
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK Henry ! Thanks for your response !  
and Thanks to Jeanne too !  
    Bookmark Topic Watch Topic
  • New Topic