Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Declaring multiple variables on one line

 
nick woodward
Ranch Hand
Posts: 370
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thought they had to all be the same type, but apparently not. Are arrays the only exception?



Thanks!

Nick

PS: sorry Roel, I'm still coming back to that exception thread, I've just been massively distracted!
 
Scott Selikoff
author
Saloon Keeper
Posts: 4033
18
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's allowed but it falls under the category of "please, never ever do this in real code".
 
nick woodward
Ranch Hand
Posts: 370
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Scott Selikoff wrote:It's allowed but it falls under the category of "please, never ever do this in real code".


So it'll definitely be in the exam then!

Thanks for the reply,

Nick
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nick woodward wrote:Thought they had to all be the same type, but apparently not. Are arrays the only exception?

Yes, they have to be the same type because you can't write something like thisBut this is allowed and it's also on one line Because using [] is allowed after the variable name (although that's definitely a bad practice!) and these brackets only apply to that variable, you can indeed have different types. And here is why that's considered a bad practice And it's even allowed in a for loop as well, and that's even more fun Output:
[10, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[10, 20, 0, 0, 0, 0, 0, 0, 0, 0]
[10, 20, 30, 0, 0, 0, 0, 0, 0, 0]
[10, 20, 30, 40, 0, 0, 0, 0, 0, 0]
[10, 20, 30, 40, 50, 0, 0, 0, 0, 0]
[10, 20, 30, 40, 50, 60, 0, 0, 0, 0]
[10, 20, 30, 40, 50, 60, 70, 0, 0, 0]
[10, 20, 30, 40, 50, 60, 70, 80, 0, 0]
[10, 20, 30, 40, 50, 60, 70, 80, 90, 0]
[10, 20, 30, 40, 50, 60, 70, 80, 90, 100]


Hope it helps!
Kind regards,
Roel
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nick woodward wrote:So it'll definitely be in the exam then!

I can't remember having a question on any of my certification exams having such a declaration. But maybe I was just lucky
 
Scott Selikoff
author
Saloon Keeper
Posts: 4033
18
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:
nick woodward wrote:So it'll definitely be in the exam then!

I can't remember having a question on any of my certification exams having such a declaration. But maybe I was just lucky


I don't think I've seen one either.. Perhaps the style is so terrible even they don't think you should know it!
 
nick woodward
Ranch Hand
Posts: 370
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah, i wasn't entirely serious about the exam but it's good to know that its the only exception of this sort - well, and the for loop - thanks roel.


cheers for the replies,

nick
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nick woodward wrote:but it's good to know that its the only exception of this sort - well, and the for loop - thanks roel.

The "exception" in the for loop is exactly the same as the other one. And as you know you can't declare multiple variables of different types in the initialization expression of a for loop, so that's also not allowed in a statement somewhere else. And there's just one exception: using [] after the variable name, you can declare multiple variables of a different type. But the variable and the different arrays you can declare will all store values of the same type. As the following code snippet illustrates, you can declare a double variable, a 1D double array, a 4D double array, another double variable, and a 7D double array in one statement

Hope it helps!
Kind regards,
Roel
 
Daniel Cox
Ranch Hand
Posts: 147
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that in this code

a number of variables are declared as the same type

da = a primitive variable is declared as type double
d1 = a reference to an array object is declared as type double
d4 = a reference to a multi-dimentional array object is declared as type double

When you declare the type of a reference to an array object, you're declaring the type of the elements stored in the array object.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel Cox wrote:a number of variables are declared as the same type

That's incorrect! And that can be easily illustrated with this simple code snippetIf da and d1 would be declared as the same type, line1 would compile successfully. But that's not the case: you'll get a compiler error. da and d1 are incompatible types: the first one is double and the latter one is double[].

Hope it helps!
Kind regards,
Roel
 
Daniel Cox
Ranch Hand
Posts: 147
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you declare the type of a primitive variable, you're declaring the type of the data that can be stored in the primitive variable.

When you declare the type of a reference to an array object, you're declaring the type of the elements that can be stored in the array object.

In both cases, the type is double
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel Cox wrote:In both cases, the type is double

But the type of da is different from the type of d1. And that's why you can invoke e.g. the getClass() method on the latter one but not on the first one.
 
Daniel Cox
Ranch Hand
Posts: 147
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The JLS says that every array object has an associated Class object. So the method getClass() returns the class of the associated Class object. However, I think that when double is used in the declaration of an array, double refers to the type of the elements that can be stored in the array object (not the type of its associated Class object). And when double is used in the declaration of a primitive variable, double refers to the type of data that can be stored in the primitive variable.
 
Daniel Cox
Ranch Hand
Posts: 147
7
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:da and d1 are incompatible types: the first one is double and the latter one is double[]

You're right! The JLS says:
An array type is written as the name of an element type followed by some number of empty pairs of square brackets [].

However, the compiler allows the declaration double da, d1[] because the primitive type and the array's element type are the same. Not because the array type and the primitive type are the same. Since the array type is made up of 2 components, element type followed by [], even though the array type is double[], double does refer to the array's element type.

I suspect that

double da, d1[];

is just syntactic sugar for

double da;
double d1[];


So the type double applies in both cases. In the first case, double implies the type of the primitive variable. In the second case, double implies the array's element type.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel Cox wrote:Not because the array type and the primitive type are the same.

Although I didn't dive into the JLS, I was absolutely sure that variables da and d1 have a different type. Otherwise the compiler would have never given a compiler error when you write da == d1.

Daniel Cox wrote:So the type double applies in both cases. In the first case, double implies the type of the primitive variable. In the second case, double implies the array's element type.

If I have to nitpick, I think this statement only applies to a 1D array. If you have a nD (with n > 1) array, that's strictly speaking not correct (although I can understand where you are coming from). Let's assume this 2D-arrayOnly for the elements on the second level ([i][j]), the element type is double. But for the elements on the first level ([i]), the type is not double but double[]. And that's again very easily illustrated with a simple code snippet

Hope it helps!
Kind regards,
Roel

PS. Have a cow for diving into the JLS and looking for an appropriate explanation!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic