• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what is difference between String[] string1 and String string2[].
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Hibernate Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Climb the rope! CLIMB THE ROPE! You too tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic