aspose file tools*
The moose likes Beginning Java and the fly likes Doubt regarding primitive array declaration Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Doubt regarding primitive array declaration" Watch "Doubt regarding primitive array declaration" New topic
Author

Doubt regarding primitive array declaration

Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

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?


~ Mansukh
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13875
    
  10

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:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36486
    
  16
It all goes to show what iffy code you can get past the compiler.
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

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?
Ranjith Suranga
Ranch Hand

Joined: Oct 28, 2012
Posts: 84
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

Joined: Jul 27, 2010
Posts: 1157

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
Sheriff

Joined: Oct 13, 2005
Posts: 36486
    
  16
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.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4240
    
    7

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

Joined: Jul 27, 2010
Posts: 1157

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?
Ranjith Suranga
Ranch Hand

Joined: Oct 28, 2012
Posts: 84
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
Sheriff

Joined: Oct 13, 2005
Posts: 36486
    
  16
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
Sheriff

Joined: Oct 13, 2005
Posts: 36486
    
  16
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

Joined: Jul 27, 2010
Posts: 1157

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt regarding primitive array declaration
 
Similar Threads
Simple array question
doubt in certpal mock ?
3d arrays
Doubt in exceptions thrown by JVM and by programmer
Why does inheritance work like this when it comes to constructors?