Question about a Java decrement assignment statement equation?
-
Hey everyone, I have a quick question about decrement operator equation I was given for homework. Here's the problem: Assume: int x = 6, y=9; y=x--*x; x=? y=? I keep getting x=5 and y=36, but when I enter it into Java, Java says it is x=5 and y=30...I have no idea why. Please can someone explain to me! And does anyone have a more systematic way of solving equations like these with increment and decrement operators? Thanks!
-
Answer:
(I recant the part about confusing PRE with POST, that's not the problem at all. The problem is, these fonts are so damned small, it's difficult to read and distinguish between "x" and "y" sometimes!) Here's how it works. 1. x is equal to 6 2. y is equal to 9 3. Reference x as the FIRST part of a multiplicand, which is 6 4. Subtract 1 from x, which sets it to 5 5. Reference x as the SECOND part of a multiplicand, which is now 5 6. 6 * 5 is 30, assign to y Don't worry about it, it IS confusing. Which is why I always recommend to people NOT to use the increment or decrement operator inside an expression like that. Always use it as a statement by itself (with some exceptions, where the meaning and results are obvious). That way, you avoid this kind of confusion.
Derek Kwok at Yahoo! Answers Visit the source
Other answers
// The problem is to do with the order in which the statement "y = x--*x" is executed. public class MinusMinusOperatorTest { /*** * Program: MinusMinusOperatorTest.jav… by Chris Clarke * Created: 07.01.2012 * Purpose: int x = 6, y=9; * y=x--*x; * x=? y=? ***/ public static void main (String[] args) { int x = 6, y = 9; System.out.println("x = 6, y = 9"); y = x--*x; System.out.println("y = x--*x"); //x=? y=? System.out.println("x = "+x+", y = "+y); System.out.println(); x = 6; y = 9; System.out.println("x = 6, y = 9"); y = --x*x; System.out.println("y = --x*x"); //x=? y=? System.out.println("x = "+x+", y = "+y); } } //Copy and paste the above code. Save, compile and run. //Note: there's a difference whether the "--" comes before or after the variable, "x". //Just like BODMAS which we learned at school (Brackets, Of, Divide, Multiply, Add, Subtract), //to help us remember the order of things, so "--x" always takes place immediately, whereas "x--" happens later, //after it has recognised the other "x" which, it knows, is 6. I hope this makes sense, and answers your //question.
x-- means access x, then decrement it. There is another alternative, --x (that is, the -- comes before the variable) which means decrement, then access. That's how Java is supposed to work. That said, it may be that because x appears twice in that expression, the SECOND time it is accessed, it has already been decremented. That would result in y being set to 30. You could check this by reversing the two, i.e.: y=x*x--; and seeing if you get the 36 answer you were expecting. Either way, my recommendation is that in the real world, you should just put the x-- outside the expression. The readability is worth the extra line.
Student programming services at helpwithprogramming.com
Related Q & A:
- how to Create a Java Package from MATLAB Code?Best solution by Stack Overflow
- How to load a Java web app in the terminal?Best solution by stackoverflow.com
- How to create a java applet?Best solution by Stack Overflow
- Salary question on a job application?Best solution by Yahoo! Answers
- What is a good resume objective statement?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.