Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Q about increment and assignment

Reply
Thread Tools

Q about increment and assignment

 
 
Big D
Guest
Posts: n/a
 
      12-23-2006
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...

 
Reply With Quote
 
 
 
 
John W. Kennedy
Guest
Posts: n/a
 
      12-23-2006
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!

--
John W. Kennedy
"The blind rulers of Logres
Nourished the land on a fallacy of rational virtue."
-- Charles Williams. "Taliessin through Logres: Prelude"
 
Reply With Quote
 
 
 
 
Patricia Shanahan
Guest
Posts: n/a
 
      12-23-2006
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...
>


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
 
Reply With Quote
 
Karl Uppiano
Guest
Posts: n/a
 
      12-24-2006

"John W. Kennedy" <(E-Mail Removed)> wrote in message
news:c0ijh.1967$(E-Mail Removed)...
> 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!
>
> --
> John W. Kennedy


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.


 
Reply With Quote
 
John W. Kennedy
Guest
Posts: n/a
 
      12-24-2006
Karl Uppiano wrote:
> "John W. Kennedy" <(E-Mail Removed)> wrote in message
> news:c0ijh.1967$(E-Mail Removed)...
>> 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!
>>
>> --
>> John W. Kennedy

>
> 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.

--
John W. Kennedy
"The blind rulers of Logres
Nourished the land on a fallacy of rational virtue."
-- Charles Williams. "Taliessin through Logres: Prelude"
 
Reply With Quote
 
Mike Schilling
Guest
Posts: n/a
 
      12-24-2006
Karl Uppiano wrote:
>
> 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.


 
Reply With Quote
 
Karl Uppiano
Guest
Posts: n/a
 
      12-24-2006

"John W. Kennedy" <(E-Mail Removed)> wrote in message
news:v8mjh.1600$(E-Mail Removed)...
> Karl Uppiano wrote:
>> "John W. Kennedy" <(E-Mail Removed)> wrote in message
>> news:c0ijh.1967$(E-Mail Removed)...
>>> 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!
>>>
>>> --
>>> John W. Kennedy

>>
>> 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'...


 
Reply With Quote
 
Stefan Ram
Guest
Posts: n/a
 
      12-24-2006
"Mike Schilling" <(E-Mail Removed)> writes:
>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.

 
Reply With Quote
 
Patricia Shanahan
Guest
Posts: n/a
 
      12-24-2006
Karl Uppiano wrote:
> "John W. Kennedy" <(E-Mail Removed)> wrote in message
> news:c0ijh.1967$(E-Mail Removed)...
>> 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!
>>
>> --
>> John W. Kennedy

>
> 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
 
Reply With Quote
 
zeromonge@gmail.com
Guest
Posts: n/a
 
      12-24-2006
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'

> 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...


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Post increment ++ has higher precedence than pre increment ++. Why? Alf P. Steinbach /Usenet C++ 0 05-22-2011 12:03 PM
why prefix increment is faster than postfix increment? jrefactors@hotmail.com C++ 99 06-11-2010 12:51 PM
post increment or pre increment? Peng Yu Perl Misc 7 11-23-2008 11:44 PM
Augument assignment versus regular assignment nagy Python 36 07-20-2006 07:24 PM
why prefix increment is faster than postfix increment? jrefactors@hotmail.com C Programming 104 10-27-2005 11:44 PM



Advertisments