Skip to content

Latest commit

 

History

History
59 lines (43 loc) · 2.35 KB

File metadata and controls

59 lines (43 loc) · 2.35 KB

Java Format Specifiers Tutorial

Introduction

Java format specifiers are used in the printf and format methods to control the formatting of text strings. They allow you to define the layout of the output by specifying how various data types should be represented.

Common Format Specifiers

  1. %s - String:

    • Used to format strings.
    • Example: System.out.printf("Hello, %s!", "World");
  2. %d - Integer:

    • Used to format integers.
    • Example: System.out.printf("The value is %d", 42);
  3. %f - Floating-point:

    • Used to format floating-point numbers.
    • Example: System.out.printf("The value is %.2f", 3.14159); (specifies two decimal places)
  4. %c - Character:

    • Used to format characters.
    • Example: System.out.printf("The first letter is %c", 'A');
  5. %b - Boolean:

    • Used to format booleans.
    • Example: System.out.printf("The condition is %b", true);

Width and Precision

  • Width: Specifies the minimum number of characters to be written. It ensures that the output is at least a certain length.

    • Example: %5s (ensures a minimum width of 5 characters for strings)
  • Precision (for floating-point numbers): Specifies the number of decimal places.

    • Example: %.2f (displays a floating-point number with two decimal places)

Padding

  • Left-justification: Flags like %-10s will left-align the output within the specified width.

  • Zero-padding: Flags like %05d will pad an integer with zeros to meet the specified width.

Argument Index

You can use argument indices to specify the order of the variables in the formatted string. For example: %2$s refers to the second argument.

Examples

String name = "John";
int age = 25;
double salary = 50000.75;

// Basic formatting
System.out.printf("Name: %s, Age: %d, Salary: %.2f\n", name, age, salary);

// Width and Precision
System.out.printf("Name: %-10s, Age: %5d, Salary: $%.2f\n", name, age, salary);

// Argument Index
System.out.printf("Age: %2$d, Name: %1$s, Salary: %3$.2f\n", name, age, salary);

Conclusion

Java format specifiers provide a powerful way to control the appearance of output. Understanding how to use them allows you to create neatly formatted and organized text in your Java programs. Experiment with different specifiers and combinations to achieve the desired output for your specific needs.