🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
In this article, we will demonstrate the use of printf() method in Java.
Java printf() method overview
Well, the printf() method is not only there in C, but also in Java. This method belongs to the PrintStream class. It’s used to print formatted strings using various format specifiers.
Syntax
Following are the syntaxes available for the printf() method:
System.out.printf(string);
System.out.printf(format, arguments);
System.out.printf(locale, format, arguments);Format Specifiers
Let’s look at the available format specifiers available for printf() method:
%c character
%d decimal (integer) number (base 10)
%e exponential floating-point number
%f floating-point number
%i integer (base 10)
%o octal number (base 8)
%s String
%u unsigned decimal (integer) number
%x number in hexadecimal (base 16)
%t formats date/time
%% print a percent sign
\% print a percent sign
Note: %n or \n are used as line separators in printf().
Escape Characters
Following are the escape characters available in printf():
\b backspace
\f next line first character starts to the right of current line last character
\n newline
\r carriage return
\t tab
\\ backslashNumber Formatting Example
public class Main{
public static void main(String []args){
int x = 20;
System.out.printf("Formatted output is: %d %d%n", x, -x);
float y = 2.28f;
System.out.printf("Precision formatting upto 4 decimal places %.4f\n",y);
float z = 3.147293165f;
System.out.printf("Precision formatting upto 2 decimal places %.2f\n",z);
}
}Output:
Formatted output is: 20 -20
Precision formatting upto 4 decimal places 2.2800
Precision formatting upto 2 decimal places 3.15String Formatting Example
public class Main{
public static void main(String []args){
System.out.printf("%s %s!%n","Hello","World");
System.out.printf("%s\f%s!%n","Hello","World!");
System.out.printf("%s\\%s!%n","Hello","World!");
System.out.printf("%s %S!%n","Hello","World");
}
}Output:
Hello World!
HelloWorld!!
Hello\World!!
Hello WORLD!Boolean Formatting Example
public class Main{
public static void main(String []args){
System.out.printf("%b%n", false);
System.out.printf("%b%n", 0.5);
System.out.printf("%b%n", "false");
}
}Output:
false
true
trueTime Formatting Example
import java.util.Date;
public class Main{
public static void main(String []args){
Date date = new Date();
System.out.printf("%tT%n", date);
System.out.printf("H : %tH, M: %tM, S: %tS%n",date,date,date);
}
}Output:
13:48:39
H : 13, M: 48, S: 39Date Formatting Example
import java.util.Date;
public class Main{
public static void main(String []args){
Date date = new Date();
System.out.printf("%s %tB %<te, %<tY", "Current date: ", date);
System.out.printf("%1$td.%1$tm.%1$ty %n", date);
System.out.printf("%s %tb %<te, %<ty", "Current date: ", date);
}
}Output:
Current date: April 24, 202224.04.22
Current date: Apr 24, 22My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment