| Author |
public static void main(String... _)
|
Lee Quixotic
Greenhorn
Joined: Oct 28, 2012
Posts: 3
|
|
Hello!
Today I found out that you can create a parameter list of variable length in a method declaration. When I read further, I stumbled upon this strange notation:
I tried it and it worked without any errors. Now I know what the String... does, but I cannot find out what the underline means. Is it a new form of writing an identifier you're sure not to use again in your code?
Thanks in advance for any help!
Lee
|
 |
Chan Ag
Ranch Hand
Joined: Sep 06, 2012
Posts: 97
|
|
|
It's your string variable name for the var-arg.
|
 |
Lee Quixotic
Greenhorn
Joined: Oct 28, 2012
Posts: 3
|
|
Ah, so "_" is a valid variable name? If so, I'm slightly embarrassed, but thank you so much for the clarification!
|
 |
Jeff Verdegan
Bartender
Joined: Jan 03, 2004
Posts: 6109
|
|
Lee Quixotic wrote:Ah, so "_" is a valid variable name?
Syntactically valid? Yes. Reasonable to use in real-world code? No.
|
 |
Winston Gutkowski
Bartender
Joined: Mar 17, 2011
Posts: 4905
|
|
Jeff Verdegan wrote:
Lee Quixotic wrote:Ah, so "_" is a valid variable name?
Syntactically valid? Yes. Reasonable to use in real-world code? No.
And I'll go further than that. DON'T put '_'s in variables names at all, unless you're specifically asked to. It's a convention used in Python, C++ (and, I believe, perl), and marks you out as a Java "foreigner".
The same is true of '$', which is also valid, but DON'T USE IT.
Winston
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16815
|
|
The other point that should be mentioned is that the main method is defined to take a string array -- which holds the command line parameters. It just so happens that using var-args works because var-args are implemented as arrays. Regardless, you should be not doing that -- assuming you mean the main method of the program that is.
Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Paul Witten
Ranch Hand
Joined: Oct 10, 2012
Posts: 86
|
|
Winston Gutkowski wrote:The same is true of '$', which is also valid, but DON'T USE IT.
Winston, what about "m" for mMyClassScopedField or "s" for sMyStaticClassScopedField?
I remember running into those deep in the bowels of something and feeling kind of glad to be reminded how they were declared.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32833
|
|
Paul Witten wrote:what about "m" for mMyClassScopedField or "s" for sMyStaticClassScopedField?
Both those things ought to be forgotten. You can find more about them by googling for “Hungarian Notation”. Note there is a kind of “Hungarian Notation”, which is all right to use, according to Joel Spolsky.
|
 |
fred rosenberger
lowercase baba
Bartender
Joined: Oct 02, 2003
Posts: 10043
|
|
Winston Gutkowski wrote:It's a convention used in Python, C++ (and, I believe, perl)...
In perl, $_ is a language defined variable with special meaning.
|
 |
Winston Gutkowski
Bartender
Joined: Mar 17, 2011
Posts: 4905
|
|
fred rosenberger wrote:In perl, $_ is a language defined variable with special meaning.
Ah, that's the chap. It's been so long...
Winston
|
 |
Lee Quixotic
Greenhorn
Joined: Oct 28, 2012
Posts: 3
|
|
Henry Wong wrote:The other point that should be mentioned is that the main method is defined to take a string array -- which holds the command line parameters. It just so happens that using var-args works because var-args are implemented as arrays. Regardless, you should be not doing that -- assuming you mean the main method of the program that is.
I'm really thankful for this information, for I otherwise would have continued to use var-args as my main method parameter.
Thank you for all the other replies and advises, too!
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12953
|
|
Henry Wong wrote:The other point that should be mentioned is that the main method is defined to take a string array -- which holds the command line parameters. It just so happens that using var-args works because var-args are implemented as arrays. Regardless, you should be not doing that -- assuming you mean the main method of the program that is.
Section 12.1.4 of the Java Language Specification explains that either the version with a string array or the version with varargs is acceptable - so, the language specification officially says it works with varargs; it's not just an implementation detail that the varargs version also works, because it happens to be implemented with arrays.
So there's not really a reason so that you shouldn't do that - it's just that varargs are a relatively new feature in Java (it was added in Java 5) and most people are used to the version with a string array.
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
 |
|
|
subject: public static void main(String... _)
|
|
|