Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes main ( String[] args) question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "main ( String[] args) question" Watch "main ( String[] args) question" New topic
Author

main ( String[] args) question

Tony VanHorn
Ranch Hand

Joined: Dec 13, 2006
Posts: 74
Can one use different types of arrays for the declaration in main or can you only use String[]? I was wondering if I could use char[] or int[] and if I used either one of those how would the command line have to be typed.

If I used the char[] would each letter be put into a seperate array item or would that be one argument and tried to forced in to args[0]? Any insight would be great, thanks.

Tony
Scott Johnson
Ranch Hand

Joined: Aug 24, 2005
Posts: 518
You can create a main() method with any parameters you want to, but Java looks for a specific method signature when starting an application and will ignore any main() methods that don't have the expected signature.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

To use main as an entry point (as opposed to just a method that happens to be called "main"), the parameter must be a String array. Note that even if you don't pass any arguments, you still have a String array in main, although its length is zero.

If you need to get chars from this, then within main you can use String methods like charAt or toCharArray. Similarly, if you need ints, you can use the Integer method parseInt.
[ December 22, 2006: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Nicholas Jordan
Ranch Hand

Joined: Sep 17, 2006
Posts: 1282
Originally posted by Tony VanHorn:
...as posted...
Tony


marc weber gives the book answer to your question- the only freedom you have is to call args something else, like blargs or yak or something. You will find this recurrent, it is the type of something you need to pay close attention to. What you call a variable may get you a lot of discussion from different code camps, but to the compiler the name does not even have to make sense, just line up correctly where ever you want to use it. (spelling AND case)


"The differential equations that describe dynamic interactions of power generators are similar to that of the gravitational interplay among celestial bodies, which is chaotic in nature."
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
You can create a main() method with any parameters you want to, but Java looks for a specific method signature when starting an application and will ignore any main() methods that don't have the expected signature.


Let's dig into Scott's tip a bit more ... when you start the JVM like this:

java SomeClass

The JVM looks at SomeClass and checks to see if it has a public static void main method with an array of Strings argument. If so, it calls that main. Otherwise it ... well, try it and see Make a class with no main method at all or different arguments and try starting it. You'll do it again some day by accident. It's good experience to see what the JVM does so you'll recognize it next time.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Tony VanHorn
Ranch Hand

Joined: Dec 13, 2006
Posts: 74
Awesome info guys. I appreciate the time you guys take to answer silly questions like mine. I was hoping that if I am going to take the argument and then change it into an int array or a char array it would seem easier, in my mind, to do it right off the bat. I didn't know if we could over load main() like we could other constructors, or if was a whole different case, which it seems to be. Thanks again guys, I am sure you will seeing alot more of me. Oh, and Stan, I will definately try it out tomorrow after I get off work. Gotta love holidays in the retail business.

Tony
[ December 22, 2006: Message edited by: Tony VanHorn ]
Steffen Reinhard
Greenhorn

Joined: Jun 13, 2005
Posts: 19
Originally posted by Tony VanHorn:
I didn't know if we could over load main() like we could other constructors,
[ December 22, 2006: Message edited by: Tony VanHorn ]


Hi Tony,

it is indeed possible to overload a main method (which is, by the way, something pretty different from a constructor). The following text shows the output of a small program using overloaded versions of the main method:


This is the small program that does it all:


But what could be the use of overloading the main method? In terms of clarity and readability of your sources you don't win. Why not do the cast to whatever type you need in the basic String[] args version of your main method?

Hope this helps,

cheers,


Steffen

[ December 23, 2006: Message edited by: Steffen Reinhard ]

[ December 23, 2006: Message edited by: Steffen Reinhard ]
[ December 23, 2006: Message edited by: Steffen Reinhard ]
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Steffen already pointed this out, but I think it's worth emphasizing: A main method is not a constructor. A constructor creates a new instance and gets that instance ready for use. But as Stan pointed out, a main method is simply a method that is automatically called when Java is invoked from the command line. In other words, it's an "entry point" for running a Java class file. And in order to function as that entry point, it must be public, static, return void, and take a String array as its argument.

Aside from that, there's nothing special about "main." It is not a keyword, and you can overload the method as much as you like. However, overloaded versions will not be directly invoked by the JVM because they don't fit the designated signature.

Also, as Nicholas pointed out, the name you give to main's String array can be any legal identifier. Conventionally, this is "args" or "argv" -- shorthand for "arguments" or "argument values" -- but this is not a requirement. (I don't know whether the real SCJP exam would ask this, but I've definitely seen it on mock exams.)

Steffen: Can we count you among our Mac users? Or are you using another Unix variant?
Steffen Reinhard
Greenhorn

Joined: Jun 13, 2005
Posts: 19
Originally posted by marc weber:

Steffen: Can we count you among our Mac users? Or are you using another Unix variant?


I'm having a never-ending love affair with Ubuntu Linux's dapper drake (Oh, I know, the roman catholic church will strongly disapprove ...)
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Steffen Reinhard:
...I'm having a never-ending love affair with Ubuntu Linux's dapper drake...

Nicholas Jordan
Ranch Hand

Joined: Sep 17, 2006
Posts: 1282
Originally posted by Tony VanHorn:
[QB]Awesome info guys.[QB]

Correct. Mastery is just mastery of the basics. We're just practicing on you.
I appreciate the time you guys take to answer silly questions like mine.

The only dumb question is one you should have asked and didn't.
I was hoping that if I am going to take the argument and then change it into an int array or a char array it would seem easier, in my mind, to do it right off the bat.

Why ?

(trick question, we know what you are thinking - it's to get you to think about what you are thinking. The sample code Steffen Reinhard addresses this issue correctly, given the phrasing of your question.

Ultimately you are correct, it's just how you go about it.

I didn't know if we could over load main() like we could other constructors, or if was a whole different case, which it seems to be.

Correctly addressed by other posters.

Gotta love holidays in the retail business.

The retail business gets us a lot of work.

Tony, the special characters in the post below are html comment markers, the posters to whom I am speaking will recognize these.
<!-- Are you guys sure you want to drag R.C.C. into this ? -->
[ January 07, 2007: Message edited by: Nicholas Jordan ]
Tony VanHorn
Ranch Hand

Joined: Dec 13, 2006
Posts: 74
Tony, the special characters in the post below are html comment markers


LOL...Thanks

Tony
 
 
subject: main ( String[] args) question
 
Similar Threads
Why can't int array agrument in main method??
Array without ints
Doubt on toString()
overloaded methods
retain apostrophe in Collection object