Math is a class that provides common mathematical operations:
Power and root
Logarithm
Max, Min
Trigonometry
Random
…
Program.java
public class Program { public static void main(String[] args) { double n = 5; double power = 4; double nPower = Math.pow(n, power); System.out.println(n + " to the " + power + "th power = " + nPower ); }}
double a = 3.75, b = 2.5;int p = 13, q = 4;double c = p / q;double d = (double) p / q;double e = (double) (p / q);System.out.println(c);System.out.println(d);System.out.println(e);
Double → Integer
double a = 3.75, b = 2.5;int p = 13, q = 4;int r = a / b;int s = (int) a / b;int t = (int) (a / b);System.out.println(r);System.out.println(s);System.out.println(t);
Scanner input = new Scanner(System.in);System.out.print("What is your name? ");String name = input.nextLine();System.out.print("What is your age? ");int age = input.nextInt();System.out.println("Hello, " + name);System.out.println("You are " + age + " years old");
Includes locale-specific grouping characters (thousands)
-
Left-justified.
8
Eight characters in width, right justified
08
Eight characters in width, with leading zeroes as necessary.
.3
Three places after decimal point.
10.3
Ten characters in width, right justified, with three places after decimal point.
String name = "Ani";int age = 6;double saving = 1234567.89012345;System.out.printf("Name: %s %n", name);System.out.printf("Name: %10s %n", name);System.out.printf("Name: %-10s %n", name);System.out.printf("Age: %d years old %n", age);System.out.printf("Age: %2d years old %n", age);System.out.printf("Age: %02d years old %n", age);System.out.printf("Saving: Rp %f %n", saving);System.out.printf("Saving: Rp %.2f %n", saving);System.out.printf("Saving: Rp %12.2f %n", saving);System.out.printf("Saving: Rp %5.2f %n", saving);System.out.printf("Saving: Rp %,.2f %n", saving);