Sunday, January 12, 2025
HomeComputer ScienceJava String format() Method With Examples

Java String format() Method With Examples

The String.format() method in Java is used to create formatted strings by replacing placeholders with specified values. It is part of the java.lang.String class and is very useful for formatting output in a structured manner.

Syntax:

java
public static String format(String format, Object... args)
  • format: A format string containing placeholders.
  • args: The values to replace the placeholders.

Common Placeholders in Format Strings

Placeholder Description
%s Formats strings.
%d Formats decimal integers.
%f Formats floating-point numbers.
%n Inserts a newline.
%x Formats integers in hexadecimal.
%o Formats integers in octal.
%b Formats boolean values.
%% Prints a % symbol.

Examples

1. Formatting Strings

java
public class FormatExample {
public static void main(String[] args) {
String name = "Alice";
String formattedString = String.format("Hello, %s!", name);
System.out.println(formattedString); // Output: Hello, Alice!
}
}

2. Formatting Integers

java
public class FormatExample {
public static void main(String[] args) {
int age = 25;
String formattedString = String.format("Age: %d years", age);
System.out.println(formattedString); // Output: Age: 25 years
}
}

3. Formatting Floating-Point Numbers

java
public class FormatExample {
public static void main(String[] args) {
double pi = 3.14159;
String formattedString = String.format("Value of Pi: %.2f", pi);
System.out.println(formattedString); // Output: Value of Pi: 3.14
}
}

4. Multiple Placeholders

java
public class FormatExample {
public static void main(String[] args) {
String name = "Bob";
int age = 30;
double salary = 55000.75;

String formattedString = String.format("Name: %s, Age: %d, Salary: $%.2f", name, age, salary);
System.out.println(formattedString);
// Output: Name: Bob, Age: 30, Salary: $55000.75
}
}

5. Formatting Boolean Values

java
public class FormatExample {
public static void main(String[] args) {
boolean flag = true;
String formattedString = String.format("Flag status: %b", flag);
System.out.println(formattedString); // Output: Flag status: true
}
}

6. Padding and Alignment

  • Left-align (-) and Right-align (default).
  • Add width (e.g., %10s for a 10-character-wide string).
java
public class FormatExample {
public static void main(String[] args) {
String name = "John";
System.out.println(String.format("'%10s'", name)); // Output: ' John'
System.out.println(String.format("'%-10s'", name)); // Output: 'John '
}
}

7. Hexadecimal and Octal Formatting

java
public class FormatExample {
public static void main(String[] args) {
int number = 255;
System.out.println(String.format("Hexadecimal: %x", number)); // Output: Hexadecimal: ff
System.out.println(String.format("Octal: %o", number)); // Output: Octal: 377
}
}

Key Points

  1. The String.format() method does not modify the original string; it returns a new formatted string.
  2. It is thread-safe and useful for creating complex formatted output.
  3. If invalid placeholders are used, it throws a java.util.IllegalFormatException.

The String.format() method is a powerful tool for building dynamic and readable strings in Java.

RELATED ARTICLES
0 0 votes
Article Rating

Leave a Reply

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
- Advertisment -

Most Popular

Recent Comments

0
Would love your thoughts, please comment.x
()
x