BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

System.out.println() in Java explained with examples

By Chaitanya Singh | Filed Under: java

In java, we use System.out.println() statement to display a message, string or data on the screen. It displays the argument that we pass to it.

Let’s understand each part of this statement:

  1. System: It is a final class defined in the java.lang package.
  2. out: It is an instance of PrintStream type and its access specifiers are public and final
  3. println(): It is a method of PrintStream class.

As we know that we need to create the object of a class to invoke its method. Here instead of creating the object of the PrintStream class we use System.out, it internally creates the object of PrintStream class so we can use the statement System.out.println() to invoke println() method.

Example 1: Displaying Strings using System.out.println()

Here we are displaying three Strings using System.out.println() statements.

class JavaExample
{
    public static void main(String[] args)
    {
        System.out.println("Welcome");
        System.out.println("To");
        System.out.println("Beginnersbook");
    }
}

Output: Since we are using println() method, the strings are displayed in new lines, if there is a need to display the strings in a single line, use print() method instead.

Welcome
To
Beginnersbook

Example 2: Displaying the variable values using println() method

In the above example, we simply passed the strings that we wanted to be displayed as output. However, we can do so much more with the println() method. We can pass the variable name in this method and it can read and display the value of the passed variable as output.

class JavaExample
{
    public static void main(String[] args)
    {
        int a = 10, b = 20;

        System.out.print("The product of ");
        System.out.println(a + " and " + b + " is: " + a*b);
    }
}

Output:

The product of 10 and 20 is: 200

Overloading of println() method

We learned in Overloading in Java that we can have more than one methods with the same name but different signatures.

The println() method belongs to the PrintStream class and this class has total 10 overloaded variants of println() method. The overloaded methods are invoked based on the arguments passed by the user.

Some of the overloaded methods are as follows. If user passes int to the println() method, it invokes the overloaded method with int parameter and if the passed parameter is of String type, it invokes String variant and so on.

System.out.println()
System.out.println(int)
System.out.println(double) 
System.out.println(boolean)
System.out.println(String)
System.out.println(char)

Let’s take an example:

class JavaExample
{
    public static void main(String[] args)
    {
        int num = 10;
        char ch = 'A';
        boolean bool = false;
        String s = "Beginnersbook";
        System.out.println(num);
        System.out.println(ch);
        System.out.println(bool);
        System.out.println(s);
    }
}

Output:

10
A
false
Beginnersbook

Difference between print() and println() methods

System.out.print(): This method displays the result on the screen and the cursor remains at the end of the the printed line. The next printing starts in the same line where the cursor is at. This method will only work when there is at least one parameter passed to it else it will throw an error.

System.out.println(): Like print() method, this method also displays the result on the screen based on the parameter passed to it. However unlike print() method, in this method the cursor jumps to the next line after displaying the result, which means the next printing starts in the new line. This method doesn’t throw an error if no parameters are passed in it.

Let’s take an example to see this in action.

class JavaExample
{
    public static void main(String[] args)
    {
        System.out.println("print() statements:");
        System.out.print("This ");
        System.out.print("is ");
        System.out.print("just ");
        System.out.print("a ");
        System.out.print("String ");

        System.out.println();//for new line
        System.out.println("println() statements:");
        System.out.println("This ");
        System.out.println("is ");
        System.out.println("just ");
        System.out.println("a ");
        System.out.println("String ");
    }
}

Output:

print() statements:
This is just a String 
println() statements:
This 
is 
just 
a 
String 
Learn Java Programming: Java Tutorial
❮ PreviousNext ❯

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap