Select Page

The print() method in Java

The print() method is used to display text output on the console. The print() method is part of the Java API. The term, API, stands for Application Programmer Interface.

nanadwumor

May 27, 2023
The print method in java language

The print() method is used to display text output on the console. The print() method is part of the Java API.

The term, API, stands for Application Programmer Interface. The Java API is a collection of prewritten classes and methods for performing specific operations.

These classes and methods are available to the programmer when coding his application.


Join other Subscribers on our YouTube channel and enjoy daily pogramming tutorials.


Example of usage of print() method

Output

The print() method in Java

Explanation of above statement

The above statement displays the text “Thanks for visiting www.villagecoder.com always”.

Parameters the print() method accepts

The print() method accepts any parameter the programmer may wish to print out on the output screen.

Program demonstrates System.out.print

Output

The print() method in java

Recommended


Variables and Literals in Java

Variables and Literals in Java

Variables are used in many situations. For Instance, game applications use variables to collect...


System

System is a class. It is found in java.lang.package. A class in Java contains methods and fields.

A class is used to construct objects in Java. Note that Java is an object oriented programming language. That’s, to code in java, you create objects.

And these objects are created from classes. So you see, classes are some sort of blueprint or plan from which you create your items or objects. This is similar to how a building constructor uses his building plan (the class) on paper to actually build a house (the object)

Where is the System class?

The System class is part of the Java API. This implies, you can call it anytime in your program if needed. System is a class that performs operations on system level.

Fields in System class

As said earlier, a class in Java has fields and methods (or functions). The fields store important information about the class (or objects created from the class).  One example of the fields of the System class is out.

what is the out field?

Mostly, fields are primitive values. Example of primitive values are int (for integer), float (for floating point number), double (another floating point number), boolean (true or false values) etc. However, a field in Java can also be an object. Example is a String.

In the above statement, the field is called nameOfWebsite. It’s a String variable. Strings are objects but not primitive values. This implies, Strings have fields and methods on their own.

So you see, a field (which is an object) has fields and methods too. Don’t be confused ! In simple terms, if the field of a class is an object (eg. a String), then it has its own fields and methods you can call.

In the above statement, the String field is initialized to the value “villagecoder”. Because it’s a String, we can call one of its methods to perform operation on the value “villagecoder”. 

Example, the statement nameOfWebsite.charAt(5) will print out g because g is the 5th character in the String villagecoder. charAt() is a method of the String class. Note that in Java, counting begins from 0, not 1 so g falls at the 5th position.

Now, is the out field primitive or an object?

out is an object. This means out itself was created from another class. This class is called PrintStream class. This might sound confusing at a glance. The practice of creating an object from one class and using the object as a field in another class is called aggregation in Java. 

out is public and static. public means it can be accessed by code outside the class. out is static means it belongs to the class but not instance of the class.

This implies that the class System can call  its static field, out, straightaway without the need to create an instance (object) of System before calling the field, out. That is why we write:

System.out without creating any instance of the System class.

Methods of the out object

We have already established that, in Java, objects have both fields and objects which they can call when necessary. One of the methods of  out (which serves as a field for System class) is the print() method.

In the above statement, the String field is initialized to the value “villagecoder”. Because it’s a String, we can call one of its methods to perform operation on the value “villagecoder”. 

Example, the statement nameOfWebsite.charAt(5) will print out *g* because *g* is the 5th character in the String *villagecoder*. charAt() is a method of the String class. Note that in Java, counting begins from 0, not 1 so *g* falls at the 5th position.

Putting it all together

So why don’t programmers type just System.print() but System.out.print() ? In other words, why wasn’t the print() method made a method of the System class but rather made a method of another class called PrintStream.

The System class has a number of functions including taking inputs and printing out data. Putting all these functions or methods under System would make it quite cumbersome. 

Due to that, the brilliant designers of the Java language decided to group methods that perform similar functions under one umbrella and give them names.

Example, System methods that print out data are grouped together and given a class name PrintStream. To use PrintStream class, an object has to be created. One example of a PrintStream object is out, which is already created. Within the PrintStream class, we have methods such as print, println, printf, etc. Thus, the out object can call all these methods.

Example :

System.out.print()
System.out.println()
System.out.printf() etc

System functions that accept inputs from outside sources are grouped together and given a class name called InputStream. One object of InputStream is called in which is a field of the System class, written as, System.in.

The print() method

The print method prints text on the console and keeps cursor on the same line. In the above example, all the print() commands will print on a single line. This explains why  space is left as the last character in the strings. This ensures that the texts are not glued together. 

We urge you to read this article to know the difference : Differences between print() and println() methods.


Share this article


You May Also Like…

double data type in Java

double data type in Java

The double data type is also a floating point number. It is called double because it has a double-precision decimal...

The print and println methods in Java

The print and println methods in Java

The print() and println() methods are Java's predefined methods used to display text output on the console. These...

float data type in Java

float data type in Java

It is a floating point number. It has single precision data type. It stores a floating point number of 7 digits of...