DEV Community

Cover image for Java Programming Language (Input/Output)
Nihal Islam
Nihal Islam

Posted on • Updated on

Java Programming Language (Input/Output)

Output

We have already seen java outputs. We can test our code by simply using java built-in print method. This method will print our desired value into the console. One simple example,

class InputOutput {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}
Enter fullscreen mode Exit fullscreen mode

Output

Hello, World!
Enter fullscreen mode Exit fullscreen mode

We can also use print instead of println.

What println does is that if I print another value below, it will create a new line. However if I use print, the next output will start where this output ends. For example,

System.out.println("Hello,");
System.out.print("My name is ");
System.out.println("Nihal.");
Enter fullscreen mode Exit fullscreen mode

Output

Hello,
My name is Nihal.
Enter fullscreen mode Exit fullscreen mode

Input

We can take input from user by Scanner class. Earlier we used String class to store String values in a variable. Scanner is also a built-in class. We will learn more about classes and objects later.

When we were creating object of String class we needed to declare String before assigning it into a variable. Here we are going to do the same thing.

Before using a java built-in class, it needs to import its package. The fun thing about IntelliJ Idea is that it automatically imports necessary packages by itself. Inside the main method, if we start writing Scanner, halfway through it will show if we want to use java built-in package java.util.Scanner. This is the package where the Scanner class has been created. By simply clicking enter will automatically imports the package.

import java.util.Scanner;

class InputOutput {
    public static void main(String[] args) {
        Scanner
    }
}
Enter fullscreen mode Exit fullscreen mode

After successfully importing the package we can use a variable name to store the object of Scanner class. Here I used input as my variable name. Next, by using = I am assigning new Scanner(System.in) to the variable.

import java.util.Scanner;

class InputOutput {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
    }
}
Enter fullscreen mode Exit fullscreen mode

new Scanner(System.in) creates an object of Scanner class and sets the location of the object into the input variable. We will call this variable, object. For now, lets just know that we can take input through this object from the console.

By using input.next(), we can take String input from the console. Whatever we write on the console will become a String data type.

import java.util.Scanner;

class InputOutput {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String name = input.next();
    }
}
Enter fullscreen mode Exit fullscreen mode

And lastly, after we are done using Scanner object, we have to close the object using input.close().

import java.util.Scanner;

class InputOutput {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String name = input.next();
        System.out.print("My name is, ");
        System.out.println(name);
        input.close();
    }
}
Enter fullscreen mode Exit fullscreen mode

After running the code, we have to write our input inside the console. Then the code will execute the output accordingly. The input output of the code will be,
Input

Nihal
Enter fullscreen mode Exit fullscreen mode

Output

My name is, Nihal
Enter fullscreen mode Exit fullscreen mode

Also we can use nextInt(), nextLong(), nextFloat() and nextDouble() to take int, long, float and double inputs.

int age = input.nextInt();
long salary = input.nextLong();
float cg = input.nextFloat();
double rate = input.nextDouble();
Enter fullscreen mode Exit fullscreen mode

Imagine having salary of long numbers :D

Appendix

Concatenation

We can use + operator between two Strings as well two variables to join the two parts into a single String. The possible use cases are given below,

String name = Nihal;
int age = 10;
System.out.println("Java " + "is a programming language");
System.out.println("My name is " + name);
System.out.println("My age is " + age);
Enter fullscreen mode Exit fullscreen mode

Output

Java is a programming language
My name is Nihal
My age is 10
Enter fullscreen mode Exit fullscreen mode

Happy Coding

Top comments (0)