Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Creating array in Java

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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};
 
Marshal
Posts: 6851
470
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd say stick to second approach in this kind of situation. It is way more concise, more readable and has less places to go wrong, i.e.: by mistyping index.

Write angle brackets after the data type, and not the variable name.

Better:
int[] numbers

Worse:
int numbers[]


Put spaces around '=', put spaces after ','.

Better:
int[] numbers = {1, 2, 3, 4};

Worse:
int a[]={98,97,99,94,96};

Noticed variable name? Instead 'a', I've got 'numbers'. Better than 'a', but not very concrete. loterryNumbers would be better. Don't have 'a', 'b', 'z' as variable names (in most cases), they mean nothing.
 
khushi sharma
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok thanks
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

khushi sharma wrote: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};



There should be no difference as far as I know. Yet approach 2 is better looking, in my opinion.
 
Marshal
Posts: 64689
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Sayali pawar wrote:. . . There should be no difference as far as I know. . . .

The first approach is not only more time‑consuming, but it error‑prone, as Liutauras has hinted. The first approach has the risk that the number of elements is not exactly the same as the length of the array, which you won't find out about until runtime when either you suffer an out of bounds exception (more elements than spaces) or a null pointer exception (arrays of referene types only: more space than elements). The array initialiser guarantees that the size of the array and the number of elements will be the same.
The better looks of the second approach are accompanied by better behaviour
But both have the style error that the [] are after the name of the array. Also, as Liutauras said, a is a very bad identifier.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!