• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

var args Vs array : k&b doubt

 
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I find a question in the K&B (in the first chapter)that is like this:

class Voop
{
public static void main(String[] args)
{
dostuff(1);
dostuff(2);
}
//insert code here (line 6)
}

Which, inserted independently at line 6 will compile?
This is the question.


===================================================================

Why is this alternative below wrong? (I saw the answers)
static void doStuff(int[] doArgs){}

==================================================================
In a prog I tried using
public static void main(String... args)
instead of the usual
public static void main(String[] args) and ya it works...

this being the case,
can i not say that well, when
static void doStuff(int... doArgs){} is correct, then
static void doStuff(int[] doArgs){} should also be correct...?
[ June 04, 2007: Message edited by: Vishwa nath.K ]
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,

Welcome to JavaRanch!


First of all, the method calls are doStuff(1) and doStuff(1,2);
When you write main(String.... args), it is converted to main(String[] args)
by the compiler. It is mere a flexibility provided. doStuff(int[] val)
requires int array not just two separate int values.

When you pass arguments to the main through command line like:
java myclass 1 2 3

The arguments 1 2 and 3 are placed in the array and passed to the main,
as I said there is automatic conversion of var arg to array by the compiler.

To make doStuff(int[] val) work, do like:
doStuff(new int[]{1});
doStuff(new int[]{1,2});


Thanks,
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For more info,

Visit this link:

doubt var arg


Thanks,
 
Vishwanath Krishnamurthi
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey, Thanks...

Now I get the whole picture.... So, a mere list of integers wouldnt form a list of array elements, whereas a list of strings will implicitly be treated as a list of array elements. Right?
[ June 05, 2007: Message edited by: Vishwa nath.K ]
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah but only in the context of main method, where automatic conversion
of elements to array is done.

In other places NOT. There is nothing special with String as you said.

Thanks,
 
Ranch Hand
Posts: 329
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Chandra Bhatt:
Yeah but only in the context of main method, where automatic conversion
of elements to array is done.



What do you mean by this?

I tried:



And it worked as expected.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Serigo,

I meant when you change the method signature to


Then you method invocation
this.method("bla", "blah");

wont work;

But in case you main() method signature is
class test {
public static void main(String[] args) {}
}
and you invoke java like:
> java test 1 2 3 hello hi 5

all the arguments you have passed to the main method are placed into a
String array and passed to the main method.


Did you get the difference? I hope so.


Thanks,
 
Sergio Tridente
Ranch Hand
Posts: 329
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry Chandra, I got you wrong. I thought you were saying that the 'to array conversion' on a var-arg method call was only performed when either:
- call to the var-arg method was done from within main() method.
- only for main() method

Both statements are wrong, of course.
 
Beauty is in the eye of the tiny ad.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic