⚡0 XP🔥0

Operators, Math and User Input

Lesson 3 of 15 ⏱ 5 min read ✎ 2 exercises ❓ 5-question quiz ▶ Runs Java

Arithmetic, relational, logical, assignment and increment operators, the Math class, and reading input with Scanner.

Arithmetic operators#

Operator Meaning 17 op 5
+ add (also joins strings) 22
- subtract 12
* multiply 85
/ divide (integer division for ints) 3
% remainder 2
▶ Try it YourselfJava
Ctrl + Enter to run
Output
3
2
3.4
-2
0.30000000000000004
Sum: 175
Sum: 22
Common Mistake

"Sum: " + a + b gives Sum: 175, not Sum: 22 - once the left side is a String, + joins text. Use brackets around arithmetic inside print statements.

Note

0.1 + 0.2 is not exactly 0.3 because decimals are stored in binary. For money in real applications, use long paise or java.math.BigDecimal.

Relational and logical operators#

Relational operators (==, !=, >, <, >=, <=) return a boolean. Logical operators combine booleans: && (and), || (or), ! (not). && and || short-circuit.

▶ Try it YourselfJava
Ctrl + Enter to run
Output
Eligible for placement: true
Scholarship: false
Not eligible: false
false

Assignment, increment and the ternary operator#

▶ Try it YourselfJava
Ctrl + Enter to run
Output
x = 4
i=7 j=5 k=7
Pass

The Math class#

java.lang.Math is available without import:

▶ Try it YourselfJava
Ctrl + Enter to run
Output
12.0
1024.0
7.5
42
5.0 4.0 5
3.141592653589793
Hypotenuse: 5.00
Dice is between 1 and 6: true

Reading input with Scanner#

java.util.Scanner reads from the keyboard (System.in). Import it, create one Scanner, and call the method for the type you want:

Method Reads
nextInt() an int
nextDouble() a double
nextLong() a long
next() one word
nextLine() the rest of the current line
nextBoolean() true/false
hasNextInt() is there another int to read?
▶ Try it YourselfJava
Ctrl + Enter to run
Output
Name: Marks in 3 subjects: 
Priya Soni, your average is 85.00
हिंदी में समझें

Scanner से input पढ़ने के लिए पहले import java.util.Scanner; लिखें, फिर Scanner sc = new Scanner(System.in); बनाएँ। number के लिए nextInt()/nextDouble(), एक शब्द के लिए next() और पूरी line के लिए nextLine() का उपयोग करें।

Common Mistake

After nextInt(), the Enter key (newline) is still waiting in the input. A following nextLine() then returns an empty string! Call an extra sc.nextLine(); to consume it, or read everything with nextLine() and convert using Integer.parseInt(...).

▶ Try it YourselfJava
Ctrl + Enter to run
Output
117 -> Siddharth Rathore

Operator precedence (high to low)#

  1. () and postfix x++
  2. unary: ++x, -x, !, casts
  3. * / %
  4. + -
  5. < > <= >= instanceof
  6. == !=
  7. &&
  8. ||
  9. ?:
  10. = += -= ...

Summary#

  • / between ints truncates; % keeps the sign of the left operand; String + joins left to right.
  • && and || short-circuit; the ternary ?: picks between two values.
  • Math has sqrt, pow, abs, max, round, random...
  • Scanner reads typed input; watch out for the leftover newline after nextInt().

Practice Exercises

Write your code, then press Check. Your program is tested automatically.

1

BMI calculator

Read weight (kg) and height (m) with Scanner and print the BMI = weight / height² with 1 decimal, like BMI: 22.9. Test input 68 1.72.

Test input:
68 1.72
Expected output:
BMI: 23.0
2

Compound interest

Read principal, annual rate (%) and years. Print the amount after compound interest A = P(1 + r/100)^t using Math.pow, with 2 decimals: Amount: 13310.00. Test input 10000 10 3.

Test input:
10000 10 3
Expected output:
Amount: 13310.00

Quick Quiz

Q1.

What is -17 % 5 in Java?

Q2.

Which Scanner method reads a whole line?

Q3.

int x = 5; x += 3 * 2; - what is x?

Q4.

What does Math.ceil(4.1) return?

Q5.

In a != 0 && b / a > 2, what happens when a is 0?