Arithmetic OPERATOR

Highest Precedence

First priority : the unary operators +, , ++, , !

e.g. total = – a;

Second : binary arithmetic operators *, / , %

e.g. total = a/b;

Third : binary arithmetic operators +,

e.g. total = a + b;

int a = 9, b = 2, c;

c = a % b;

System.out.println (“c= “+ c);

The output will be 1 (the amount left over)

Like automatic type conversions,

the type of the value produced by an arithmetic expression is:

byte à short à int à long à float à double

e.g.

int a = 9;

double b= 2.0;

c = a + b;

if variable c is not declared, so the automatic type must be double

——————————————————————–

Increment Operator (++)and DecrementOperator (–)

Means +1 or – 1

e.g.

a++; // equivalent with a = a + 1

a–; // equivalent with a = a – 1

It is different put the ++ before variable and after variable

a++;

different with

++a;

e.g.

int n=1, m=2, count;

count= n * (++m) //the value of count will be 3

because it means ‘change value of m with m+1 before using in the arithmetic expression

int n=1, m=2, count;

count= n * (m++) //the value of count will be 2

because it means ‘change value of m with m+1 after using in the arithmetic expression

the same way with

a–;

a;

2 Responses

  1. Hi Tony,

    Could you review the calculator on my site, it draws a tree for the precedence.

    thanks,
    Martin

    • Hi Martin…that’s nice teaching material, I can recommend your site (I also teach about this for Foundation Course at Flinders Uni.-SA:) Thanks

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: