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
Joined: Nov 06, 2000
Posts: 1506
posted
0
Number 2 will not compile. Did you try it?
Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
Tom Keith
Greenhorn
Joined: Mar 26, 2002
Posts: 24
posted
0
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
Joined: Jun 26, 2001
Posts: 149
posted
0
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
Joined: Jun 26, 2001
Posts: 149
posted
0
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
Joined: Jul 26, 2001
Posts: 46
posted
0
Tom, FYI - unless you are running your own special implementation of Java - number 2 cannot compile as it is syntactically rubbish !!!
Its not what you do, its the way you say you've done it.
matt hooker
Ranch Hand
Joined: Jul 26, 2001
Posts: 46
posted
0
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
Joined: Jan 30, 2000
Posts: 18670
posted
0
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.
"I'm not back." - Bill Harding, Twister
Marilyn de Queiroz
Sheriff
Joined: Jul 22, 2000
Posts: 9033
10
posted
0
Perhaps you mean
String[] paramString = new String[] {"user1", "pwd", "1", "sa"};
JavaBeginnersFaq "Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt