What's the difference? (int a; int b vs int a, b;)

edited March 2017 in Programming Questions

Hi, what exactly is the difference between declaring variables seperately, e.g.

int a;
int b;

and declaring them this way:

int a, b;

?

Tagged:

Answers

  • Answer ✓

    none

  • edited March 2017 Answer ✓

    @goldey --

    You can do this:

    int a;
    int b;
    int c;
    a = 2;
    b = 4;
    c = 6;
    println(a,b,c);
    

    or:

    int a, b, c;
    a = b = c = 6;
    println(a,b,c);
    

    or:

    int a=2, b=4, c=6;
    println(a,b,c);
    

    ...and the you end up with three declared and assigned variables using any approach.

    However, if you mix types:

    int a;
    int b;
    String c;
    

    ...you cannot declare or assign then together with commas:

    int a, b, String c; // WRONG
    int a=2, b=4, c="x"; // WRONG
    
  • int a,b,c=a=b=6;

Sign In or Register to comment.