Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Array Question

 
Tom Keith
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
Consider the following String array declarations.
1)
String[] paramString = new String[4];
paramString[0] = "user1";
paramString[1] = "pwd";
paramString[2] = "1";
paramString[3] = "sa";
2)
String[] paramString = new String[4] {"user1", "pwd", "1", "sa"};
3)
String[] paramString = {"user1", "pwd", "1", "sa"};
My assumption is that all of three are same
Is there is any advantage or disadvantage in doing them or is it just a matter of coding style?
Does it have to do anything with Garbage Collection?
Thanks,
Tom
 
Bosun Bello
Ranch Hand
Posts: 1511
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Number 2 will not compile. Did you try it?
 
Tom Keith
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bosun,
Number 2 compiles and works fine . I have been using it for quite sometime. I just wanted to know the difference.
Thanks,
Tom
 
timothy zimmerman
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Odd I would not have thought that 2 would have compiled either. I don't think you need to dimension the array if you are setting it's elements.
to be sure I did a quick test and declaring the array as String[] arr1 = new String[4]{ "one", "two", "three", "four" };
gives the following:
Test.java:10: ';' expected
String[] arr1 = new String[4]{ "one", "two", "three", "four" };
 
timothy zimmerman
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry Posted before I finished. As far as your question ... I am not whether the way in which an array is declared and populated would matter to the GarbageCollector.
I would think it is probably coding style. You may know how many elements you want to put in an array but you may not know what those elements are at that moment ... or even at the same time ( you may need to store some ... od some work and come back and add the rest later )
 
matt hooker
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tom,
FYI - unless you are running your own special implementation of Java - number 2 cannot compile as it is syntactically rubbish !!!
 
matt hooker
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anyway - that matter aside, the difference between style 1 and 3 (the 2 that work ) is the amount you have to type.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I recall, option 3 was not possible in the earliest versions of Java. But eventually they decided to go ahead and give us this bit of syntactic sugar. The difference between 1 and 3 has no effect on garbage collection. If you know all the elements you want at compile time, you might as well use 3 as it's shorter. 1 is longer but more flexible - use it if you don't know all the elements in advance.
 
Marilyn de Queiroz
Sheriff
Posts: 9067
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps you mean

String[] paramString = new String[] {"user1", "pwd", "1", "sa"};
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic