Java Input and Output



In this tutorial, we will learn basic ways to display output to users and take input from users.


Java Output

In Java, to send output to standard output (screen), we can use the following statement:

System.out.println();

System.out.print();

System.out.printf();

Here above,

  • System is a class
  • out is a public static field.

For public, class, and static, don't worry if you don't understand; we will see them in later chapters.


In the following example, we will use the println() method to display a string to the standard output.

public class Main {
    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

Output:

Hello World!

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

  • print() - It prints the string inside the quotes.
  • println() - It is similar to print(), it prints the string inside the quotes and also move the cursor to the begining of the next line.
  • printf() - It offers string formating (like to printf in C programming).

Example: print() and println()

In the following example, we will see the working of the print() and println() methods.

public class Main {
    public static void main(String[] args) {
        System.out.println("1. println ");
        System.out.println("2. println ");

        System.out.print("1. print ");
        System.out.print("2. print ");
    }
}

Output:

1. println 
2. println 
1. print 2. print 

Example: Printing Variables and Literals

In the following example, we will see how to use variables and literals with the println() method.

public class Main {
    public static void main(String[] args) {
        int num = 17;

        System.out.println(23);
        System.out.println(num);
    }
}

Output:

23
17

As we can see above, we didn't use quotation marks to display literals (here integers) and variables.


Example: Printing Concatenated Strings

In the following example, we will see how we can print concatenated strings.

public class Main {
    public static void main(String[] args) {
        int num = 9;
        
        System.out.println("Hello " + "World");
        System.out.println("Number = " + num);
    }
}

Output:

Hello World
Number = 9

As we can see above, we used the + operator to concatenate two strings, "Hello " and "World".

And also, we used the + operator with the line System.out.println("Number = " + num);. The first value of variable num is evaluated, then the value is concatenated to the string: "Number = ".


Java Input

Java offers various ways to get input from the user. In this tutorial, we will learn how to get input from users using the object of the Scanner class.

To use objects from the Scanner class, we need to import the java.util.Scanner package.

import java.util.Scanner;

After, we need to create an object of the Scanner class. Then, we need to use the object to take input from the user.

// instantiate an object of Scanner
Scanner input = new Scanner(System.in);

// take input from the user
int num = input.nextInt();

Example: Get Integer Input From the User

In the following example, we will see how to get an integer input from the user.

import java.util.Scanner;

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

        Scanner input = new Scanner(System.in);

        System.out.println("Enter an integer : ");
        int num = input.nextInt();
        System.out.println("You entered " + num);

        // closing the scanner object
        input.close();
    }
}

Output:

Enter an integer : 
128
You entered 128

Here, above we created an object named input of the Scanner class. And we call the nextInt() method of the Scanner class to get an integer input from the user.

Similarly, we can use nextLong(), nextFloat(), nextDouble(), and next() methods to get long, float, double, and string input respectively from the user.

Note: It is recommended to use the close() method to close the scanner object once the input is taken.


Example: Get long, float, double, and String Input

In the following example, we will see how to get long, float, double, and String input from the user.

import java.util.Scanner;

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

        Scanner input = new Scanner(System.in);

        // Getting long input
        System.out.print("Enter long: ");
        long myLong = input.nextLong();
        System.out.println("Long entered = " + myLong);

        // Getting float input
        System.out.print("Enter float: ");
        float myFloat = input.nextFloat();
        System.out.println("Float entered = " + myFloat );

        // Getting double input
        System.out.print("Enter double: ");
        double myDouble = input.nextDouble();
        System.out.println("Double entered = " + myDouble);

        // Getting String input
        System.out.print("Enter string: ");
        String myString = input.next();
        System.out.println("String entered = " + myString);
        
        // closing the scanner object
        input.close();
    }
}

Output:

Enter long: 173
Long entered = 173
Enter float: 12,4
Float entered = 12.4
Enter double: 23,454
Double entered = 23.454
Enter string: Hello
String entered = Hello


ExpectoCode is optimized for learning. Tutorials and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using this site, you agree to have read and accepted our terms of use, cookie and privacy policy.
Copyright 2020-2021 by ExpectoCode. All Rights Reserved.