Formatted Output in Java using printf()

Sometimes in programming, it is essential to print the output in a given specified format. Most users are familiar with the printf function in C. Let us discuss how we can Formatting Output with printf() in Java in this article.

Formatting Using Java Printf()

printf() uses format specifiers for formatting. There are certain data types are mentioned below:

i). For Number Formatting

The number itself includes Integer, Long, etc. The formatting Specifier used is %d.

Below is the implementation of the above method:


Output
10,000

ii). For Decimal Number Formatting

Decimal Number Formatting can be done using print() and format specifier %f .

Below is the implementation of the above method:


Output
3.141593 3.142 3.14

iii). For Boolean Formatting

Boolean Formatting can be done using printf and ( ‘%b’ or ‘%B’ ) depending upon the result needed.

Below is the implementation of the above method:


Output
true TRUE false FALSE

iv). For Char Formatting

Char Formatting is easy to understand as it need printf() and Charracter format specifier used are ‘%c’ and ‘%C’.

Below is the implementation of the above method:


Output

v). For String Formatting

String Formatting requires the knowledge of Strings and format specifier used ‘%s’ and ‘%S’.

Below is the implementation of the above method:


Output
geeksforgeeks GEEKSFORGEEKS GFG GFG

vi). For Date and Time Formatting

Formatting of Date and Time is not as easy as the data-type used above. It uses more than simple format specifier knowledge can be observed in the example mentioned below.

Below is the implementation of the above method:


Output
Current Time: 11:32:36 Hours: 11 Minutes: 32 Seconds: 36 11:32:36 am 198 198000000 +0000

Note: System.out.format() is equivalent to printf() and can also be used.

Other Methods for Formatting

1. Formatting using DecimalFormat class

DecimalFormat is used to format decimal numbers.

Below is the implementation of the above method:


Output
Without fraction part: num = 123 Formatted to Give precision: num = 123.46 appended zeroes to right: num = 123.456700 formatting Numeric part : num = 00123.46 your Formatted Dream Income : $23,456.79

2. Formatting dates and parsing using SimpleDateFormat class

This class is present in java.text package.

Below is the implementation of the above method:


Output
Formatted Date : 24-01-2022 Parsed Date : Sat Feb 18 00:00:00 UTC 1995
Pankaj Kumar Like Article -->

Please Login to comment.

Similar Reads

PrintWriter printf(Locale, String, Object) method in Java with Examples

The printf(Locale, String, Object) method of PrintWriter Class in Java is used to print a formatted string in the stream using the given Locale. The string is formatted using specified format and arguments passed as the parameter. Syntax: public PrintWriter printf(Locale locale, String format, Object. args) Parameters: This method accepts two mand

2 min read PrintWriter printf(String, Object) method in Java with Examples

The printf(String, Object) method of PrintWriter Class in Java is used to print a formatted string in the stream. The string is formatted using specified format and arguments passed as the parameter. Syntax: public PrintWriter printf(String format, Object. args) Parameters: This method accepts two mandatory parameter: format which is the format ac

2 min read PrintStream printf(String, Object) method in Java with Examples

The printf(String, Object) method of PrintStream Class in Java is used to print a formatted string in the stream. The string is formatted using specified format and arguments passed as the parameter. Syntax: public PrintStream printf(String format, Object. args) Parameters: This method accepts two mandatory parameter: format which is the format ac

2 min read PrintStream printf(Locale, String, Object) method in Java with Examples

The printf(Locale, String, Object) method of PrintStream Class in Java is used to print a formatted string in the stream using the given Locale. The string is formatted using specified format and arguments passed as the parameter. Syntax: public PrintStream printf(Locale locale, String format, Object. args) Parameters: This method accepts two mand

2 min read Console printf(String, Object) method in Java with Examples

The printf(String, Object) method of Console class in Java is used to write a formatted string to the output stream of the console. It uses the specified format string and arguments. It is a convenience method. Syntax: public Console printf(String fmt, Object. args) Parameters: This method accepts two parameters: fmt - It represents the format of

2 min read Cin-Cout vs Scanf-Printf

Regular competitive programmers face common challenge when input is large and the task of reading such an input from stdin might prove to be a bottleneck. Such problem is accompanied with “Warning: large I/O data”. Let us create a dummy input file containing a line with 16 bytes followed by a newline and having 1000000 such lines, making a file of

3 min read Java IO : Input-output in Java with Examples

Java brings various Streams with its I/O package that helps the user to perform all the input-output operations. These streams support all the types of objects, data-types, characters, files etc to fully execute the I/O operations. Before exploring various input and output streams lets look at 3 standard or default streams that Java has to provide

7 min read Output of Java Program | Set 1

Difficulty Level: Rookie Predict the output of the following Java Programs.Program 1: Java Code // filename Main.java class Test < protected int x, y; >class Main < public static void main(String args[]) < Test t = new Test(); System.out.println(t.x + &quot; &quot; + t.y); >> Output: 0 0 In Java, a protected member is accessible in all cl

3 min read Output of Java Program | Set 2

Predict the output of the following Java programs. Question 1: Java Code package main; class Base < public void Print() < System.out.println("Base"); >> class Derived extends Base < public void Print() < System.out.println("Derived"); >> class Main < public static void DoPrint(Base o) < o.Print(); >public static void main(Str

3 min read Output of Java Program | Set 3

Predict the output of the following Java Programs: Example1: Java Code // filename: Test.java class Test < // Declaring and initializing integer variable int x = 10; // Main driver method public static void main(String[] args) < // Creating an object of class inside main() Test t = new Test(); // Printing the value inside the object by // above cre

3 min read Output of Java program | Set 23 (Inheritance)

Prerequisite: Inheritance in Java 1) What is the output of the following program? Java Code public class A extends B < public static String sing() < return "fa"; >public static void main(String[] args) < A a = new A(); B b = new A(); System.out.println(a.sing() + " " + b.sing()); >> class B < public static String sing() < retu

3 min read Output of Java Programs | Set 54 (Vectors)

Prerequisite : Vectors in Java Basics 1. What is the Output Of the following Program Java Code import java.util.*; class demo1 < public static void main(String[] args) < Vector v = new Vector(20); System.out.println(v.capacity()); System.out.println(v.size()); >> Output: 20 0 Explanation: function - int capacity( ) Returns the capacity of the vect

6 min read Java Tricky Output Questions

Question 1: What will be the Output of the below code: public class A < public static void main(String[] args) < if (true) break; >> Choices: a) Nothing b) Error Answer: b) Error Reason: Break statement can only be used with loop or switch. So, using break with if statement causes "break outside switch or loop" error. Question 2: What will be the

3 min read Output of Java programs | Autoboxing and Unboxing 3 min read Fast Output in Java

While doing problems in various coding platforms in some questions we end up with (TLE). At that point of time even if we use fast input then also, sometimes (TLE) remains in Java. At that time if we use fast output with fast input it can reduce the time taken. The fast output is generally used when we have to print numerous items which is shown la

3 min read XML Output Factory in Java StAX

StAX provides various classes to create XML stream readers, writers, and events by using the XMLInputFactory, XMLOutputFactory, and XMLEventFactory classes. In this article we're going to study about XML Output Factory . The class javax.xml.stream.XMLOutputFactory is a root component of the Java StAX API. From this class you can create both an XMLS

3 min read Output of Java Program | Set 4

Predict the output of following Java Programs: Question 1 // file name: Main.java class Base < protected void foo() <>> class Derived extends Base < void foo() <>> public class Main < public static void main(String args[]) < Derived d = new Derived(); d.foo(); >> Output: Compiler Error foo() is protected in Base and default in Derived. Default a

2 min read Redirecting System.out.println() Output to a File in Java

System.out.println() is used mostly to print messages to the console. However very few of us are actually aware of its working mechanism. We can use System.out.println() to print messages to other sources too, not just restricting it to the console. However, before doing so, we must reassign the standard output by using the following method of Syst

2 min read Output of Java Program | Set 6

Difficulty level : Intermediate Predict the output of following Java Programs. Program 1: class First < public First() < System.out.println("a"); >> class Second extends First < public Second() < System.out.println("b"); >> class Third extends Second < public Third() < System.out.println("c"); >> public class MainCl

2 min read Output of Java Program | Set 7

Difficulty level : Intermediate Predict the output of following Java Programs. Program 1 : public class Calculator < int num = 100; public void calc(int num) < this.num = num * 10; >public void printNum() < System.out.println(num); >public static void main(String[] args) < Calculator obj = new Calculator(); obj.calc(2); obj.printNum(); >> Option

4 min read Input/Output from external file in C/C++, Java and Python for Competitive Programming

In Competitive Programming, most of the time we need to enter input for checking our code manually. But it would become cumbersome if we have a questions like Graphs, strings, or bulky entry of input data because it will definitely time out or the chances of typing incorrect data would be increased. We can easily get rid of problem by simply saving

4 min read Output of Java programs | Set 10 (Garbage Collection)

Prerequisite - Garbage Collection in Java Difficulty level : Intermediate In Java, object destruction is taken care by the Garbage Collector module and the objects which do not have any references to them are eligible for garbage collection. Below are some important output questions on Garbage collection. Predict the output of following Java Progra

4 min read Output of Java Program | Set 11

Predict the output of following Java programs: Question 1 : public class Base < private int data; public Base() < data = 5; >public int getData() < return this.data; >> class Derived extends Base < private int data; public Derived() < data = 6; >private int getData() < return data; >public static void main(String[] args) < Derived myData = new

4 min read Output of Java Programs | Set 12

1) What is the output of the following program? Java Code public class Test implements Runnable < public void run() < System.out.printf("%d", 3); >public static void main(String[] args) throws InterruptedException < Thread thread = new Thread(new Test()); thread.start(); System.out.printf("%d", 1); thread.join(); System.out.pri

5 min read Output of Java programs | Set 13 (Collections)

Prerequisite - Collections in Java 1) What is the output of the following program? Java Code import java.util.*; public class priorityQueue < public static void main(String[] args) < PriorityQueue<Integer> queue = new PriorityQueue<>(); queue.add(11); queue.add(10); queue.add(22); queue.add(5); queue.add(12); queue.add(2); while (queue.

3 min read Input/Output from external file in C/C++, Java and Python for Competitive Programming | Set 2

Prerequisites : Input/Output from external file in C/C++, Java and Python for Competitive Programming In above post, we saw a way to have standard input/output from external file using file handling. In this post, we will see a very easy way to do this. Here we will compile and run our code from terminal or cmd using input and output streams. Win

3 min read Output of Java program | Set 15 (Inner Classes)

Prerequisite :- Local inner classes , anonymous inner classes 1) What is the output of the following java program? public class Outer < public static int temp1 = 1; private static int temp2 = 2; public int temp3 = 3; private int temp4 = 4; public static class Inner < private static int temp5 = 5; private static int getSum() < return (temp1 + temp2

3 min read Output of Java program | Set 12(Exception Handling)

Prerequisites : Exception handling , control flow in try-catch-finally 1) What is the output of the following program? public class Test < public static void main(String[] args) < try < System.out.printf("1"); int sum = 9 / 0; System.out.printf("2"); >catch(ArithmeticException e) < System.out.printf("3"); >catch(Exce

3 min read Output of Java program | Set 17

1) What is the output of following program? public class Test < private static float temp() < public static float sum = 21; return(--(sum)); >public static void main(String[] args) < Test test = new Test(); System.out.println(test.temp()); >> a) 21 b) 20 c) Compilation error d) Runtime error Ans. (c) Explanation: static variables are associated w

2 min read Output of Java program | Set 18 (Overriding)

Prerequisite - Overriding in Java 1) What is the output of the following program? class Derived < protected final void getDetails() < System.out.println("Derived class"); >> public class Test extends Derived < protected final void getDetails() < System.out.println("Test class"); >public static void main(String[] args) < Derive