DEV Community

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

Posted on • Updated on

Streams. System.out.print()

Introduction

  • This is the fourth post in my streams series, it will be divided into two parts. Part one(this part) will be on basic formatting and part two will be on formatting with the format() method. I have created a YouTube video version, so please make sure to check that out as well.

Formatting

  • Streams that implement formatting are instances of either, Character Streams, Byte Streams, PrintWritter or PrintStream. Now in this series we have already seen streams of type character and byte, but what are these print classes?

PrintWriter

  • This classes' main job is to print formatted representations of objects to output text streams and it implements all of the methods found in PrintStream. However, for this tutorial our main focus will be on PrintStream.

PrintStream

  • In the documentation for PrintStream it says, "A PrintStream adds functionality to another output stream, namely the ability to print representations of various data values conveniently.". While I know that was not entirely helpful, but I believe it roughly translates to this, " any output stream that is of type PrintStream will have added functionality(methods) that will allow the output stream to print representations of various data types. This is what helps us with formatting. Speaking of formatting, there are two main levels of formatting.

1) print and println : these methods format individual values in a predetermined way.

2) format : this method allows us to format any number of values based on a format string. This will be covered in part 2 of this blog post

print and println

  • First I will display some code and then break it down into its parts.
public class Root {
    public static void main(String[] args) {
        int i = 2;
        double r = Math.sqrt(i);

        System.out.print("The square root of ");
        System.out.print(i);
        System.out.print(" is ");
        System.out.print(r);
        System.out.println(".");

        i = 5;
        r = Math.sqrt(i);
        System.out.println("The square root of " + i + " is " + r + ".");
    }
}
Enter fullscreen mode Exit fullscreen mode

1) public static void main(String[] args) : public means that any class has access to this method. The static modifier means that this method belongs to the class and not the object, so we don't have to instantiate an object to access it. Void is a return type and it means that this method returns nothing. "main" is the name of the method, but in Java, main is a very special method. When a Java program starts running, it needs to start somewhere, a Java program will always start executing inside of a main method. We could change the name of the file and or the name of the class but we can not change the name of the main method. The main method is identified by the JVM through its name, if its name is changed, then it loses its status of starting point. String[] args just contains the supplied command line arguments as an array of strings. Unlike the main method, we can change the name of the args to whatever we want.

  • 2) int i = 2 : this is just us declaring an integer in Java and assigning its value to 2. An integer is just a primitive type in Java

  • 3) double r = Math.sqrt(i) : here we are first declaring the variable r to be a double, which is another primitive type in Java. Then we use the Math class and call the method sqrt(i), which will give us the square root on the int i.

  • 4) System.out.print("The square root of ") : while this code may look simple and I am sure you have used it without even thinking much about it. We are going to break down what System, out and print are actually doing

System : the main goal of System is to provides us with classes and methods for handling standard input/output streams. With that being said lets dive a little deeper into the documentation. The first thing that we should notice is that System can not be instantiated. This is because the constructor of System is private, now why would you want to make the constructor of a class private? Well there are actually a number of reasons, creating a singleton, limiting the number of instances created, prevent subclasses or to make a utility class. In the case of System the constructor is private because it is a utility class. When creating a utility class there are 3 main things that you do.

  • 1) declare the class final, so that no extensions can be made.

  • 2) declare its constructor private(no instance creation)

  • 3) provide public static utility methods.

  • If you look at the documentation of System you will notice that it has all three of these conditions. So to make a long story short, System is a Utility class that is non-extendable/non-instantiable and provides us with static methods for handling standard input/output streams.

out : out is a field on the System class. A field is described as being either a class, interface, or enum with an associated value. "out" is also the actual output stream and we do not have to open it, as it is already done for us. "out" also has a predefined destination for its stream, this destination is determined by the host environment. For us the determined destination is the eclipse console. The documentation of out states that it is a static class of type PrintStream. This is very good because PrintStream's main goal is to provide streams with extra functionality. So since "out" is of type PrintStream, we have access to all the print() and println() methods that PrintStream provides.

print() : in PrintStream the print method is overloaded, what this means is that there are many methods with the same name but they all have different parameters. This defines separate method signatures. The print(String s) method does exactly what you would expect, it prints a String. However, if you want to go a little deeper into the details, this is what it does. The String characters are converted into bytes according to the platform's default character encoding, for us the encoding is Unicode and then these bytes are passed to the output stream, which are finally "printed" to the defined location. All prints work the same way, regardless of what kind of parameters they receive. The print() class handles all of the encoding behind the scenes for us.

Recap : so to just recap everything, we use the utility class in Java called System and inside System we access the field called "out". "out" is the actual output stream that we are using and it has a predefined destination determined by the host environment, it is also a static class of type PrintStream. PrintStream gives the "out" class access to all of the PrintStream methods. We then use the print() method to handle all the encoding behind the scenes and print out our characters to the predefined destination.

  • This is the first level of formatting stream output provided to us. This level of formatting gives us very little control of what we format.

  • This marks the end of the first part of formatting streams. Please be on the look out for part two of formatting where we look at formatting with the format method.

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)