0% found this document useful (0 votes)
8 views3 pages

Java Arithmetic Operators Explained

The document outlines the arithmetic operators used in Java, including addition, subtraction, multiplication, division, and modulus, along with their precedence rules. It explains how integer division and modulus work, provides examples of mathematical functions from the Maths class, and discusses type casting between data types. Additionally, it introduces the structure of a complete Java program and mentions the necessary software for development.

Uploaded by

abraham.demsis
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views3 pages

Java Arithmetic Operators Explained

The document outlines the arithmetic operators used in Java, including addition, subtraction, multiplication, division, and modulus, along with their precedence rules. It explains how integer division and modulus work, provides examples of mathematical functions from the Maths class, and discusses type casting between data types. Additionally, it introduces the structure of a complete Java program and mentions the necessary software for development.

Uploaded by

abraham.demsis
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Computer Science

ARITHMETIC OPERATORS
JETS only uses the following:

operator meaning

+ addition

- subtraction

* multiplication

/ division

% modulus

Divison of two int values produces an int result:

int x = 214;
int y 7;
int z x/y;

z will have the int value 30. The modulus operator returns the remainder after integer division
thus after:

int x = 214;
int y 7;
int z = x%y;

z will have the value 4.


PRECEDENCE
For example, does the statement:

int x = 23 % 5 + 2

mean:

int x (23 % 5) + 2

or

in t x = 23 % (5 + 2) ••• ?

This is not one of those things you can safely ignore and hope for the best since the answer to the
first is 5 and the second 2.

Precedence tells us which operators are done first when there are no brackets. You are surely
familiar with an expression like 23 + 5 * 2 from maths. The multiplication is done before the
addition, it has higher precedence.

The order in Java is %, * and / are done before + and -. If a combination of %, * and / appears in
the same expression, then they are evaluated from left to right:

int x = 23 % 5 * 2

49
Computer Fundamentals

leaves x with the value 6 (and not 3). Of course, as in maths, we can force parts of expressions to
be evaluated first by using parentheses (that's brackets to you and me).

int x = 23 % (5 * 2)

leaves x with the value 3.

EXERCISE 2.3
Evaluate the following statements, stating whether parentheses are necessary:
double x = 23.0 / (5.0 * 2.0);
int x = 9 + 6 - 2 * 8;
intx=9%3* (5/7);
double x = -23.0 - (-5.0 + 2.0)

MATHEMATICAL FUNCTIONS
••••••••••••••
Other mathematical functions are specified in the Maths class, they are:

method use
Returns the absolute value of its argument (if x < 0 then
double, int abs (x)
returns x " -1), x may be double or int.

Returns x raised to the power y. pow( 16.0, .5) returns


double pow (x, y) 4.0 while pow( 2.0,10.0) returns 1024.0, for example.
Operates on double values.

double sin(x) Returns the sine of x, where x is in radians.

double cos (x) Returns the cosine of x, where x is in radians.

Rounds x to the nearest integer. Returns a long type


which you can convert to int, if required, as in:
long round(x)
int i = (int) [Link](3.45)

(i now has the value 3).

Rounds x to the nearest integer not greater than x.


floor( 9.5) returns 9.0, floor( -9.5) returns -10.0.
double floor(x) Returns a double value which can be converted to int if
required, as in:

int f = (int) [Link](x);

CASTING
Means converting from one data type to another. Placing the primitive type in front of the right
hand side of an assignment (as in the last two examples) is known as a cast.

so
Computer Science

Students are expected to know how to use the cast operator. Generally speaking you can always
convert a type that uses less bytes or has a smaller range of values into one that has more bytes or
stores a greater range of values:

int k = 450;
double x = k; II k gets converted to a double

This is an 'implicit' cast, it is done automatically. lmpicit type conversion also occurs in mixed
mode arithmetic:

double x 20.5 * 5;

However, beware:

int x = 23;
int y = 5;
double z = x/y;

z has the value 4.0 because, in assignments, the right hand side is evaluated first and in this case,
performs integer division. The following will give the expected result since it then becomes a
mixed mode expression (x converted to double first):

int x = 23;
int y = 5;
double z = (double) x/y;

In many cases you use a cast to convert a 'bigger' type to a 'smaller' one, accepting the risk that
precision could be lost:

int y = 120;
byte x = (byte) y;

No loss of data, but:

int y = 210;
byte x = (byte) y;

A byte only holds values up to + 127 so you get an unexpected result. Java does not warn you
because you used a cast - the compiler assumes you know what you are doing!

PROGRAM STRUCTURE
Let's look at the structure of a complete program. If you want to try this out you will need some
Java software on your computer. For this book (and the IB Computer Science, in general) we have
used the Java Development Kit (JDK) from Sun Microsystems and the BlueJ development
environment (both free):

Software Version Available for free download from:

Java Development Kit 1.3 [Link]

BlueJ 1.2 [Link]

51

You might also like