• 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

Doubt regarding primitive array declaration

 
Ranch Hand
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

Have a look at this snippet:



the compiler is complaining at line 07 that "Type mismatch: cannot convert from int[] to int[][]". Aren't we declaring both ant and pant to be 1D arrays? Why is the compiler viewing pant[] as an array of an array of ints?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, you're declaring pant as a 2D array. This line is confusing:

It means the same as:

Note that in Java, you are allowed to put the [] before or after the variable name, but it's recommended to put it before the variable name. So:
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It all goes to show what iffy code you can get past the compiler.
 
Mansukhdeep Thind
Ranch Hand
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But howcome translates in to ? There is a comma(,) between the 2 declarations. So shouldn't the compiler treat them as 2 1D arrays ? Why this behavior?
 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Buddy...

I think , you should see this...
http://docs.oracle.com/javase/specs/jls/se7/html/jls-10.html#jls-10.2

The [] may appear as part of the type at the beginning of the declaration, or as part of the declarator for a particular variable, or both.
For example:

byte[] rowvector, colvector, matrix[];

This declaration is equivalent to:

byte rowvector[], colvector[], matrix[][];



But don't ask me why.. ? like what I did with "final variables". do you remember... ?
 
Mansukhdeep Thind
Ranch Hand
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is the whole point of JavaRanch. Finding the "WHAT happens when" is easy. It is the "WHY IT HAPPENS SO" which interests me?
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The type of a variable should precede it.
The type of an array is Foo[].
∴Foo[] should precede the variable name.

The scope of the type is the whole declaration, as underlined.
   byte[] rowvector, colvector, matrix[];
The scope of [] after an identifier is the identifier, as in red print.
   byte[] rowvector, colvector, matrix[];
∴ matrix is in scope for two pairs of [].

You may use that, but it is poor style. The type of the variable should precede it. Look at this style recommendation.
 
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mansukhdeep Thind wrote:So shouldn't the compiler treat them as 2 1D arrays ? Why this behavior?



Think of it being like this:

So there are two variables of type int[]. But the second one is an array variable, which means it's an array of type int[]. The spacing you gave in your example makes it misleading, but the [] attaches to the int, not the following ant.

I'm another who thinks that this is poor style. My speculation is that it was allowed in Java because it was allowed in C (and C programmers seem to enjoy declaring all their variables on the same line, if the old code I maintain is anything to go by).
 
Mansukhdeep Thind
Ranch Hand
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ohhhh. OK Now! So [] after an identifier means only that identifier and [] before an identifier means the whole declaration only if that is the first identifier. Correct Ritchie?
 
Ranji Sura
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mansukhdeep Thind wrote:That is the whole point of JavaRanch. Finding the "WHAT happens when" is easy. It is the "WHY IT HAPPENS SO" which interests me?



Hi buddy.. Let me ask you something...


I know this occurs a compile time error.. But now I also want to know why ?
Can't it treat like int[][]y ?
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matthew Brown wrote:. . . My speculation is that it was allowed in Java because it was allowed in C . . .

There is another possible explanation. It was allowed in C++…
Agree about C programmers. You can find C books which teach how to program with the fewest possible keystrokes.

I said the [] formed part of the type; if they are after the comma, the [] are dissociated both from their type and their identifier. You will have to look through the grammar; that tells you what is and what is not allowed. It would have been awkward to permit the [] after the comma.
As a general rule of thumb, the less you allow in the grammar the fewer disputes there will be.
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mansukhdeep Thind wrote:Ohhhh. OK Now! So [] after an identifier means only that identifier and [] before an identifier means the whole declaration only if that is the first identifier. Correct Ritchie?

Look at Matthew Brown’s posts; you can hardly explain it better than that.
 
Mansukhdeep Thind
Ranch Hand
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matthew Brown wrote:

Mansukhdeep Thind wrote:So shouldn't the compiler treat them as 2 1D arrays ? Why this behavior?



Think of it being like this:

So there are two variables of type int[]. But the second one is an array variable, which means it's an array of type int[]. The spacing you gave in your example makes it misleading, but the [] attaches to the int, not the following ant.

I'm another who thinks that this is poor style. My speculation is that it was allowed in Java because it was allowed in C (and C programmers seem to enjoy declaring all their variables on the same line, if the old code I maintain is anything to go by).



You nailed it Matt. Thanks a pile. I shall go ahead and mark this post as closed.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic