0% found this document useful (0 votes)
2 views28 pages

Java Operator Precedence Explained

The document explains Java's operator precedence rules, highlighting that multiplication and division take precedence over addition and subtraction, which can be overridden by parentheses. It also includes a sample Java program that initializes an array of foot sizes, calculates the total and average, and prints the results. The program demonstrates basic array manipulation and control flow in Java.

Uploaded by

Alex O'Neill
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)
2 views28 pages

Java Operator Precedence Explained

The document explains Java's operator precedence rules, highlighting that multiplication and division take precedence over addition and subtraction, which can be overridden by parentheses. It also includes a sample Java program that initializes an array of foot sizes, calculates the total and average, and prints the results. The program demonstrates basic array manipulation and control flow in Java.

Uploaded by

Alex O'Neill
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

1

2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Note – slight change from original notes

17
18
19
20
Java has well-defined rules for specifying the order in which the operators in an
expression are evaluated when the expression has several operators. For example,
multiplication and division have a higher precedence than addition and subtraction.
Precedence rules can be overridden by explicit parentheses.
Precedence Order.
When two operators share an operand the operator with the higher precedence goes
first. For example, 1 + 2 * 3 is treated as 1 + (2 * 3), whereas 1 * 2 + 3 is treated as (1
* 2) + 3 since multiplication has a higher precedence than addition.

21
22
public static void main(String[] args) {

int[] feetSize;
int total, average;
total =0;
average =0;

feetSize = new int[4];

// populate the array


feetSize[0] = 6;
feetSize[2] = 7;
feetSize[3] = 5;
feetSize[1] = 4;

// iterate through the array


for (int loop=0; loop<[Link]; loop++){
[Link](loop +" size : " +feetSize[loop]);
total+=feetSize[loop];
}
// calculate average and output to screen
average = total / [Link];
[Link]("Average is : "+average);
}

23
24
25
26
27
28

You might also like