Input/Output & Command Line Arguments in Java

Taking input and providing output to user is a fundamental concept of every programming language. In this tutorial, we will learn simple ways to take input from user and display output. We will also get a basic overview of Command Line Arguments in Java.

Java uses various Streams with its I/O package that helps the user to perform all the input-output operations. The package contains all the classes required for input and output operations. It consists of elements such as input, output, and stream. Java uses the concept of a stream to make I/O operations fast.

1. Printing Output in Java:

In Java, you can simply use the following syntax to send or print output to the screen.





  • System : It is a final class defined in the java.lang package.
  • out : This is an instance of PrintStream type, which is a public and static member field of the System class.
  • println() : This method in Java is used to display a text on the console. This text is passed as the parameter to this method in the form of a String.
Java Program Example to Print a String
// java program to write string data

class HashOutput {
  public static void main(String[] args) {
    System.out.println(" to ");
    System.out.print("To learn Java.");

Output :

Welcome to HashCodec, To learn Java.

  • Here, println() is the method that is used to print String on a new line in the console. It can work without arguments.
  • print() is a method that is used to print string on the same line in the console. This method only and only works with arguments, otherwise, it is a syntax error.

2. Taking User Input in Java:

Java provides different ways to get input from the user.

  • The Scanner class is used to get user input, and it is found in the java.util package.
  • It accepts multiple inputs from a file or keyboard and divides them into tokens.
Scanner Class Input Types:
  1. nextBoolean() : Reads a boolean value from the user.
  2. nextByte() : Reads a byte value from the user.
  3. nextDouble() : Reads a double value from the user.
  4. nextFloat() : Reads a float value from the user.
  5. nextInt() : Reads a int value from the user.
  6. nextLine() : Reads a String value from the user.
  7. nextLong() : Reads a long value from the user.
  8. nextShort() : Reads a short value from the user.
Java program to read float, double values form user
// java program to read float, double values form user.

import java.util.Scanner;

class Input {
  public static void main(String[] args) {

    Scanner input = new Scanner(;
    String userName;
    // float input
    System.out.print("Enter Float Value: ");
    float floatnum = input.nextFloat(); // Read user input

    // double input
    System.out.print("Enter Double Value : ");
    double doublenum = input.nextDouble();

    System.out.println("Float Value = " + floatnum);
    System.out.println("Double Value = " + doublenum);


Output :

Enter Float Value: 17.4 Enter Double Value: 200 Float Value = 17.4 Double Value = 200.0

Just like System.out, Java provides us with two other standard or default input-output streams:

  1. In Java, this is the standard input stream that is used to read characters from the keyboard or any other standard input device.
InputStreamReader input = new InputStreamReader(;
  1. System.err: In Java, this is the standard error stream that is used to output all the error data that a program might throw, on a computer screen or any standard output device.

3. Command Line Arguments in Java

The command line argument is the argument that is passed to a program during the execution of the program. It is the way to pass an argument to the main method in Java. These arguments are stored in the String type args parameter which is the main method parameter and always separated by white-space.

Example of Command Line Arguments :
class Main {
  public static void main(String[] args) {
    System.out.println("Command-Line arguments are : ");

    for (int i = 0; i < args.length; i++) {

Output :

Command - Line arguments are: 100 200 HashCodec

Compile the code
Run the code
java Main 100 200 "HashCodec"

Here, 100, 200, "HashCodec" are arguments passed to the program through the command line.