james freeman

Greenhorn

Posts: 2

posted 6 years ago

Hi newbie to java programming could someone please explain how I could find the value of a at the end.

int a = 2;

a = 2 + 3 % 2 * 3 + 2 + 6

a =

Also another code how would I work out this?

int i = 10;

int j = i % 4 + 1;

Thanks very much for your help..

int a = 2;

a = 2 + 3 % 2 * 3 + 2 + 6

a =

Also another code how would I work out this?

int i = 10;

int j = i % 4 + 1;

Thanks very much for your help..

Greg Brannon

Bartender

Posts: 563

posted 6 years ago

In addition to the use of primitive variables (or simply primitives), it's an exercise in arithmetic operators and their precedence in Java.

I didn't follow the whole thread to see that it addresses all of those areas, but here's a good start:

Java Arithmetic Operators

Come back and tell us what you think the answers are and why.

I didn't follow the whole thread to see that it addresses all of those areas, but here's a good start:

Java Arithmetic Operators

Come back and tell us what you think the answers are and why.

Always learning Java, currently using Eclipse on Fedora.

Linux user#: 501795

posted 6 years ago

The easiest way to figure out what it would be at the end is to code and run it. THEN you can go back and try to figure out WHY it is what it is. You may also want to check out this, which defines the precedence.

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

james freeman

Greenhorn

Posts: 2

posted 6 years ago
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors

You should probably read the article linked to in the above posts.

Java, just like algebra, follows a very specific order of operations... Some operators have a higher precedence than others, so they go first, regardless of where they appear in the expression. In Algebra, the order is:

Parenthesis

exponents

Multiplication/Diving, in order from left to right

Addition/subtraction, in order from left to right.

In other words, anything inside a paren is done first. when all that is done, you go to the next step. Note that the rules are recursive. Once you go inside a set of parens, you look for a new set of parens, and do THAT first. once you get to the innermost set, you do all the exponents, etc.

Second, in the original expression, there is a '%' character. That is NOT division, as you show in your reply. In fact, in your first description, you seem to think it's division, and in the second, you think it's multiplication. You may want to read up on what it actually does. Look up "modulus operator".

Java, just like algebra, follows a very specific order of operations... Some operators have a higher precedence than others, so they go first, regardless of where they appear in the expression. In Algebra, the order is:

Parenthesis

exponents

Multiplication/Diving, in order from left to right

Addition/subtraction, in order from left to right.

In other words, anything inside a paren is done first. when all that is done, you go to the next step. Note that the rules are recursive. Once you go inside a set of parens, you look for a new set of parens, and do THAT first. once you get to the innermost set, you do all the exponents, etc.

Second, in the original expression, there is a '%' character. That is NOT division, as you show in your reply. In fact, in your first description, you seem to think it's division, and in the second, you think it's multiplication. You may want to read up on what it actually does. Look up "modulus operator".

posted 6 years ago

So you think that in math 2 + 3 * 3 is 15 instead of 11? If so I dread to see your report cards...

Java's mathematical operator precedence isn't much different from that of plain old math. And like in plain old math, if two operators have the same precedence then the left-one is evaluated first. So 3 % 2 * 3 will be (3 % 2) * 3.

james freeman wrote:Hi thanks for fast replay

Here is how i thought you would work out the code.

2+3 = 5

5 / 2 = 2

2 x 3 = 6

2 + 6 = 8

= 21

So you think that in math 2 + 3 * 3 is 15 instead of 11? If so I dread to see your report cards...

Java's mathematical operator precedence isn't much different from that of plain old math. And like in plain old math, if two operators have the same precedence then the left-one is evaluated first. So 3 % 2 * 3 will be (3 % 2) * 3.

SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6

How To Ask Questions How To Answer Questions