Skip to main content
OCA Java Programmer 1Z0-803

Java : Use parenthesis to override operator precedence

By November 17, 2013No Comments

You may have been, like me, asleep in the mathematics classes where you learned BODMAS; but it is important and yes we do use it. The mnemonic can help you remember the order of evaluations of operators in a mathematical statement. We can use the brackets or parenthesis to help us as they are evaluated first. We may look at a simple statement such as:

2 + 3 * 5

And as we read from left to right we may think the answer is 25; we would be wrong. The answer is 17. This is because the multiplication will happen before the addition. Just look at the M and A is BODMAS the M comes before A.

 

  1. Brackets
  2. Order Of ( 2 power of 3 etc)
  3. Division
  4. Multiplication
  5. Addition
  6. Subtraction

We could rewrite the statement so it would be come 25:

(2 + 3) * 5

The contents of the brackets are evaluated first and then the multiplication. For the example we use in the JAVA OCA 1ZO-803 tutorial is to create a class to convert temperatures from Fahrenheit to Centigrade and vice – versa. To convert from Centigrade to Fahrenheit the formula is:

((t * 9) / 5) + 32

  1. temperature t * 9
  2. Divided by 5
  3. and add 32

And from Fahrenheit to Centigrade

((t – 32) * 5) / 9

  1. Temperature t – 32
  2. multiplied by 5
  3. and divided by 9

We also get back to basics with Java and have a revision of command line java. Creating the java files in vi, compiling with javac and running with java. Making sure we know about the CLASSPATH variable and how we create packages. All this done on a Raspberry Pi.

Using an IDE is great I cannot deny that but especially when we are leaning java we need to make sure that we do know how it all goes together. So if we have packages set up our CLASSPATH should point to the top of the structure and we can access the client class file like this:

java com.tup.code.TemplClient

in Windows we would use the forward slash in place of the . separator.

java com/tup/code/TemplClient