0% found this document useful (0 votes)
63 views6 pages

Description Operators Associativity: Parentheses

This document introduces basic operators in Java, including: - Arithmetic operators like addition, subtraction, multiplication, division, and modulus. Division of integers truncates remainders, while division of doubles returns decimals. - Modulus uses the % sign and returns the remainder of a division. - Operator precedence follows rules like multiplication before addition. Parentheses can be used to alter order of operations. - Examples demonstrate the behavior of different operator types using the Java interactions pane.

Uploaded by

nav16
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)
63 views6 pages

Description Operators Associativity: Parentheses

This document introduces basic operators in Java, including: - Arithmetic operators like addition, subtraction, multiplication, division, and modulus. Division of integers truncates remainders, while division of doubles returns decimals. - Modulus uses the % sign and returns the remainder of a division. - Operator precedence follows rules like multiplication before addition. Parentheses can be used to alter order of operations. - Examples demonstrate the behavior of different operator types using the Java interactions pane.

Uploaded by

nav16
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
You are on page 1/ 6

INTRODUCTION TO OPERATORS IN JAVA

Basic operators:

Description
Operators
Associativity
Parentheses

()
L to R
multiplication, division, modulus
* / %

L to R as they appear
addition, subtraction
+ -

L to R as they appear
What is integer division?

An int is a whole number. When you divide with integers, the remainder is truncated (done away
with). You are left with just the whole number.

Example:

7/2 = 3

What is dividing by double?

5/2 = 2

14 / 3 = 4

When you divide by a double, the answer will be a double.


a double, the answer will be a double.

Example: 7/2 = 3
15/4 = 3

What is modulus?
remainder.

7.0/2 =

3.5

So when one number is an int and one is

7/2.0 = 3.5

15.0/4 = 3.75

Modulus is the remainder operator. You will divide and then the answer is the

7/2 = 3
7%2 = 1

answer is 1

(1 is left over from dividing 7/2)


Do not use a calculator. It is not the decimal number from dividing.
7%2 = 1

answer is 1

(1 is left over from dividing 7/2)


Do not use a calculator. It is not the decimal number from dividing.

2 7
7
-6____
1

Open Dr. Java. At the bottom left, click on the tab that says Interactions Pane.
Type each of these into the interactions pane and record the result.

32/ 3

32.0 / 3

14/5

14.0 / 5

18 / 4

18.0 / 4

What did you observe about dividing with integers?

What did you observe when dividing with a decimal (float value)

Modulus Operator:

Type these into the interactions pane and record the result.

Modulus is the % sign and it is the remainder from dividing.

32 % 3

14 % 5

18 % 4

18 % 4

12 % 5

Description
Operators
Associativity
Parentheses

()
L to R
multiplication, division, modulus
* / %

L to R (equal precedence as they appear left to right)


addition, subtraction
+ -

L to R (equal precedence as they appear left to right)


Work these out (show your work.

Then type them into the interactions pane to check your work.

Example:

3*6-4/2

3* 6 = 18
4/2 = 2
18-2 = 16

6/3*2

((4+9) * 2+3) * 2) + 1

2 + 3 * 2 + 15 / 5

12 + 3 % 4 + 1

Go to the following website: Practice Java problets at:


http://problets.org/user/f13/luella/java/

You might also like