Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Differences

 
Ranganath Junpal
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
int[] intArray={3, 5, 2, 8, 6};
System.out.println(intArray[3]);

If this is ok...then why is the following not ok???
................

int[] intArray;
intArray ={3, 5, 2, 8, 6};
System.out.println(intArray[3]);
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
int[] intArray;
//intArray ={3, 5, 2, 8, 6};
intArray =new int[]{3, 5, 2, 8, 6};
System.out.println(intArray[3]);
 
Praveen Babu
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Array initializer is a shortcut option for the two line declaration of
an array

And the specification says,

An array initializer may be specified in a declaration, or as part of an array creation expression (�15.10), creating an array and providing some initial values:

i.e, the possible ways could be:

1. int[] intArr = new int[] {1,2,3};
2. int[] intArr = {1,2,3};

For more info see,

http://java.sun.com/docs/books/jls/third_edition/html/arrays.html#11358

Regards,
Praveen
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Ranganath]: If this is ok...then why is the following not ok???

Essentially, the syntax rules are written in a way that forces you to identify the type of the array in some way when you create it. If you initialize the array variable when you declare it, then the compiler (and reader) can tell what type it is from the declared type of the array variable:

    int[] intArray={3, 5, 2, 8, 6};

However if I see this line:

    intArray ={3, 5, 2, 8, 6};

I can't immediately tell what type the array is. Is it byte[], int[], double[]? Of course the name of the variable is a clue here, but usually names are not that specific. I can look up the type of the intArray declaration, but that may be a bit harder to find - it's not always going to be declared just one line earlier as in your example. Java's designers wanted to make it easy for a reader to see the type of an object as it's being created. (There are other places in the language where this idea is not followed rigorously, e.g. autoboxing/unboxing, but that was the intent for this rule at the time it was made.) In fact Java originally required the use of the "new" keyword to create an array, and you had to say something like

    int[] intArray=new int[] {3, 5, 2, 8, 6};

But later someone decided this extra verbosity was not necessary, and around JDK 1.1 (or maybe 1.2) they allowed the shorter form

    int[] intArray={3, 5, 2, 8, 6};

Hope that helps...
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jim Yingst:
I can look up the type of the intArray declaration, but that may be a bit harder to find - it's not always going to be declared just one line earlier as in your example.


And remember that the declaration *could* be

Object intArray;
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic