Jump to content




[Java] Multiple vars in one line


  • You cannot reply to this topic
10 replies to this topic

#1 svdragster

  • Members
  • 222 posts
  • LocationGermany

Posted 09 July 2013 - 02:56 PM

Hey, does anyone know if something like this is possible in Java, too?
x, y, z = 4, 64, 4


#2 ETHANATOR360

  • Members
  • 423 posts
  • Locationyour hardrive

Posted 09 July 2013 - 03:01 PM

why not assign them like this?
int x = 4,y = 64, z = 4;


#3 GopherAtl

  • Members
  • 888 posts

Posted 09 July 2013 - 03:01 PM

You can if they're all the same type, the syntax is...

 int x=4, y=64, z=4;


#4 Mads

  • Members
  • 604 posts
  • LocationCopenhagen, Denmark

Posted 10 July 2013 - 02:43 AM

Provided they're all the same value and type, you can also do shit like
int x = y = z = 4;


#5 nolongerexistant

  • Validating
  • 201 posts
  • LocationNetherlands

Posted 10 July 2013 - 03:09 AM

View PostMads, on 10 July 2013 - 02:43 AM, said:

Provided they're all the same value and type, you can also do shit like
int x = y = z = 4;

You actually can't do it like that. Y and Z are undefined and that will error

#6 Mads

  • Members
  • 604 posts
  • LocationCopenhagen, Denmark

Posted 10 July 2013 - 03:52 AM

Wasn't it introduced in 1.7?

#7 nolongerexistant

  • Validating
  • 201 posts
  • LocationNetherlands

Posted 10 July 2013 - 03:57 AM

I always have the latest version of Java and my Eclipse errors:

Multiple markers at this line
- z cannot be resolved to a
  variable
- y cannot be resolved to a
  variable


#8 Engineer

  • Members
  • 1,378 posts
  • LocationThe Netherlands

Posted 10 July 2013 - 08:26 AM

http://puu.sh/3zrBv.png ?

Show us your code please

#9 Tiin57

    Java Lunatic

  • Members
  • 1,412 posts
  • LocationIndiana, United States

Posted 10 July 2013 - 09:58 AM

I thought it was
int x, y, z = 2;


#10 Mads

  • Members
  • 604 posts
  • LocationCopenhagen, Denmark

Posted 10 July 2013 - 10:45 AM

View Posttiin57, on 10 July 2013 - 09:58 AM, said:

I thought it was
int x, y, z = 2;
That would declare all three, but leave x and y un-initialised.

#11 nolongerexistant

  • Validating
  • 201 posts
  • LocationNetherlands

Posted 11 July 2013 - 05:15 AM

View PostEngineer, on 10 July 2013 - 08:26 AM, said:

http://puu.sh/3zrBv.png ?

Show us your code please

Posted Image
Posted Image





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users