• Post Reply Bookmark Topic Watch Topic
  • New Topic

what is difference between String[] string1 and String string2[]  RSS feed

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is difference between String[] string1 and String string2[].
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Priyatham Anisetty wrote:what is difference between String[] string1 and String string2[].

Absolutely nothing (and I wish to God Java had never allowed the second style).

Winston
 
Priyatham Anisetty
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Winston,
Thanks for reply .
I have one more small question

String[] s1;
s1[0] ="hi";


But I am getting an error s1 is not initialized
How can I make it Dynamic.

Thanks in Advance
 
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Priyatham Anisetty wrote:what is difference between String[] string1 and String string2[].

Absolutely nothing (and I wish to God Java had never allowed the second style).


For the most part, I agree... however...

I think that it is somewhat relevant when multiple variables are declared at the same time... for example, with the first syntax, I can declare three string arrays like this....



which I guess is shorter than with the second syntax....



However, with the second syntax, I can mix arrays with non-arrays...



Henry
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Priyatham Anisetty wrote:Hi Winston,
Thanks for reply .
I have one more small question

String[] s1;
s1[0] ="hi";


But I am getting an error s1 is not initialized
How can I make it Dynamic.

Thanks in Advance


Not sure what you mean by dynamic, but the error message is clear. You can't use an array without actually instantiating one.

Henry
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Priyatham Anisetty wrote:But I am getting an error s1 is not initialized
How can I make it Dynamic.

By initializing it, viz:
String[] s1 = new String[1];
s1[0] ="hi";


although that is not what is meant by "dynamic arrays" (see ArrayList).

Winston
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:However, with the second syntax, I can mix arrays with non-arrays...

Yup, that may well have been their thinking for allowing it. Still don't like it though, especially as it may lead newbies to believe that String and String[] are interchangeable.

Winston
 
Priyatham Anisetty
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Priyatham Anisetty wrote:But I am getting an error s1 is not initialized
How can I make it Dynamic.

By initializing it, viz:
String[] s1 = new String[1];
s1[0] ="hi";


although that is not what is meant by "dynamic arrays" (see ArrayList).

Winston


Yes I was thinking of Dynamic arrays. So I should use ArrayList<String> if I want the dynamic capabilities.
 
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An example such as



illustrates perfectly why the second array declaration syntax should never have been introduced. Declaring multiple variables on the same line would not pass any code review of mine.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!