Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

what is the difference between String[] args and String args[]

 
Cq
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are they same.
 
deekasha gunwant
Ranch Hand
Posts: 396
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Cq,

yes they are absolutely same.

regards
Deekasha
 
Angela Poynton
Ranch Hand
Posts: 3143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cq as you are new to Javaranch i think I should point out that we have a naming policy which we enforce to mantain a professional atmosphere. Take a look here please and register again with a name that fits the rules.
Thanks
 
kirank
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, both of them are absolutly the same. Please refer to Java Laguage Specification (JLS) to see the syntax used in java.
String[]args or String args[] will create an array (reserves a place in memory)with no size and name args.
 
Sandeep Jain
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes they are the same.
You can decalre an array of string by either ways. Both will work fine.

------------------
Sandeep Jain
 
Mike Brown
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm glad he asked that question! I have been wondering that since I started studying Java. I have seen it both ways and both seem to work but I dodnt want to ask. Thanks cq!
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cq and Kirank:
Please do note that you should update your names to be in line with the naming policy as Angela mentioned. However, you do not need to re-register; simply update your display name to be in accordance with that policy.
Thanks,
Joel
 
Steven Broadbent
Ranch Hand
Posts: 400
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This form is preferred for declaring arrays:-

String [] x;
putting the [] after the name. Why? because it avoids subtle problems like this:-
String x [] , y; //x is an array but y is a String
String [] x y ; //two arrays
 
Yi Meng
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They are the same as in JLS
The [] may appear as part of the type at the beginning of the declaration, or as part of the declarator for a particular variable, or both, as in this example:
byte[] rowvector, colvector, matrix[];
This declaration is equivalent to:
byte rowvector[], colvector[], matrix[][];

The bottom line is that dun put [] before your variable, you may see the following legal:

But this line will fail:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic