1

What happens here? This seems very weird to me.

public int v0, v1 = v0++, v2;
0

4 Answers 4

4

It declares all three instance members (I know they're instance members because you're not getting an error), exactly as though they were declared separately, and initializes one of them explicitly to the value of v0++, which is the value of v0 before it gets incremented:

public int v0;
public int v1 = v0++;
public int v2;

You could also think of it like this:

public int v0;
public int v1;
public int v2;

// (In a constructor)
v1 = v0++;

Which is effectively:

public int v0 = 0;
public int v1 = 0;
public int v2 = 0;

// (In a constructor)
v1 = v0++;

After the instance is created, the variables will have these values:

v0: 1    Because it was incremented
v1: 0    Because it received the value of `v0` *prior* to the increment
v2: 0    Because that's the default value for `int` instance members

I said "instance members" because if these were variables in a method rather than instance members of a class, you'd get an error complaining that you were using v0 before it was initialized. (Edit: Er, um, and then there's that public thing -- doh!)

Sign up to request clarification or add additional context in comments.

Comments

4

It is equal to:

public int v0;
public int v1 = v0++;
public int v2;

2 Comments

I see! I misunderstood and thought this set v0 AND v1 equal to v0 incremented and a new variable v2, respectively.
No problem, look at the answer of @T.J. Crowder for a more detailed explanation.
0

Basicly, you can it like so:

public int v0;
public int v1 = v0++;
public int v2;

It is a way to add multiple variables more efficiently. If you don't understand the v1 = v0++, it is a way to say v1 = v0; v0 = v0 + 1;. So v1 will get the value of v0 then v0 wil get + 1. So let's say v0 = 5, then v0++ -> v1 = 5 and v0 + 1 -> 5 + 1 -> v0 = 6;

I find it better to setup your vars like so:

public int v0, 
           v1 = v0++,
           v2;

3 Comments

v0++ means v0 = v0 + 1 (increment v0). v1 = v0++ means set v1 to current value of v0, then increment v0 (v0 gets a new value, v1 will have v0's old value). v1 = ++v0 means increment v0, then set v1 to new value of v0 (both will have same value).
I noticed your edit but you're still getting it wrong. ++ doesn't only give you an incremented value to store in another variable, it also increments the source variable. The position of the ++ (before or after variable name) also determines the behavior and can give very different results! This applies to -- as well.
Ergo: "So let's say v0 = 5, then v0++ -> v0 + 1 -> 5 + 1 -> v1 = 6" would have been correct if it was ++v0 but it's wrong for v0++. The correct result in that case is v1 becomes 5, while v0 becomes 6.
0

The above code will give a syntax error: v0 may not have been initialized or so. And even after you try to separate the vo and v1, the code will still give you syntax error.

1 Comment

Why don't you test it? I did, and it runs fine. No syntax error. Even without separating.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.