This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes Differences Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Differences" Watch "Differences" New topic
Author

Differences

Ranganath Junpal
Ranch Hand

Joined: Aug 31, 2006
Posts: 76
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

Joined: Jun 09, 2003
Posts: 4632
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

Joined: Jul 30, 2006
Posts: 138
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

Joined: Jan 30, 2000
Posts: 18671
[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...


"I'm not back." - Bill Harding, Twister
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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;


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Differences
 
Similar Threads
Method Invocation..
Can anyone explain please
ArrayIndexOutOfBound
Array
Anonymous arrays