aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Basic arithmetic problem. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Basic arithmetic problem." Watch "Basic arithmetic problem." New topic
Author

Basic arithmetic problem.

Tommaso Nuccio
Ranch Hand

Joined: Dec 11, 2006
Posts: 66
Hi there,

I have a problem to understand this:


the output is 14.

Now my problem here is that LINE 1 can be written as
c = c * a + b
Here now "*" has a higher priority as "+"
and the result must be
c = (2 * 3) + 4
c = 10

Why is it 14?

Or does
c *= a + b;
c = c * a + c * b;
c = c * (a + b);
?

Thanks in advance.


Ciao,<br /> Tommaso<br /> <br />~*~*~*~<br />There are 10 types of people, those who understand binary and those who don't.
Srikanth CS
Greenhorn

Joined: Apr 10, 2007
Posts: 9
the compund assignment operators have the lowest precedence of all the operators in javal allowin the expression on the right -hand side to be evaluated before the assignment
so c * = a+b;
will be x =((c)*(a+b))
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
This is from the Java Language Specification 15.26.2.

A compound assignment expression of the form E1 op = E2 is equivalent to
E1 = (T )((E1) op (E2)), where T is the type of E1, except that E1 is evaluated only once.
Tommaso Nuccio
Ranch Hand

Joined: Dec 11, 2006
Posts: 66
When I compiled I got 14, but I always thought it is the other way!!

That's ok, now I know it BEFORE the exam.

Thank you very much.
Meena R. Krishnan
Ranch Hand

Joined: Aug 13, 2006
Posts: 178
c = c * a + b; returns 46. Any thoughts?
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
If c is 14, then c*a + b is 14*3 + 4 = 42 + 4 = 46.
Meena R. Krishnan
Ranch Hand

Joined: Aug 13, 2006
Posts: 178
Oh yes. I didn't realize that I had a line above this line where c gets assigned to 14. Thanks.
 
jQuery in Action, 2nd edition
 
subject: Basic arithmetic problem.