• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

public static void main(String... _)

 
Lee Quixotic
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1089
14
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's your string variable name for the var-arg.
 
Lee Quixotic
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, so "_" is a valid variable name? If so, I'm slightly embarrassed, but thank you so much for the clarification!
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lee Quixotic wrote:Ah, so "_" is a valid variable name?


Syntactically valid? Yes. Reasonable to use in real-world code? No.
 
Winston Gutkowski
Bartender
Pie
Posts: 10508
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21419
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Paul Witten
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49796
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12186
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10508
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Saloon Keeper
Posts: 15441
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic