8 min read•Last Updated on June 18, 2024
Avanish Gupta
user_sophia9212
Avanish Gupta
user_sophia9212
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.
Adding or Subtracting 1 in Java
When you want to add one to a value, there are a few ways to do so in Java. For example, for int score = 0;, you can add 1 by…
score = score + 1; This may look confusing because the variable score is on both sides of the equation. However, in coding the score variable on the left is the previous value score was (aka score = 0), making the equation score = 0 + 1. This makes the new value of the score variable 1.
score += 1;
score++; All three methods above will give you the same outcome.
For subtraction, it’s similar. For int score = 0;, you can subtract 1 by…
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**)
Division in Java
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.
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:
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**
What are the values of a, b, and c after the following code executes?
int a = 1;
int b = 2;
int c = 3;
a = c;
b = b * 2;
c = 4;
A. a = 1, b = 2, c = 3
B. a = 4, b = 2, c = 3
C. a = 4, b = 4, c = 3
D. a = 3, b = 4, c = 4
Answer: D. a = 3, b = 4, c = 4
What are the values of p, q, and r after the following code executes?
int p = 3;
int q = 4;
int r = 5;
p = q;
q = q + 1;
r = r * 2;
A. p = 3, q = 4, r = 5
B. p = 4, q = 4, r = 5
C. p = 4, q = 5, r = 5
D. p = 4, q = 5, r = 10
Answer: D. p = 4, q = 5, r = 10
What are the values of x, y, and z after the following code executes?
int x = 2;
int y = 3;
int z = 4;
x = z;
y = y + 2;
z = x * y;
A. x = 2, y = 3, z = 4
B. x = 4, y = 5, z = 20
C. x = 4, y = 5, z = 4
D. x = 4, y = 3, z = 4
Answer: B. x = 4, y = 5, z = 20
What are the values of m, n, and o after the following code executes?
int m = 5;
int n = 6;
int o = 7;
m = n;
n = o;
o = m + n;
A. m = 6, n = 7, o = 13
B. m = 6, n = 6, o = 7
C. m = 6, n = 7, o = 7
D. m = 5, n = 6, o = 7
Answer: A. m = 6, n = 7, o = 13
What are the values of x, y, and z after the following code executes?
int x = 3;
int y = 4;
int z = 5;
x = x * 2;
y = y + 3;
z = z - 1;
A. x = 3, y = 4, z = 5
B. x = 6, y = 4, z = 5
C. x = 3, y = 7, z = 5
D. x = 6, y = 7, z = 4
Answer: D. x = 6, y = 7, z = 4
What are the values of x, y, and z after the following code executes?
int x = 0;
int y = 1;
int z = 2;
x = y - z;
y = x + y;
z = x - y;
A. x = -1, y = 0, z = -1
B. x = 1, y = 2, z = -1
C. x = -1, y = 1, z = 2
D. x = 0, y = 1, z = 2
Answer: A. x = -1, y = 0, z = -1
What are the values of x, y, and z after the following code executes?
int x = 3;
int y = 4;
int z = 5;
x = y / x;
y = z / y;
z = x / z;
A. x = 3, y = 4, z = 5
B. x = 1, y = 1, z = 0
C. x = 1, y = 4, z = 5
D. x = 1, y = 1, z = 1
Answer: B. x = 1, y = 1, z = 0
Reminder:
A. 5
B. 10
C. 0
Answer: C. 0
What is the result of 15 % 4?
A. 4
B. 3
C. 2
Answer: B. 3
What is the result of 100 % 7?
A. 1
B. 6
C. 2
Answer: C. 2
What is the result of 9 % 3?
A. 0
B. 3
C. 1
Answer: A. 0
What is the result of 25 % 5?
A. 0
B. 5
C. 1
Answer: A. 0
What is the result of 12 % 10?
A. 1
B. 2
C. 0
Answer: B. 2
What is the result of 7 % 10?
A. 7
B. 0
C. 10
Answer: A. 7
What is the result of 3 % 10?
A. 10
B. 0
C. 3
Answer: C. 3
What is the result of 0 % 10?
A. 10
B. 0
C. 1
Answer: B. 0
What is the result of 2 % 10?
A. 2
B. 0
C. 10
Answer: A. 2
Reminder:
int a = 8;
int b = 3;
double c = 2.0;
System.out.println(7 + a / b * c - 1);
A. 0.666666666666667
B. 9.0
C. 10.0
D. 11.5
E. 14.0
Answer: C. 10.0
What is printed when the following code segment is executed?
int a = 12;
int b = 4;
double c = 5.0;
System.out.println(3 + a / b * c - 2);
A. 16.0
B. 17.0
C. 15.0
D. 13.0
E. 12.5
Answer: A. 16.0
What is printed when the following code segment is executed?
int a = 10;
int b = 5;
double c = 3.0;
System.out.println(2 + a / b * c - 3);
A. 0
B. 6.0
C. 1.5
D. 10.0
E. 5.0
Answer: E. 5.0
What is printed when the following code segment is executed?
int a = 15;
int b = 6;
double c = 4.0;
System.out.println(1 + a / b * c - 4);
A. 5.0
B. 9.0
C. 1.0
D. 11.5
E. 10.0
Answer: A. 5.0
What is printed when the following code segment is executed?
int a = 20;
int b = 8;
double c = 6.0;
System.out.println(6 + a / b * c - 5);
A. 7.5
B. 8.0
C. 11.0
D. 13.0
E. 14.5
Answer: D. 13.0
The assignment operator is used to assign a value to a variable. It is represented by the equals sign (=) and it assigns the value on the right side of the equals sign to the variable on the left side.
Term 1 of 6
(3 * 5 / 2) / (2.0 + 8)
in Java, and why does it differ from (3 * 5 / 2) / (2 + 8)
?The assignment operator is used to assign a value to a variable. It is represented by the equals sign (=) and it assigns the value on the right side of the equals sign to the variable on the left side.
Term 1 of 6
The assignment operator is used to assign a value to a variable. It is represented by the equals sign (=) and it assigns the value on the right side of the equals sign to the variable on the left side.
Term 1 of 6
(3 * 5 / 2) / (2.0 + 8)
in Java, and why does it differ from (3 * 5 / 2) / (2 + 8)
?The int is a primitive data type in Java used to store whole numbers without decimal points. It has a fixed size and represents integer values within a specific range.
double: A primitive data type representing real numbers with decimal points.
arithmetic operators: Mathematical symbols (+, -, *, /) used to perform calculations on numeric data types.
casting: The process of converting one data type to another, such as converting an int to a double.
In Java, Double is both a wrapper class for primitive type double and also represents real numbers with decimal points. It provides useful methods for performing mathematical operations on these numbers.
intValue(): This method converts a Double value into an int value.
MAX_VALUE: This constant represents the maximum finite value that can be represented by Double.
isNaN(): This method checks if a Double value is "Not-a-Number" (NaN).
The assignment operator is used to assign a value to a variable. It is represented by the equals sign (=) and it assigns the value on the right side of the equals sign to the variable on the left side.
Arithmetic operators: These are operators used for mathematical calculations such as addition (+), subtraction (-), multiplication (*), and division (/).
Comparison operators: These are operators used to compare two values or variables, such as greater than (>), less than (<), equal to (==), not equal to (!=).
Compound assignment operators: These are shorthand versions of assignment operators combined with other arithmetic or bitwise operations, such as +=, -=, *=, /=.
The modulo operator, represented by the symbol %, returns the remainder of a division operation. It is used to determine if a number is divisible by another number.
Division Operator: Performs division between two numbers and returns the quotient.
Integer Division: Performs division between two numbers and discards any decimal part of the result.
Remainder: The amount left over after dividing one number by another.
The order in which mathematical operations should be performed to obtain the correct result. It follows the acronym PEMDAS, which stands for Parentheses, Exponents, Multiplication and Division (from left to right), and Addition and Subtraction (from left to right).
Arithmetic Operators: Symbols used in mathematical expressions to perform calculations such as addition (+), subtraction (-), multiplication (*), and division (/).
Parentheses: Symbols used in mathematical expressions to group parts that need to be evaluated first.
Exponents: A mathematical operation that represents repeated multiplication of a number by itself.
System.out.println is a Java statement used to display output on the console. It prints the specified message or value and adds a new line character at the end.
Variable: A variable is a named storage location in computer memory that holds a value. It can be used to store data that can be accessed and manipulated in a program.
Method: A method is a block of code that performs a specific task. It can take input parameters, perform operations, and return a result.
String: In Java, String refers to a sequence of characters. It is used to represent textual data and is one of the most commonly used data types in programming.