Table of content

In Java, there are print(), println() and printf() methods to print the statements. The print methods belong to the PrintStream class.

Depending on the type of output one of the three methods can be used.

Printing using print() method

In Java, the print() method is used to print text on the console. This method accepts a string as a parameter.

Once the text is printed on the console, the cursor remains at the end of the text. The next printing takes place from the same line. This method works fine even if we do not parse any parameter.

The syntax is

System.out.print();

The various print() methods are:

  • print(boolean b) - It prints a boolean value.
  • print(char c) - It prints a character.
  • print(char[] s) - It prints an array of characters.
  • print(double d) - It prints a double-precision floating-point number.
  • print(float f) - It prints a floating-point number.
  • print(long l) - It prints a long integer.
  • print(int i) - It prints an integer.
  • print(Object obj) - It prints an object.
  • print(String s) - It prints a string.
public static void main(String[] args) {
  int i = 50;
  System.out.print("Java Program.");
  // This line is printed after Hello User on the same line
  System.out.print("Value of integer: " + i);
}

The output is

Java Program.Value of integer: 50

Printing using println() method

The println() method is an upgraded version of the print() method. It is also used to print the text on the console. This method accepts the string as a parameter.

In this method, after the text is printed on the console the cursor remains at the starting position of the next line.

The syntax is

System.out.println();

The various println() methods are:

  • print(boolean b) - It prints boolean value.
  • print(char c) - It prints a character.
  • print(char[] s) - It prints an array of characters.
  • print(double d) - It prints a double-precision floating-point number.
  • print(float f) - It prints a floating-point number.
  • print(long l) - It prints a long integer.
  • print(int i) - It prints an integer.
  • print(Object obj) - It prints an object.
  • print(String s) - It prints a string.
public static void main(String[] args) {
  float f = 5087.029 f;
  System.out.println("Hello User.");
  // This line is printed after Hello User in next line
  System.out.println(f); // prints a floating point value
}

The output is

Hello User.
5087.029

Printing using printf() method

The printf() method is used if the user wants to print a formatted string to the console using the specified format string and arguments.

The syntax is

System.out.println(String format, Object args);
  • format - It is a string that specifies the formatting to be used.
  • args - It is an argument referenced by the format specifiers in the format string.

The format for different data types are as follows:

  • %d - for integer values
  • %f - for floating point numbers
  • %c - for character
  • %s - for string
public static void main(String[] args) {
   int i = 50;
   float x = 80.08 f;
   String str = "Java";
   System.out.printf("%d", i);
   System.out.printf("\n%f", x);
   System.out.printf("\n%s", str);
 }

The output is

50
80.08
Java
0 results
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions