DEV Community

Cover image for Streams. System.out.format()
Tristan Elliott
Tristan Elliott

Posted on • Updated on

Streams. System.out.format()

Introduction

  • This is the fifth post in my streams series, it will be divided into two parts. Part one was on basic formatting and part two(this post) is on formatting with the format() method.

The Format Method

  • To start things off, lets first look at some code, break it down and get a better understanding of how the format method is working.
public class Root2 {
    public static void main(String[] args) {
        int i = 2;
        double r = Math.sqrt(i);

        System.out.format("The square root of %d is %f.%n", i, r);
    }
}
Enter fullscreen mode Exit fullscreen mode
  • 1) public static void main(String[] args) : this is the very important main method in Java, it is the starting point for the application. It is very important that we do not change its name, because the main method stops becoming the main method if we change its name. "public" means all classes can see this method, static means it is tied to the class and not the instance of the class and void simply states that there is no return value. Then we have the name that gives this method all of its power, "main". The JVM will look for this name to start our application. Finally we have String[] args, these are used for command line arguments and since we are not using any here we are going to skip over them.

  • 2) int i = 2 : defining a variable of type integer and assigning it the value of 2.

  • 3) double r = Math.sqrt(i) : defining a variable of type double and setting its value to Math.sqrt(i), which is just the square root of 2.

  • 4) System : as I mentioned in the previous post System is just a utility method that gives us static methods for handling standard input and output streams.

  • 5) out : this is the standard output stream that is going to display output to a location that is determined by the host environment. It is of type PrintStream and gives us access to all the methods that PrintStream has for handling byte streams. It is because of PrintStream that we have access to the format method.

  • 6) format("The square root of %d is %f.%n", i, r) : this format method comes from PrintStream and it gives us the ability to format multiple arguments based on a format string.

Format String API

  • Every method that produces a formatted output requires a format string and our format method does produce formatted data, so we need to use a format string. The general syntax for a formatted string is as follows:
%[argument_index][flags][width][.precision]conversion
Enter fullscreen mode Exit fullscreen mode

argument_index : optional, and it is a decimal integer that indicates the position of the argument

flags : optional, and are a set of characters that modify the output format.

width : optional, and is a positive decimal integer indicating the minimum number of characters to be written to the output.

precision : optional, and a non-negative decimal usually used to restrict the number of characters.

conversion : required. This the only required option and it actually the only option that is used in the example, so it goes without saying that this is very important. A conversion is just a single character indicating how the argument should be formatted. The different kind of conversions can be found here. Listed below are the explanations of the conversion types used in the example

d : formats an integer value as a decimal.

f : formats a floating point value as a decimal.

n : outputs a platform specific line terminator

  • One last thing to note is the % character, this is mandatory for all characters in the format string. Please make sure to play with the format method and really understand what it is doing because moving forward we will be using this method a lot.

Conclusion

  • Thank you for taking the time out of you day to read this blog post of mine. If you have any questions or concerns please comment below or reach out to me on Twitter.
  • Also make sure to checkout my YouTube channel for more programming tutorials

Top comments (0)