• Post Reply Bookmark Topic Watch Topic
  • New Topic

Basic Syntax Confusion  RSS feed

 
Andy Richard
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone!
I'm a newbie and have just started learning Java.
I'm stuck at a point,
does the below three statements mean the same?
1. String[ ] args[ ] ;
2. String args[ ] ;
3. String[ ] args ;
 
Jhon Gonzales
Greenhorn
Posts: 22
Java Linux PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, all those mean the same. However, is easier identify a array if the brackets are in the middle.
 
Andy Richard
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Edwin
okay, the last two i understand.
but what about the first one?
what does having two " [ ] " signifies ??
 
Andy Richard
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
moreover, if i use the third type as a parameter in my main( ) function,
then it throws a run-time error..
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Juzzz Coded wrote:@Edwin
okay, the last two i understand.
but what about the first one?
what does having two " [ ] " signifies ??

Edwin is wrong.
The first one declares an array that contains String arrays. It copuld also have been declared as
2 and 3 declare arrays that contain Strings.
 
Jhon Gonzales
Greenhorn
Posts: 22
Java Linux PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The first one is a array of array (Two dimensional) like this one: String [ ][ ] args. The second and last ones are one-dimensional arrays.
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Edwin Gonzales wrote:The first one is a array of array (Two dimensional)

Don't think of arrays of arrays as two-dimensional arrays. That suggests that all the contained arrays need to be of the same length, which they don't.
 
Andy Richard
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay well, so you all mean that i can't use String[ ] args[ ].
thanks.
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andy Richard wrote:okay well, so you all mean that i can't use String[ ] args[ ].
thanks.

Not in the declaration of your main method.
You can use it elsewhere in your code, but if you do it is better to use the

format as it is easier for someone reading the code to understand.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12564
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stuart A. Burkett wrote:
Andy Richard wrote:okay well, so you all mean that i can't use String[ ] args[ ].
thanks.

Not in the declaration of your main method.

Hate to nitpick, but you CAN use it in the declaration of a main method. Just not in the main method you want to run when you type "java myClass". There is nothing wrong with having a method named main that takes an array of arrays of strings.
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:
Stuart A. Burkett wrote:
Andy Richard wrote:okay well, so you all mean that i can't use String[ ] args[ ].
thanks.

Not in the declaration of your main method.

Hate to nitpick, but you CAN use it in the declaration of a main method. Just not in the main method you want to run when you type "java myClass". There is nothing wrong with having a method named main that takes an array of arrays of strings.

True, but I was basing my answer on Andy's earlier post
Andy Richard wrote:moreover, if i use the third type as a parameter in my main( ) function,
then it throws a run-time error..

He would only get a runtime error if the main method he was referring to was the program entry point. The runtime error would have complained about not being able to find a main method.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!