πŸ“š

Β >Β 

πŸ’»Β 

Β >Β 

βž•

1.3 Expressions and Assignment Statements

3 min readβ€’november 16, 2020

Peter Cao

Peter Cao


AP Computer Science AΒ πŸ’»

130Β resources
See Units

1.3: Expressions and Assignment Statements

Basic Math in Java

Now, we can start working more with these data types. In this section, we're going to focus on the two numeric types: int and double.
For addition, subtraction, and multiplication, equations can be input as you would input them into your calculator. Variables are used to assign the answers to the correct equation.
int a = 1 + 2; double b = 1.0 + 3.5; int c = 3 - 5; double d = 24 * 1.0 a = 3 b = 4.5 c = -2 d = 24.0
The equal sign, which is the assignment operator, can be used to change the value of the variable or other expressions. It evaluates the expression first and stores the final value in the variable. If an operation has two integers, then the result is an integer. If an operation has at least one double, then the result is a double. There is also a modulo operator, denoted by %. When a user inputs a % b, the program reports the integer remainder of a divided by b, where a and b are both integers and b is positive. We will only talk about modulo with positive values of a because once a is negative, the math becomes much more complicated. A few examples are below:
int a = 4 % 2; int b = 10 % 3; int c = 3 % 5; a = 0 (4 = 2 * 2 + **0**) b = 1 (10 = 3 * 3 + **1**) c = 3 (3 = 0 * 5 + **3**)
Things get a bit more complicated with division. Remember that operations with two integers return an integer, and if there is at least one double, it returns a double. Here is an example of each:
int a = 5 / 2; double b = 5 / 2.0 a = 2 b = 2.5
Integer division returns only the whole number quotient, while the double division returns the actual quotient as a decimal.

Order of Operations in Java

Java follows the regular order of operations that you learned in algebra, with the inclusion of modulo with multiplication and division. Here is a table of the order of operations with operators from highest to lowest precedence:
https://firebasestorage.googleapis.com/v0/b/fiveable-92889.appspot.com/o/images%2F-fVYqVS7qCqky.png?alt=media&token=5b1c381a-ccce-4c86-b278-a93ce86a8864

Courtesy of Stack Overflow

Here is some practice with order of operations. Remember the difference between double division and integer division. Find the value of each variable in the following:
int a = 3 + 4 / 3 * (4-1); double b = (3 * 5 / 2) / (2.0 + 8); int c = 4 % 3 + 2 * 5 / 4; int d = (3 * 5 / 2) / (2 + 8);
    Solution:
a = 3 + 4 / 3 * (4 - 1) = 3 + 4 / 3 * 3 = 3 + 1 * 3 = 3 + 3 = **6** b = (3 * 5 / 2) / (2.0 + 8) = (15 / 2) / (10.0) = (7) / (10.0) = **0.7** c = 4 % 3 + 2 * 5 / 4 = 1 + 2 * 5 / 4 = 1 + 10 / 4 = 1 + 2 = **3** d = (3 * 5 / 2) / (2 + 8) = (15 / 2) / (10) = (7) / (10) = **0**
Browse Study Guides By Unit
βž•Unit 1 – Primitive Types
πŸ“±Unit 2 – Using Objects
πŸ–₯Unit 3 – Boolean Expressions & if Statements
πŸ•ΉUnit 4 – Iteration
βš™οΈUnit 5 – Writing Classes
⌚️Unit 6 – Array
πŸ’ΎUnit 7 – ArrayList
πŸ’»Unit 8 – 2D Array
πŸ–²Unit 9 – Inheritance
πŸ–±Unit 10 – Recursion
πŸ™Exam Reviews

Fiveable
Fiveable
Home
Stay Connected

Β© 2023 Fiveable Inc. All rights reserved.


Β© 2023 Fiveable Inc. All rights reserved.