Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   Arrays in java (http://www.velocityreviews.com/forums/t947961-arrays-in-java.html)

mmc.java 07-09-2012 02:54 PM

Arrays in java
 
I am just starting to learn java and wanted to know when creating an array if there is any reason why you choose one of the following over the others or if it is just a styling choice by the developer

int productIDs[] = {10,20,30};
int []productIDs = {10,20,30};
int[] productIDs = {10,20,30};

I did notice the the following will cause a compilation error
[]int productIDs = {10,20,30}; //causes an error

Eric Sosman 07-09-2012 03:30 PM

Re: Arrays in java
 
On 7/9/2012 10:54 AM, mmc.java wrote:
> I am just starting to learn java and wanted to know when creating an array if there is any reason why you choose one of the following over the others or if it is just a styling choice by the developer
>
> int productIDs[] = {10,20,30};
> int []productIDs = {10,20,30};
> int[] productIDs = {10,20,30};


All are equivalent. The first suggests the code was written by
an immigrant from C or C++, the last is the style usually seen in
Java, and the middle is just plain ugly.

> I did notice the the following will cause a compilation error
> []int productIDs = {10,20,30}; //causes an error


So will

int productIDs = []{10,20,30};
int productIDs = {10,20,30}[];
int productIDs = {[10,20,30]};
productIDs int[] = {10,20,30};

.... and a lot of other random stuff, too.

--
Eric Sosman
esosman@ieee-dot-org.invalid



markspace 07-09-2012 04:17 PM

Re: Arrays in java
 
On 7/9/2012 7:54 AM, mmc.java wrote:
> I am just starting to learn java and wanted to know when creating an
> array if there is any reason why you choose one of the following over
> the others or if it is just a styling choice by the developer



It's "style choice" but there a convention too, most of the code I've
seen uses the style below. For readability, I'd recommend it.
(Readability = it's what everyone is used to seeing.)

> int[] productIDs = {10,20,30};



Joshua Cranmer 07-09-2012 04:45 PM

Re: Arrays in java
 
On 7/9/2012 10:54 AM, mmc.java wrote:
> I am just starting to learn java and wanted to know when creating an
> array if there is any reason why you choose one of the following over
> the others or if it is just a styling choice by the developer
>
> int productIDs[] = {10,20,30};


This one, I suspect, is a holdover from C/C++, where the array specifier
has to come after the variable name. I'd recommend not using it, since
it makes it less clear that 'int[]' is the type.

> int []productIDs = {10,20,30};
> int[] productIDs = {10,20,30};


These two are effectively equivalent, differing only in whether you
think "int []" or "int[]" is a more natural way to specify the type. I
tend towards the latter, since "[]" is bound to the int type and not to
the name of productIDs.

--
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth



Roedy Green 07-09-2012 06:03 PM

Re: Arrays in java
 
On Mon, 9 Jul 2012 07:54:39 -0700 (PDT), "mmc.java"
<michael.p.mulligan@gmail.com> wrote, quoted or indirectly quoted
someone who said :

>int[] productIDs = {10,20,30};


the [] are part of the type so I like to keep them together. The
other style was to make Java look more like C.

see http://mindprod.com/jgloss/array.html
for things a newbie needs to know about arrays.
--
Roedy Green Canadian Mind Products
http://mindprod.com
Why do so many operating systems refuse to define a standard
temporary file marking mechanism? It could be a reserved lead character
such as the ~ or a reserved extension such as .tmp.
It could be a file attribute bit. Because they refuse, there is no
fool-proof way to scan a disk for orphaned temporary files and delete them.
Further, you can't tell where the orhaned files ame from.
This means the hard disks gradually fill up with garbage.


Lew 07-09-2012 07:56 PM

Re: Arrays in java
 
markspace wrote:
> mmc.java wrote:
>> I am just starting to learn java [sic] and wanted to know when creating an
>> array if there is any reason why you choose one of the following over
>> the others or if it is just a styling choice by the developer


The brackets with the type is the more sensible style.

Putting brackets on the variable splits the type information between the
beginning and the end of the declaration. It's more sensible to keep
all the type part of the declaration together.

> It's "style choice" but there a convention too, most of the code I've
> seen uses the style below. For readability, I'd recommend it.
> (Readability = it's what everyone is used to seeing.)
>
> > int[] productIDs = {10,20,30};


It's also logic.

The array dimensions are part of the type. So the type of 'productIDs'
in your example is 'int[]'. Splitting that into 'int productIDs[]' is both
less logical and less readable.

Regardless of your style choice (i.e., the right style or the wrong one)
the code conventions require that you not mix types on one line of
declaration. This is moot of you follow the other convention to
declare only one variable per line.

<http://www.oracle.com/technetwork/java/javase/documentation/codeconventions-141270.html#2992>

Note also that the return type of a method must use the 'int[]' form,
so varying that for variables makes even less sense.

--
Lew

Daniel Pitts 07-09-2012 10:16 PM

Re: Arrays in java
 
On 7/9/12 3:03 PM, Patricia Shanahan wrote:
> Eric Sosman wrote:
>> On 7/9/2012 10:54 AM, mmc.java wrote:

> ...
>>> I did notice the the following will cause a compilation error
>>> []int productIDs = {10,20,30}; //causes an error

>>
>> So will
>>
>> int productIDs = []{10,20,30};
>> int productIDs = {10,20,30}[];
>> int productIDs = {[10,20,30]};
>> productIDs int[] = {10,20,30};
>>
>> ... and a lot of other random stuff, too.
>>

>
> I think the OP may be trying to find out why one totally illogical
> placement of the "[]" works, although another, slightly less illogical,
> placement does not. At least "[]int" keeps all the type information
> together.
>
> The answer, already given, is to allow C and C++ programmers to use a
> familiar order.
>
> Patricia

Here is one that'll blow your mind:

int[] arr[] = {{},{1},{2,3},{4,5,6}};

I'm guessing you could even do "int[] []arr[]"


Lew 07-09-2012 11:14 PM

Re: Arrays in java
 
Daniel Pitts wrote:
> Here is one that'll blow your mind:
>
> int[] arr[] = {{},{1},{2,3},{4,5,6}};
>
> I'm guessing you could even do 'int[] []arr[]';


Yes, but the JLS strongly discourages that.

“Brackets are allowed in declarators as a nod to the tradition of C and C++. The general rules for variable declaration, however, permit brackets toappear on both the type and in declarators, so that the local variable declaration:

“ float[][] f[][], g[][][], h[]; // Yechh!

“We do not recommend "mixed notation" in an array variable declaration, where brackets appear on both the type and in declarators.”

<http://docs.oracle.com/javase/specs/jls/se7/html/jls-10.html#jls-10.2>

Know. Don't guess. Read the JLS.

Chant 12 times until it's ingrained in your subconscious.

--
Lew


Daniel Pitts 07-10-2012 04:43 AM

Re: Arrays in java
 
On 7/9/12 4:14 PM, Lew wrote:
> Daniel Pitts wrote:
>> Here is one that'll blow your mind:
>>
>> int[] arr[] = {{},{1},{2,3},{4,5,6}};
>>
>> I'm guessing you could even do 'int[] []arr[]';

>
> Yes, but the JLS strongly discourages that.
>
> “Brackets are allowed in declarators as a nod to the tradition of C and C++. The general rules for variable declaration, however, permit brackets to appear on both the type and in declarators, so that the local variable declaration:
>
> “ float[][] f[][], g[][][], h[]; // Yechh!
>
> “We do not recommend "mixed notation" in an array variable declaration, where brackets appear on both the type and in declarators.”
>
> <http://docs.oracle.com/javase/specs/jls/se7/html/jls-10.html#jls-10.2>
>
> Know. Don't guess. Read the JLS.

If it was something I cared to know, I would have. I know that even if
it was valid, I would never use it. If I ever encountered it, I would
look up the rules on how exactly it works.

I was mostly just being goofy with examples of what can be done, even if
it should be avoided.

Lew 07-10-2012 06:47 AM

Re: Arrays in java
 
On 07/09/2012 09:43 PM, Daniel Pitts wrote:
> On 7/9/12 4:14 PM, Lew wrote:
>> Daniel Pitts wrote:
>>> Here is one that'll blow your mind:
>>>
>>> int[] arr[] = {{},{1},{2,3},{4,5,6}};
>>>
>>> I'm guessing you could even do 'int[] []arr[]';

>>
>> Yes, but the JLS strongly discourages that.
>>
>> “Brackets are allowed in declarators as a nod to the tradition of C and C++.
>> The general rules for variable declaration, however, permit brackets to
>> appear on both the type and in declarators, so that the local variable
>> declaration:
>>
>> “ float[][] f[][], g[][][], h[]; // Yechh!
>>
>> “We do not recommend "mixed notation" in an array variable declaration,
>> where brackets appear on both the type and in declarators.”
>>
>> <http://docs.oracle.com/javase/specs/jls/se7/html/jls-10.html#jls-10.2>
>>
>> Know. Don't guess. Read the JLS.

> If it was something I cared to know, I would have. I know that even if it was
> valid, I would never use it. If I ever encountered it, I would look up the
> rules on how exactly it works.
>
> I was mostly just being goofy with examples of what can be done, even if it
> should be avoided.


Sorry, I should have made it clear that I was speaking of general principle
for the wider audience. If anything I should have emphasized the validity of
your observation.

But lately in particular, and for a long time in general, I have seen a
tendency for programmers to assume or guess or infer or imagine technical
information that is readily and authoritatively available. The Internet
removes much of the excuse for uncertainty, pretty much all of it when it
comes to Java. Especially things that are baldly and rather aggressively
stated in the single most seminal and normative Java specification there is,
the Java specification itself.

I found the under-five minutes to confirm this particular nugget, via a link
that had been posted in this very thread multiple times already by Roedy and
others, to be a negligible burden to incur prior to averring the validity of
your observation.

--
Lew
Honi soit qui mal y pense.
http://upload.wikimedia.org/wikipedi.../c/cf/Friz.jpg




All times are GMT. The time now is 03:49 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.