• 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

Creating array in Java

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 8863
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok thanks
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79240
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic