Java Basic Input and Output
Updated Jan 6, 2021 View by 1.8 K

In this article, you will learn simple ways to display output, and take input from the user.
Java Output
You can simply use System.out.println()
, System.out.print()
or System.out.printf()
to send output to standard output (screen).
System
is a class and out
is a public static
field which accepts output data. Don’t worry if you don’t understand it. Classes
, public
, and static
will be discussed in later chapters.
Let’s take an example to output a line.
class AssignmentOperator { public static void main(String[] args) { System.out.println("Java programming is interesting."); } }
When you run the program, the output will be:
Java programming is interesting.
Here, println
is a method that displays the string inside quotes.
What’s the difference between println(), print() and printf()?
print()
– prints string inside the quotes.println()
– prints string inside the quotes similar likeprint()
method. Then the cursor moves to the beginning of the next line.printf()
– it provides string formatting (similar to printf in C/C++ programming).
Example 2: print() and println()
class Output { 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"); } }
When you run the program, the output will be:
1. println 2. println 1. print 2. print
Visit this page to learn about Java printf().
To display integers, variables and so on, do not use quotation marks.
Example 3: Printing Variables and Literals
class Variables { public static void main(String[] args) { Double number = -10.6; System.out.println(5); System.out.println(number); } }
When you run the program, the output will be:
5 -10.6
You can use + operator to concatenate strings and print it.
Example 4: Print Concatenated Strings
class PrintVariables { public static void main(String[] args) { Double number = -10.6; System.out.println("I am " + "awesome."); System.out.println("Number = " + number); } }
When you run the program, the output will be:
I am awesome. Number = -10.6
Consider: System.out.println("I am " + "awesome.")
;
Strings "I am "
and "awesome."
is concatenated first before it’s printed on the screen.
Consider: System.out.println("Number = " + number)
;
The value of variable number
is evaluated first. It’s value is in double
which is converted to string by the compiler. Then, the strings are concatenated and printed on the screen.
Java Input
There are several ways to get input from the user in Java. You will learn to get input by using Scanner
object in this article.
For that, you need to import Scanner
class using:
import java.util.Scanner;
Learn more about Java import
Then, we will create an object of Scanner
class which will be used to get input from the user.
Scanner input = new Scanner(System.in); int number = input.nextInt();
Example 5: Get Integer Input From the User
import java.util.Scanner; class Input { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter an integer: "); int number = input.nextInt(); System.out.println("You entered " + number); } }
When you run the program, the output will be:
Enter an integer: 23 You entered 23
Here, input
object of Scanner
class is created. Then, the nextInt()
method of the Scanner
class is used to get integer input from the user.
To get long
, float
, double
and String
input from the user, you can use nextLong()
, nextFloat()
, nextDouble()
and next()
methods respectively.
Example 6: Get float, double and String Input
import java.util.Scanner; class Input { public static void main(String[] args) { Scanner input = new Scanner(System.in); // 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 = input.next(); System.out.println("Text entered = " + myString); } }
When you run the program, the output will be:
Enter float: 2.343 Float entered = 2.343 Enter double: -23.4 Double entered = -23.4 Enter text: Hey! Text entered = Hey!
As mentioned, there are other several ways to get input from the user. To learn more, visit: How to get user input in Java?