Input Output In Java

input output in java

Java Basic Input and Output

To make I/O operations quick, Java uses the idea of a stream. All the classes needed for input and output operations are included in the java.io package.

 

Input Output Streams

A data stream is a collection of data. A stream in Java is made up of bytes. It resembles a flowing stream of water, which is why it is called a stream.
Three streams are automatically created for us in Java. Each of these streams is connected to the console.
 
  • System.out: standard output stream
  • System.in: standard input stream
  • System.err: standard error stream

 

System.in:  
It reads characters from the keyboard or any other standard input device using the standard input stream.
 
System.out:
This is the common output stream that is used to display a program’s output on a display device, such as a computer screen.
 
System.err: 

This is the standard error stream, which is used to display any standard output device or the full set of error data that a program might produce.

 

Java Output

You can mainly use these syntax to take output from the console
 

Syntax:

System.out.println(); or

System.out.print(); or

System.out.printf();

Difference between println(), print() and printf()

print():

Java uses this method to display text on the console. This method receives this text as a parameter in the form of a String. With this technique, the text is printed on the console and the cursor is left where it is at the console. From right here, the subsequent printing begins.

Example code :

Run
// Java code for  print() Statement 
import java.io.*;

class Main {
	public static void main(String[] args)
	{
		System.out.print("prepinsta "); // Now all the print statement will
		System.out.print("prepinsta "); // print in the same line 
		System.out.print("prepinsta ");
	}
}

Output :

prepinsta prepinsta prepinsta

println():

In Java, a text can also be displayed on the console using this method. The text is printed on the console, and the cursor is then moved to the beginning of the subsequent line. From the following line, the next printing begins.

Example code :

Run

// Java code for  println() Statement 
import java.io.*;

class Main {
	public static void main(String[] args)
	{
		System.out.println("prepinsta "); // Now all the println statement will
		System.out.println("prepinsta "); // print in the next line 
		System.out.println("prepinsta ");
	}
}

Output :

prepinsta 
prepinsta 
prepinsta

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

printf():

As it is similar to printf in C, this method is the simplest of all. It should be noted that while printf() may accept multiple arguments, System.out.print() and System.out.println() only accept one argument each. The output in Java is formatted using this.

Example code :

// Java code for  print() Statement 
import java.io.*;

class JavaFormat
{ 
  public static void main(String args[]) 
  { 
    String data = "Hello World!"; 
    System.out.printf("Printing a string: %s\n", data); 
  } 
} 

Output :

Printing a string: Hello World!

Java Input

Java offers several methods for obtaining user input. However, you will discover how to obtain user input using an object from the Scanner class in this tutorial.
We must import the java.util.Scanner package in order to use the Scanner object.
The next step is to create a Scanner class object. We can use the object to gather user input.
 

Example code :

Run

import java.util.Scanner;  //importing of Scanner class

public class Main {
    public static void main(String[] args) {
    	
        Scanner input = new Scanner(System.in); //object creation of scanner class
    	
        System.out.print("Enter an integer number: ");
        int number = input.nextInt();  //Taking input from the user
        System.out.println("You have entered " + number);
        input.close(); // closing the scanner object
    }
}

Output :

Enter an integer number: 23
You have entered 23