This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes Postfix vs Unary.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Postfix vs Unary.." Watch "Postfix vs Unary.." New topic
Author

Postfix vs Unary..

Jay Taylor
Greenhorn

Joined: Jan 31, 2008
Posts: 6
I want to make sure I understand this.
class Sequen
{
public static void main(String args[])
{
int a = 2;
int b = 7;

int e = 2;
int f = 7;

int c = a *++b;

int d = --e *f;// This will case d to equal 7
int d = e-- *f;// This will cause d to equal 14

System.out.println(c);
System.out.println(d);
}
So the question is.. Will my Unary not be calculated in these problems? How can I cause the Unary to decrement from the int prior to the calculation?
}
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11256
    
  16

well...

first, this code won't compile. you have declared 'd' twice. assuming you fix that (I made the second one a 'g'), your assumption of what d/g becomes is wrong. Here is my code:

here is the output:

C:\slop>java -cp . Sequen
16
7
7

C:\slop>


so, your second comment is wrong. when we hit the "d = --e * f" line, originally e is 2, and f is 7.

the pre-increment operator says "ok, decrement e FIRST - that makes e equal to 1. NOW finish the line... so 1 * 7 is 7".

Then we come to "g = e-- * f". When we get here, we say "ok, i have a post-fix operator. So, get the value of e, which is 1. I'll remember that, and increment e, making it 2. Using that value I remember, finish the line. I remember e was 1, so multiply that by 7, so I set g to 7".
[ February 04, 2008: Message edited by: Fred Rosenberger ]

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Jay Taylor
Greenhorn

Joined: Jan 31, 2008
Posts: 6
Let me re-word my question... I failed to comment out a line last time or to fully explain my question. Sorry.

Why does g=14 in the code below?

class Sequen
{
public static void main(String args[])
{
int a = 2;
int b = 7;

int e = 2;
int f = 7;

int c = a *++b;

//int d = --e *f;

int g = e-- *f;

System.out.println(c);
//System.out.println(d);
System.out.println(g);
}
}
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11256
    
  16

Jay,

Please start using the 'code' tags when you post code. it preserves the formatting, and makes your code easier to read. you can click the litle "Instant UBB Code" buttons below where you enter your text to have them pop in, then paste your code between them.

Second, you may want to read some of these, in particular the 'isolate the details' and 'avoid red herrings'. For code as small as this, it's not a big deal, although you would have gotten an answer faster if you had been more clear in your original post.

I'm not trying to be mean at all - i'm trying to help you use our site better.

now, to answer your question. Let's chop out all the stuff that doesn't matter:


So, why is g 14, you ask? when we get to the assignment, java runs like this:

"ok, 'e' has a post-fix decrement operator. So, i need to FIRST figure out what e is - that's 2. i'll remember that, since i'll use it later. now i need to decrement e by one, making it 1. Ok, now i need to multiply that value i remember by whatever 'f' is - 7. So, i remember that value was 2, and 2 * 7 is 14."
 
Don't get me started about those stupid light bulbs.
 
subject: Postfix vs Unary..