Foundations
Hello, Numbers!
You're helping a friend split a restaurant bill. The total is $47.50 and there are 4 people. You need to calculate each person's share: 47.50 / 4 = 11.875, round to $11.88.
Print a single number
The simplest thing we can do - tell the computer to show us a number.
Number.java
Replay: real traced execution (multi-file project)
public class Number {
public static void main(String[] args) {
System.out.println(42);
System.out.println(100);
System.out.println(-7);
}
}
public static void main(String[] args)
1public class Number {2 public static void main(String[] args) {3 System.out.println(42);4 System.out.println(100);5 System.out.println(-7);6 }output42 100 -7
The computer echoes back exactly what we ask for.
println
In Java, `System.out.println(...)` displays the value to the screen.
Basic arithmetic
Now let's do some math. We can add, subtract, multiply, and divide.
Arithmetic.java
Replay: real traced execution (multi-file project)
public class Arithmetic {
public static void main(String[] args) {
System.out.println(10 + 5);
System.out.println(10 - 5);
System.out.println(10 * 5);
System.out.println(10 / 5);
System.out.println(10 % 3);
}
}
public static void main(String[] args)
1public class Arithmetic {2 public static void main(String[] args) {3 System.out.println(10 + 5);4 System.out.println(10 - 5);5 System.out.println(10 * 5);6 System.out.println(10 / 5);7 System.out.println(10 % 3);8 }output15 5 50 2 1
operators
Arithmetic operators: `+` add, `-` subtract, `*` multiply, `/` divide, `%` remainder.
Grouping with parentheses
What if we want to control the order of operations? Use parentheses.
Grouping.java
Replay: real traced execution (multi-file project)
public class Grouping {
public static void main(String[] args) {
System.out.println(2 + 3 * 4);
System.out.println((2 + 3) * 4);
System.out.println(100 / (5 + 5));
}
}
public static void main(String[] args)
1public class Grouping {2 public static void main(String[] args) {3 System.out.println(2 + 3 * 4);4 System.out.println((2 + 3) * 4);5 System.out.println(100 / (5 + 5));6 }output14 20 10
Without parentheses: 2 + 3 * 4 gives 14 (multiply first).
With parentheses: (2 + 3) * 4 gives 20 (add first).
precedence
Multiplication and division happen before addition and subtraction, unless you use parentheses.
A real calculation
Let's calculate the area of a rectangle with sides 7 and 5.
Area.java
Replay: real traced execution (multi-file project)
public class Area {
public static void main(String[] args) {
System.out.println(7 * 5);
}
}
public static void main(String[] args)
1public class Area {2 public static void main(String[] args) {3 System.out.println(7 * 5);4 }output35
Complex expression
Combine everything: multiple operations, nested parentheses.
Complex.java
Replay: real traced execution (multi-file project)
public class Complex {
public static void main(String[] args) {
System.out.println((10 + 20) * (30 - 15) / 5);
System.out.println(((2 + 3) * (4 + 5)) - 10);
System.out.println(100 / 10 / 2);
}
}
public static void main(String[] args)
1public class Complex {2 public static void main(String[] args) {3 System.out.println((10 + 20) * (30 - 15) / 5);4 System.out.println(((2 + 3) * (4 + 5)) - 10);5 System.out.println(100 / 10 / 2);6 }output90 35 5
The computer follows the same math rules you learned in school.