Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Can anyone explain this code?

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..
 
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
lowercase baba
Posts: 13074
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
james freeman
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

and for the second one

10 x 4 = 2 + 1 = 3


thanks very much for your help
 
fred rosenberger
lowercase baba
Posts: 13074
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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".
 
Sheriff
Posts: 22684
128
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Those cherries would go best on cherry cheesecake. Don't put those cherries on this tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic