articles

home / developersection / articles / explain the 'system.out.println()' and 'system.in'

Explain the 'System.out.println()' and 'System.in'

Explain the 'System.out.println()' and 'System.in'

Ravi Vishwakarma 388 02-Jun-2024

System.out.println()

It is a Java method used to print output to the console. Here’s a detailed explanation:

 

Components of System.out.println()

System:

  • System is a final class in the java.lang package. It cannot be instantiated.
  • The System class contains several useful class fields and methods. It provides access to system resources like standard input, output, and error output streams.

out:

  • out is a static member of the System class. Specifically, it is a static field of type PrintStream.
  • PrintStream is a class that provides methods to print various data types conveniently. By default, System.out represents the standard output stream, which is typically the console.

println():

  • println is a method of the PrintStream class. It stands for "print line".
  • The println method prints the argument passed to it, and then terminates the line by writing a newline character.
  • It can print different types of data: strings, integers, characters, objects, etc.

 

How it Works

When you call System.out.println(), here’s what happens step-by-step:

Access the System class:

  • Since System is a class in the java.lang package (which is automatically imported in all Java programs), you don't need to import anything extra.

Use the out field:

  • The System class has a static field out, which is an instance of PrintStream that is connected to the console.

Invoke the println method:

  • The PrintStream instance (which System.out refers to) has a method println. When you call this method, it prints the text you pass to it, followed by a newline character.

 

Example

Here's a simple example to demonstrate:

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}
  • System: Access the System class.
  • out: Use the out field of the System class, which is a PrintStream.
  • println("Hello, World!"): Call the println method on the PrintStream instance to print "Hello, World!" followed by a newline.

 

Variants of println

The println method is overloaded to handle different types of data:

  • println(): Prints a newline.
  • println(boolean x): Prints a boolean value.
  • println(char x): Prints a character.
  • println(int x): Prints an integer.
  • println(long x): Prints a long integer.
  • println(float x): Prints a floating-point number.
  • println(double x): Prints a double precision floating-point number.
  • println(char[] x): Prints an array of characters.
  • println(String x): Prints a string.
  • println(Object x): Prints an object by calling its toString() method.

 

System.in

It is another field in the System class in Java. Here's an explanation of System.in similar to the one for System.out.println().

Components of System.in

System:

  • As mentioned earlier, System is a final class in the java.lang package that cannot be instantiated.
  • The System class provides access to system resources.

in:

  • in is a static field of the System class. Specifically, it is a public static final InputStream.
  • InputStream is an abstract class that represents an input stream of bytes.

 

How it Works

When you use System.in, it refers to the standard input stream, which by default is connected to the keyboard input in a console or command line environment.

 

Common Usage

System.in is typically used with classes like Scanner to read input from the user. Here’s how it works step-by-step:

Access the System class:

  • System is automatically imported from the java.lang package.

Use the in field:

  • The System class has a static field in, which is an instance of InputStream connected to the standard input stream.

Read Input:

  • System.in itself is an InputStream, so it provides basic byte-level input methods. However, for ease of use, it's often used with a Scanner or other higher-level classes to read various types of input like strings, integers, etc.

 

Example

Here's an example using System.in with Scanner to read a line of text from the user:

import java.util.Scanner;

public class InputExample {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);  // Create a Scanner object connected to System.in
        System.out.println("Enter your name:");
        String name = scanner.nextLine();  // Read a line of text from the user
        System.out.println("Hello, " + name + "!");
    }
}
  • System: Access the System class.
  • in: Use the in field of the System class, which is an InputStream connected to standard input.
  • new Scanner(System.in): Create a Scanner object that wraps around System.in to read user input conveniently.
  • scanner.nextLine(): Use the Scanner object to read a line of text from the user.

 

Summary

System.out.println() is a convenient way to print text and other data to the console in Java. It accesses the System class, uses the out field (a PrintStream connected to the console), and calls the println method to print the provided data followed by a newline.

System.in is a static field of the System class that represents the standard input stream, typically connected to the keyboard input in a console environment. It is an InputStream and is often used with higher-level classes like Scanner to read user input. This allows you to easily read various types of data from the standard input, enhancing the interaction of your Java programs with users.

 


Updated 02-Jun-2024

Hi, my self Ravi Vishwakarma. I have completed my studies at SPICBB Varanasi. now I completed MCA with 76% form Veer Bahadur Singh Purvanchal University Jaunpur. SWE @ MindStick | Software Engineer | Web Developer | .Net Developer | Web Developer | Backend Engineer | .NET Core Developer

Leave Comment

Comments

Liked By