Java input output

In this, tutorial we will learn how to take input from a user and how to print output in java


In java to print output we use these three statements:



System: is a class in the java.lang package.

out: is a static member of the System class and it is an instance of so we can say it’s  a reference  variable

print:  it is a print method It prints string inside the quotes

What is the different between println, printf,print:


 The println(“…”) method prints the string   inside the quotes “…” and moves the cursor to a new line or it start printing next statement in a new line


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


The print(“…”) method instead prints just the string inside the quotes”…”, but does not move the cursor to a new line. Or it starts printing the next statement on the same line


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


it provides string formatting (similar to printf in C/C++ programming). It’s used to print formatted strings using various format specifiers.

How to print variable:

Let int i is an integer variable which contains value 2

int i = 2;

We print i like this


Here you Notice that we do not use the quotation marks

System.out.println(“i”) ;  ❌this is wrong

See full example:

class Variables {
    public static void main(String[] args) {
        Double number = -10.6;

Note: To display integers, variables and so on, we don’t use quotation marks.


It’s very interesting to learn how to take input from the user

Let’s understand

To take input from user we use object of scanner class

Note: To use Scanner class first we have to import java.utill.Scanner package

How to create an object of the scanner and take input:

// 1st create an object of Scanner
Scanner input = new Scanner(;

// 2nd take input from the user
int number = input.nextInt();

In scanner class there are different method for different input

For example

To take input as integer we use nextInt() method of scanner class


For long =  nextLong();

For float = nextFloat();

For double = nextDouble();

String = next();

At the end

It is a good habit to close scanner object

To this, we use the close() method after taking input

See following example:

import java.util.Scanner;

class Input {
    public static void main(String[] args) {
        Scanner input = new Scanner(;
        // 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 text: ");
        String myString =;
        System.out.println("Text entered = " + myString);
Enter float: 2.343
Float entered = 2.343
Enter double: -23.4
Double entered = -23.4
Enter text: Hey!
Text entered = Hey!


Important Link