• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

How to Use the Print() Method in Java

July 31, 2023 //  by Talha Malik

In Java, the “print()” method plays a key role in retrieving the desired output. It is such that printing the applied code functionalities need to be analyzed at the developer’s end to achieve the desired outcome. Java provides multiple methods for printing the code features based on the user’s requirements from time to time.

How to Use the Print() Method in Java?

The printing in Java can be carried out via the following three approaches:

  • “print()” Method.
  • “println()” Method.
  • “printf()” Method.

Approach 1: Applying the “print()” Method in Java

The “print()” method prints the provided text without a line break on the console. Let’s overview the code demonstration of this method:

public class PrintJava {
public static void main(String[] args) {
  System.out.print("This is a boy");
  System.out.print("This is a Man");
}}

In this code, the “print()” method prints the given two statements on the console.

Output

As seen, the statements are returned without any line break.

However, utilizing an escape character “\n” with this method places a line break, as follows:

Approach 2: Applying the “println()” Method in Java

The “println()” method prints the given text with a line break on the console which is demonstrated in the following code:

public class PrintJava {
public static void main(String[] args) {
  System.out.println("This is a boy");
  System.out.println("This is a Man");
}}

According to this snippet of code, the “println()” method accumulates the given two statements one by one and prints them.

Output

This outcome implies that the given statements are displayed with a default line break appropriately.

Approach 3: Applying the “printf()” Method in Java

The “printf()” method is used with a format specifier to print the values comprising various data types. This method is used in this code example with the “int” and “float” types:

public class PrintJava {
public static void main(String[] args) {
  int a = 3;
  float b = 4.5f;
  System.out.printf("The value of a is %d", a);
  System.out.printf("\nThe value of b is %f", b);
}}

In this block of code:

  • Initialize the stated integer and float values.
  • After that, apply the “printf()” method with the “%d” format specifier to print the defined integer value.
  • Lastly, the “printf()” method is used with the “%f” format specifier to return the initialized float value.

Output

From this output, it can be verified that both the values are returned accordingly.

Conclusion

The printing in Java can be carried out via the “print()” method, the “println()” method, or the “printf()” method. These methods print the applied functionalities without a line break, with a line break and with a format specifier, respectively.

Category: Java

Previous Post: « What is Meant by ! in Java
Next Post: How to Use toDegrees() Method in Java »

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

How to Use Optional.ofNullable() Method in Java

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact