Q about increment and assignment

B

Big D

I'm confused by the output of the following code:

public class PP {
public static void main(String[] args) {
int i = 1;
i = i++;
System.out.println(i);
}
}

It outputs 1.

I understand the assignment operator happening before the ++, but I don't
understand why the ++ doesn't increment. I thought the statement should
basically expand to:

i = i;
i = i+1;

but the ++ gets lots somewhere...
 
J

John W. Kennedy

Big said:
I'm confused by the output of the following code:

public class PP {
public static void main(String[] args) {
int i = 1;
i = i++;
System.out.println(i);
}
}

It outputs 1.

I understand the assignment operator happening before the ++, but I don't
understand why the ++ doesn't increment. I thought the statement should
basically expand to:

i = i;
i = i+1;

but the ++ gets lots somewhere...

No, it gets expanded into:

t = i;
i = i + 1;
i = t;

Don' do dat!
 
P

Patricia Shanahan

Big said:
I'm confused by the output of the following code:

public class PP {
public static void main(String[] args) {
int i = 1;
i = i++;
System.out.println(i);
}
}

It outputs 1.

I understand the assignment operator happening before the ++, but I don't
understand why the ++ doesn't increment. I thought the statement should
basically expand to:

i = i;
i = i+1;

but the ++ gets lots somewhere...

In Java, any side effects of operand evaluation happen before side
effects of the operation that uses the operands. i++ will be completely
evaluated before the assignment of its value to i.

The value of "i++" is, by definition, the value of i prior to the
increment, so the assignment restores the old value of i. The code is
equivalent to:

int temp = i; // record the value of i++, the old value of i
i = i+1; // increment i
i = temp; // assign the value of i++ to i.

Patricia
 
K

Karl Uppiano

John W. Kennedy said:
Big said:
I'm confused by the output of the following code:

public class PP {
public static void main(String[] args) {
int i = 1;
i = i++;
System.out.println(i);
}
}

It outputs 1.

I understand the assignment operator happening before the ++, but I don't
understand why the ++ doesn't increment. I thought the statement should
basically expand to:

i = i;
i = i+1;

but the ++ gets lots somewhere...

No, it gets expanded into:

t = i;
i = i + 1;
i = t;

Don' do dat!

Holy crap! I hope no one ever asks me to evaluate code like that on a job
interview. I would never write code like that, but I doubt if I'd squirt
that nuance from the top of my head.
 
J

John W. Kennedy

Karl said:
John W. Kennedy said:
Big said:
I'm confused by the output of the following code:

public class PP {
public static void main(String[] args) {
int i = 1;
i = i++;
System.out.println(i);
}
}

It outputs 1.

I understand the assignment operator happening before the ++, but I don't
understand why the ++ doesn't increment. I thought the statement should
basically expand to:

i = i;
i = i+1;

but the ++ gets lots somewhere...
No, it gets expanded into:

t = i;
i = i + 1;
i = t;

Don' do dat!

Holy crap! I hope no one ever asks me to evaluate code like that on a job
interview. I would never write code like that, but I doubt if I'd squirt
that nuance from the top of my head.

Java is an improvement over its ancestor, C. In C, the official
interpretation of the code in question is "whatever the compiler feels
like doing". Because Java places such an emphasis on portability, it
sets down rules.
 
M

Mike Schilling

Karl said:
Holy crap! I hope no one ever asks me to evaluate code like that on a
job interview. I would never write code like that, but I doubt if I'd
squirt that nuance from the top of my head.

I'll have to make that an interview question. If you answer anything other
than "I would *never* write code like that", you wouldn't get the job.
 
K

Karl Uppiano

John W. Kennedy said:
Karl said:
John W. Kennedy said:
Big D wrote:
I'm confused by the output of the following code:

public class PP {
public static void main(String[] args) {
int i = 1;
i = i++;
System.out.println(i);
}
}

It outputs 1.

I understand the assignment operator happening before the ++, but I
don't understand why the ++ doesn't increment. I thought the statement
should basically expand to:

i = i;
i = i+1;

but the ++ gets lots somewhere...
No, it gets expanded into:

t = i;
i = i + 1;
i = t;

Don' do dat!

Holy crap! I hope no one ever asks me to evaluate code like that on a job
interview. I would never write code like that, but I doubt if I'd squirt
that nuance from the top of my head.

Java is an improvement over its ancestor, C. In C, the official
interpretation of the code in question is "whatever the compiler feels
like doing". Because Java places such an emphasis on portability, it sets
down rules.

That is a Good Thing(TM). I'm not disputing that. But I doubt I could quote
chapter and verse from the JLS off the top of my head. I'm an open-book-test
kind of guy (it's how the real world works anyway. No employer I know of
would require you to work without access to any kind of reference
materials). I'm just sayin'...
 
S

Stefan Ram

Mike Schilling said:
I'll have to make that an interview question. If you answer anything other
than "I would *never* write code like that", you wouldn't get the job.

Such code might make perfect sense, for example, as part of a
Java compiler test suite.
 
P

Patricia Shanahan

Karl said:
John W. Kennedy said:
Big said:
I'm confused by the output of the following code:

public class PP {
public static void main(String[] args) {
int i = 1;
i = i++;
System.out.println(i);
}
}

It outputs 1.

I understand the assignment operator happening before the ++, but I don't
understand why the ++ doesn't increment. I thought the statement should
basically expand to:

i = i;
i = i+1;

but the ++ gets lots somewhere...
No, it gets expanded into:

t = i;
i = i + 1;
i = t;

Don' do dat!

Holy crap! I hope no one ever asks me to evaluate code like that on a job
interview. I would never write code like that, but I doubt if I'd squirt
that nuance from the top of my head.

Variations on the exact effect of various combinations of prefix/postfix
++/-- are frequently asked questions in this newsgroup. I've seen the
questions often enough to remember how to work out the answers. The fact
that the JLS has a very simple, consistent, well-defined model makes
them quite easy to answer.

The only time I've ever coded something like that in Java was when
checking my answers for more complicated cases.

Patricia
 
Z

zeromonge

Hi, this is not gonna work like you write it. Look if you do i = i++;
it will asign i = i because it evaluates i and after that it
increments.

You should do:

i = ++i;

If you only want to increment "i" then just do:

public static int main(String[] args) {
int i = 1;
i++;
System.out.println("The value of variable i is: " + i );
return 0;
}
}

Output = 'The value of variable i is: 2'
 
J

John W. Kennedy

Hi, this is not gonna work like you write it. Look if you do i = i++;
it will asign i = i because it evaluates i and after that it
increments.

You are right about the result, as far as it goes, but why are you
giving a confusing and imprecise explanation after two of us have given
exact descriptions?
You should do:

i = ++i;

No, he shouldn't. It's true that this statement works, but it's stupid.
The correct statement is:

++i;

or

i++;

(I personally prefer the first, but the second is more common in the wild.)

And don't top-post.
 
L

Luc The Perverse

Big D said:
I'm confused by the output of the following code:

public class PP {
public static void main(String[] args) {
int i = 1;
i = i++;
System.out.println(i);
}
}

It outputs 1.

I understand the assignment operator happening before the ++, but I don't
understand why the ++ doesn't increment. I thought the statement should
basically expand to:

i = i;
i = i+1;

but the ++ gets lots somewhere...


Yes

++ is a function, which does two things - returns a value, and changes the
variable.

i=i++;
the function i++ must be calculated first before the final assignment is
completed.

i= (i++);

i++ returns i, so you can reduce it to

i=i;

In other words, the ++ is not being lost, it is just being overwritten.

int i = 1;
i = i++;

is the same as writing

int i = 1;
int oldval = i;
i = i + 1;
i = oldval;
 
L

Luc The Perverse

John W. Kennedy said:
No, he shouldn't. It's true that this statement works, but it's stupid.
The correct statement is:

++i;

or

i++;

He didn't write either of them

He was trying to understand part of a test which deliberately used operators
in an unusual way to test one's understanding. The practicality of such a
test, or the implications on actual programming ability are questionable.
But it should suffice to eliminate those completely inept.
 
M

Mike Schilling

Stefan Ram said:
Such code might make perfect sense, for example, as part of a
Java compiler test suite.

As it happens, I don't hire Java compiler writers. For the usual purposes
of code writing (to implement algorithms), there's no excuse for writing
code whose meaning is that obscure, any more than there's an excuse for

int sum(int []arr)
{
int sum = 0;
int i = 0;
try {
while(true) {
sum += arr[i++];
} catch (ArrayIndexOutOfBoundsException ex) {
return sum;
}
}
 
J

John Ersatznom

Karl said:
That is a Good Thing(TM). I'm not disputing that. But I doubt I could quote
chapter and verse from the JLS off the top of my head. I'm an open-book-test
kind of guy (it's how the real world works anyway. No employer I know of
would require you to work without access to any kind of reference
materials). I'm just sayin'...

Agreed. In fact, if you're even thinking of reaching for an official
specification or asking a language lawyer to be sure of what some code
will do, it's a sign that the code in question is excessively obscure
and should be rewritten to be more self-documenting.

If I were giving a job interview, and I did include code like that on
it, the best answer I would be looking for from a potential hire would
be "I don't know for sure, but I wouldn't write code like that anyway,
and I'd hope to hell anyone who did would document it for me." :)
 
B

Big D

Thanks for the explanation, all. Obviously the temporary variable was the
key to this caper.
 
P

Patricia Shanahan

John said:
Agreed. In fact, if you're even thinking of reaching for an official
specification or asking a language lawyer to be sure of what some code
will do, it's a sign that the code in question is excessively obscure
and should be rewritten to be more self-documenting.

If I were giving a job interview, and I did include code like that on
it, the best answer I would be looking for from a potential hire would
be "I don't know for sure, but I wouldn't write code like that anyway,
and I'd hope to hell anyone who did would document it for me." :)

Oh well, that's one job I would never get. Personally, I think knowing
exactly what statements with multiple side effects do is useful, because
it makes it easier to replace them with functionally equivalent but more
readable code. It's not as though the Java model of ++ and -- is in any
way messy, unmemorable, or difficult to understand.

Patricia
 
J

John Ersatznom

Patricia said:
Oh well, that's one job I would never get. Personally, I think knowing
exactly what statements with multiple side effects do is useful, because
it makes it easier to replace them with functionally equivalent but more
readable code. It's not as though the Java model of ++ and -- is in any
way messy, unmemorable, or difficult to understand.

It's the "I wouldn't write code like that anyway" part I'd consider most
important. If you could clean up code like that written by someone else,
so much the better, especially if you could do it even when the "someone
else" hadn't documented it. :)
 
J

jupiter

John W. Kennedy said:
Big said:
I'm confused by the output of the following code:

public class PP {
public static void main(String[] args) {
int i = 1;
i = i++;
System.out.println(i);
}
}

It outputs 1.

I understand the assignment operator happening before the ++,
but I don't understand why the ++ doesn't increment. I thought
the statement should basically expand to:

i = i;
i = i+1;

but the ++ gets lots somewhere...

No, it gets expanded into:

t = i;

John, is t a copy of the bits in i made by the postfix ++ method?
i = i + 1;
i = t;

Is it fair then to say that t gets restored because of the way the
postfix operator applies only after the expression is evaluated?

What is the difference between the original problem and this one?

int i=1;
System.out.println("i=" + i++); //output=1
System.out.println("i=" + i);//output=2

I find this one easier to understand because eventually the new
bits do output. They don't get overwritten. In the original problem
there is overwriting going on that cause the 2 value to be lost
entirely. Is this due to the self-referencing aspect of the
original problem i=i++;?

One last question: Where is that temp copy of the bits visible? I
can't see it in a debugger, so maybe I'm not using it (Eclipse) to
great advantage. I see the original i bits on the stack and that's
about it. This should not involve heap storage, so that leaves me
wondering where/how I'd ever see that temp set of bits.

thanks ...
 
J

John W. Kennedy

jupiter said:
John W. Kennedy said:
Big said:
I'm confused by the output of the following code:

public class PP {
public static void main(String[] args) {
int i = 1;
i = i++;
System.out.println(i);
}
}

It outputs 1.

I understand the assignment operator happening before the ++,
but I don't understand why the ++ doesn't increment. I thought
the statement should basically expand to:

i = i;
i = i+1;

but the ++ gets lots somewhere...
No, it gets expanded into:

t = i;

John, is t a copy of the bits in i made by the postfix ++ method?

For explanatory purposes, I've eliminated the ++ altogether.
Is it fair then to say that t gets restored because of the way the
postfix operator applies only after the expression is evaluated?

My t does not exist in the postfix version at all, so in one sense the
question is meaningless, but yes, I guess you could say so.
What is the difference between the original problem and this one?
int i=1;
System.out.println("i=" + i++); //output=1
System.out.println("i=" + i);//output=2

It doesn't have the expression "i = i++", which is bad coding, though
technically legal.
I find this one easier to understand because eventually the new
bits do output. They don't get overwritten. In the original problem
there is overwriting going on that cause the 2 value to be lost
entirely. Is this due to the self-referencing aspect of the
original problem i=i++;?

Yes, that's the key.
One last question: Where is that temp copy of the bits visible? I
can't see it in a debugger, so maybe I'm not using it (Eclipse) to
great advantage. I see the original i bits on the stack and that's
about it. This should not involve heap storage, so that leaves me
wondering where/how I'd ever see that temp set of bits.

It isn't visible at the Java level, though it will be seen in the
bytecode (does Eclipse have an option to trace at the bytecode level?
I've never had occasion to try). But "i=i++" is bad, evil, wicked,
perverse, satanic, blood-sucking, kitten-murdering, baby-raping,
Bush-voting malpractice, so Just Don't Do It.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,052
Latest member
LucyCarper

Latest Threads

Top