Java โ Formatted Output with printf() & Math Utilities
Welcome to Java โ Formatted Output with printf() & Math Utilities in our Java Complete Masterclass! System.out.printf() ยท String.format() ยท Format Specifiers (%d, %f, %.2f, %s, %-15s) ยท java.lang.Math Library (sqrt, pow, abs, max, min, round, random)
In Java software engineering, mastering Formatted Output with printf() & Math Utilities is fundamental to writing robust, object-oriented, memory-safe, and high-performance applications. Java's strongly typed system and JVM architecture ensure maximum safety and reliability across platforms.
- Master core Java syntax, keywords, and class design for Formatted Output with printf() & Math Utilities
- Understand JVM heap/stack memory mechanics and type safety guarantees
- Implement clean production-grade Java classes, interfaces, and methods
- Avoid common memory leaks, NullPointerExceptions, and concurrency bugs
Understanding Formatted Output with printf() & Math Utilities equips you to architect scalable enterprise applications, leverage Java standard libraries, and write clean, maintainable code accepted by top tech engineering teams.
Java source files follow strict naming rules: public class Main must live in a file named Main.java inside standard package structures (e.g., com.ourcompiler.demo).
Mechanism: Java Bytecode & JVM Execution. Topic: System.out.printf().
public class Main {
public static void main(String[] args) {
// 1. Formatted Output with printf()
String productName = "MacBook Pro M3";
int stockQuantity = 42;
double unitPrice = 199999.956;
System.out.println("--- Table Formatting with printf ---");
System.out.printf("%-20s %-10s %-15s%n", "ITEM NAME", "QTY", "PRICE (INR)");
System.out.println("--------------------------------------------------");
System.out.printf("%-20s %-10d โน%,.2f%n", productName, stockQuantity, unitPrice);
System.out.printf("%-20s %-10d โน%,.2f%n", "Magic Mouse", 120, 8500.00);
System.out.printf("%-20s %-10d โน%,.2f%n", "USB-C Adapter", 300, 1900.50);
// 2. Math Library Utilities
System.out.println("
--- java.lang.Math Utilities ---");
System.out.println("Square Root of 144 : " + Math.sqrt(144));
System.out.println("2 raised to 8 (2^8): " + Math.pow(2, 8));
System.out.println("Absolute of -50.5 : " + Math.abs(-50.5));
System.out.println("Max of (120, 85) : " + Math.max(120, 85));
System.out.println("Round 99.6 : " + Math.round(99.6));
// 3. Random Number Generation between 1 and 6 (Dice Roll)
int diceRoll = (int)(Math.random() * 6) + 1;
System.out.println("Random Dice Roll (1-6): " + diceRoll);
}
}
--- Table Formatting with printf ---
ITEM NAME QTY PRICE (INR)
--------------------------------------------------
MacBook Pro M3 42 โน199,999.96
Magic Mouse 120 โน8,500.00
USB-C Adapter 300 โน1,900.50
--- java.lang.Math Utilities ---
Square Root of 144 : 12.0
2 raised to 8 (2^8): 256.0
Absolute of -50.5 : 50.5
Max of (120, 85) : 120
Round 99.6 : 100
Random Dice Roll (1-6): 4
public class CircleGeometryEngine {
public static void main(String[] args) {
double radius = 7.5; // Circle radius in cm
// Area = PI * r^2
double area = Math.PI * Math.pow(radius, 2);
// Circumference = 2 * PI * r
double circumference = 2 * Math.PI * radius;
System.out.println("=== Circle Geometric Calculations ===");
System.out.printf("Radius : %.2f cm%n", radius);
System.out.printf("Area : %.4f sq.cm%n", area);
System.out.printf("Circumference : %.4f cm%n", circumference);
}
}
Java's strong type system and automatic Garbage Collection ensure zero dangling memory pointers and rock-solid platform stability.
- Comparing Objects using
==instead of.equals(). - Ignoring NullPointerException checks when calling methods on reference variables.
- Catching raw
Throwableor swallowing exceptions without logging. - Failing to close I/O resources or database connections (use
try-with-resources). - Modifying a Collection while iterating over it without using an
Iterator.
Write a Java program that demonstrates Formatted Output with printf() & Math Utilities inside our online Java compiler. Test your implementation by clicking the "Run in IDE" button above!
โ Question: What is the primary role of Formatted Output with printf() & Math Utilities in Java?
Answer: It provides structured Java object-oriented mechanisms for System.out.printf(), streamlining enterprise software development.
- System.out.printf() ยท String.format() ยท Format Specifiers (%d, %f, %.2f, %s, %-15s) ยท java.lang.Math Library (sqrt, pow, abs, max, min, round, random)
- Java follows the "Write Once, Run Anywhere" (WORA) paradigm powered by the JVM.
- Utilize type-safe classes, interfaces, generic collections, and functional streams.