Java printf() is really useful! 🖨️ @BroCodez
Java printf() is really useful! 🖨️  @BroCodez
Uploaded December 2024 | Updated November 2026, 1 day ago
#java #javatutorial #javacourse

public class Main {
public static void main(String[] args) {

// printf() is a method used to format output

// % [flags] [width] [.precision] [specifier-character]

// [specifier-character]

String name = "Spongebob";
char firstLetter = 'S';
int age = 30;
double height = 60.5;
boolean isEmployed = true;

System.out.printf("Hello %s\n", name);
System.out.printf("Your name starts with a %c\n", firstLetter);
System.out.printf("You are %d years old\n", age);
System.out.printf("You are %f inches tall\n", height);
System.out.printf("Employed: %b\n", isEmployed);

System.out.printf("%s is %d years old", name, age);

// [.precision]

double price1 = 9.99;
double price2 = 100.15;
double price3 = -54.01;

System.out.printf("%.3f\n", price1);
System.out.printf("%.3f\n", price2);
System.out.printf("%.3f\n", price3);

// [flags]

// + = output a plus
// , = comma grouping separator
// ( = negative numbers are enclosed in ()
// space = display a minus if negative, space if positive

System.out.printf("%f\n", price1);
System.out.printf("%f\n", price2);
System.out.printf("%f\n", price3);

// [width]

// 0 = zero padding
// number = right justified padding
// negative number = left justified padding

int id1 = 1;
int id2 = 23;
int id3 = 456;
int id4 = 7890;

System.out.printf("id: %04d\n", id1);
System.out.printf("id: %04d\n", id2);
System.out.printf("id: %04d\n", id3);
System.out.printf("id: %04d\n", id4);
}
}
Java printf() is really useful! 🖨️Malloc in C is easy! 🏢Learn Java overloaded methods in 6 minutes! 🍕The Java Math class + exercises! 📐Lets code a Java SLOT MACHINE! 🎰Format specifiers are easy! 🛠️Create QR codes with Python in 4 minutes! 📱Lets code a calculator with C programming! 🖩Learn typedef in 5 minutes! 📛Learn to reshape NumPy arrays in 4 minutes! ↔️Learn Java AGGREGATION in 9 minutes! 🏫Learn the STATIC keyword in 8 minutes! 🤝
Bro Code |

Java printf() is really useful! 🖨️

SHARE TO X SHARE TO REDDIT SHARE TO FACEBOOK WALLPAPER