Udemy logo

java iteratorJava is one of the world’s most popular programming languages.  It was first developed by James Gosling of Sun Microsystems. This exciting technology was released in 1995 and since then its use has grown by leaps and bounds. Java is ideal for the distributed environments particularly the Internet. Programmers use it to develop web based applications as well as standalone applications.  Java is an object-oriented and high level programming language. C++ developers will find it relatively easy to migrate to this platform. Java’s mission for developers is “Write Once, Run Anywhere”.   This platform is known to be fast, secure as well as reliable. Java is easy to learn and has a syntax somewhat similar to C++. Programs written in Java are compiled to machine independent byte code. Then the latter is interpreted by Java Virtual Machines(JVM) to the byte code of the platform it is using.  Important features of this language are  support for multi-threading and automatic memory management.

In this beginner’s level tutorial we walk you through the Java printf() function. We assume that you are familiar with the basics of programming. If not you may want to take this introductory course to Java programming.

The printf function is common across most programming languages, and is used to display any statement on the screen. It has multiple formatting options that can be used to format the variable or string you want to print, in various ways.

Formatting using the System.out.printf() Function

In Java the syntax of this method is as follows:

System.out.printf( “format string” [,arg1, arg2, ... ] );

The first argument is either a literal or a format specifier. Only if format specifiers are present are arguments used. Flags, precision, width and conversion characters are the format specifiers. Given below is the sequence of format specifiers.

% [flags][width][.precision]conversion-character

  1. ‘d’ is used for the different type of integers such as byte, int, short, long.
  2. ‘f’ is used for different of floating point numbers such as float and double.
  3. ‘c’ is used for character values
  4. ‘s’ is used for strings.
  5. ‘n’ is used for newline.

You can learn more about printf formatting in Java with this course.

Exampl 1: A Simple Java Program using System.out.printf()

public class sample{
public static void main(String args[]) {
System.out.printf("Welcome to this Java programming tutorial");
}
}

The keyword class defines sample to be a new type of class. The public keyword used means that the class can be accessed by all. The main() function starts the execution of the Java program. It does not return any value and is class specific. This means there can be only one instance of main.  The System.out.printf() function will print the string passed as the parameter to it. Every statement in a Java program is terminated by a semi-colon. Take this course to learn how to write your own Java programs.

Integer formatting

Suppose we declare an integer variable and initialize it to the value 1234.

System.out.printf(“%d”,num);
 System.out.printf(“%6d”,num);

This specifies the field width as 6 characters. As the integer 1234 occupies only 4 spaces, there will be 2 leading blank spaces in the output.

This is similar to the previous example. The difference is here 1234 will be output first followed by 2 trailing blank spaces.

Here the field width is 6 characters. Here instead of leading spaces there will be 2 leading zeroes.

Here the precision is 2 characters while the field width is 6 characters. Only the first 2 digits of the integer will be printed.

Floating Point Formatting

Let’s declare a floating point variable and initialize it to a floating point value in Java. Then we’ll see how we can print it out using different formatting options.

Float float1;
Float1 = 12.3456;

This will output the floating point value as it is.

This specifies the field width as 8 characters. However the number has only 6 charcters. Hence the output will be padded on the left by 2 blank spaces.

Here the precision is 6 characters. Hence maximum 6 decimal digits of the floating point number will be printed.

Here the field width is 8 characters. Precision is 4 characters. Hence maximum 4 decimal digits of the number will be printed. The output will occupy minimum 8 characters.  Since the float1’s value has only 6 characters there will be 2 leading space characters in the output.

String Formatting

Here we declare a String object and initialize it.

String Str1;

Str1 = “ HelloWorld”.

This will print the string as it is.

Here the field width is 12 characters. As Str1 has only 10 characters it will have 2 leading spaces.

This is similar to the previous example. The difference is it will have 2 trailing spaces.

Here the precision is 8 characters.  Hence maximum 8 characters of the string will be printed. As Str1’s value has 10 characters the last 2 characters will be omitted from the output.

There is another way to use printf in the java programming language. This is the java.io.PrintStream.printf() method. Its parameters are identical to that of System.out.printf(). However, it returns a PrintStream object which is an output stream. If the format parameter is null then, this method throws a NullPointerException. For other errors, in the parameters the IllegalFormatException is thrown.

The third way provided by Java to use printf() method is the  java.io.Console.printf() method. Its parameters and functioning are identical to that of System.out.printf()method. However, it returns a Console object. In the event of any errors in its parameters, this particular method throws IllegalFormatException.

Hope this tutorial was interesting and useful. Do study the given code and experiment with it. To master any programming concept, you should write your own programs and play around with them.Once you’re ready to move on to the next level, you can take this advanced Java course to learn more in depth techniques.

Page Last Updated: May 2014

Top courses in Java

Java for Beginners
Navin Reddy
4.5 (1,824)
Java SE 11 Developer 1Z0-819 OCP Course - Part 1
Tim Buchalka, Tim Buchalka's Learn Programming Academy
4.5 (4,087)
Bestseller
Learn Selenium with Java, Cucumber & Frameworks
Pavan Kumar
4.6 (8,366)
Bestseller
Modern Java - Learn Java 8 Features By coding it
Pragmatic Code School
4.5 (11,325)
Java Interview Help
Bharath Thippireddy
4.5 (1,631)

More Java Courses

Java students also learn

Empower your team. Lead the industry.

Get a subscription to a library of online courses and digital learning tools for your organization with Udemy Business.

Request a demo