~ Mansukh
~ Mansukh
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[][];
~ Mansukh
Mansukhdeep Thind wrote:So shouldn't the compiler treat them as 2 1D arrays ? Why this behavior?
~ Mansukh
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?
There is another possible explanation. It was allowed in C++…Matthew Brown wrote:. . . My speculation is that it was allowed in Java because it was allowed in C . . .
Look at Matthew Brown’s posts; you can hardly explain it better than that.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?
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).
~ Mansukh
Consider Paul's rocket mass heater. |