What is the difference between following 2 approaches in creating an array.
Which is preferred depending on the use?
Approach 1:
int a[]= new int[5];
a[0] = 98;
a[1] = 97;
a[2] = 99;
a[3] = 94;
a[4] = 96;
Approach 2:
int a[]={98,97,99,94,96};
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
What is the difference between following 2 approaches in creating an array.
Which is preferred depending on the use?
Approach 1:
int a[]= new int[5];
a[0] = 98;
a[1] = 97;
a[2] = 99;
a[3] = 94;
a[4] = 96;
Approach 2:
int a[]={98,97,99,94,96};
Also posted and answered at: https://coderanch.com/t/703504/java/array-java
and https://www.dreamincode.net/forums/t...array-in-java/
Please use more descriptive title for your posts. All questions on this forum are about Java.
I've changed this one. Next time be sure to use a better title.
If you don't understand my answer, don't ignore it, ask a question.
It doesn't really matter. I prefer the 2nd approach because there is less typing and I'm less likely to make a mistake. But the compiler generates the same byte code for both.
Regards,
Jim
One issue might be where the code is located. The first would need to be inside a method. The second could be outside of a method.
If you don't understand my answer, don't ignore it, ask a question.
If we're to simply fill the thing with one constant after another, devoid of any other kind of complicated construction, the second approach is dramatically preferable IMO.
So much about proper coding is in clarity/readability. One of my mantras is to always code for the poor engineer who has to read your code at 3am while hopped up on caffeine....
In the first case, my own pattern recognition will look several times at the individual assignments and wonder what the reason may be for not using the 2nd well understood idiom. I'd also be forced to verify (painfully if the array were even a little larger) that the indexes were all increasing by 1.
The first declaration allocates a variable "a" on the stack which points to the first element of the array on the heap. The heap has 4 spaces allocated for it. You then fill those spaces up with following declarations. This is called dynamic allocation. It's useful when you don't know exactly what's going to be in those allocated memory spaces. The second declaration allocates those variables directly on the stack.