Java Basic Input and Output
Complete guide to Java input/output with examples. Learn System.out.println, Scanner class, print vs println, and user input techniques.
Java Output
In Java, you can simply use System.out.println(), System.out.print(), or System.out.printf() to send output to standard output (screen).
Systemis a classoutis apublicstaticfield: it accepts output data.
1. println() vs print() vs printf()
println(): Prints the text and moves to the next line.print(): Prints the text and stays on the same line.printf(): Prints formatted strings (like C/C++).
System.out.println("Hello");
System.out.print("World");
System.out.println("!");
// Output:
// Hello
// World!2. Formatting with printf()
You can use format specifiers to format your output.
%d: Integer%f: Float/Double%s: String%n: New Line
int age = 25;
String name = "John";
System.out.printf("My name is %s and I am %d years old.%n", name, age);Java Input
Java provides different ways to get input from the user. The most common way is using the Scanner class.
1. Using Scanner Class
First, import the Scanner class.
import java.util.Scanner;Then, create an object of Scanner and use its methods.
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);
// Close the scanner
input.close();
}
}Scanner Methods
| Method | Description |
|---|---|
nextBoolean() | Reads a boolean value |
nextByte() | Reads a byte value |
nextDouble() | Reads a double value |
nextFloat() | Reads a float value |
nextInt() | Reads an int value |
nextLine() | Reads a line of text |
nextLong() | Reads a long value |
nextShort() | Reads a short value |
next() | Reads a single word |
2. Using BufferedReader (Fast I/O)
For competitive programming or reading large files, Scanner can be slow. BufferedReader is faster.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter your name: ");
String name = reader.readLine();
System.out.println("Hello, " + name);
}
}Key Takeaways
- Output: Use
System.out.println()to print text and move to a new line. Useprint()to stay on the same line. - Input: Use the
Scannerclass to read user input. - Import: You must
import java.util.Scanner;before using it.
Common Pitfalls
[!WARNING] Closing Scanner: Always close your scanner with
input.close()when you're done to prevent resource leaks.
[!WARNING] NextLine Issue: Mixing
nextInt()andnextLine()can be tricky.nextInt()reads the number but leaves the "enter" key press in the buffer, whichnextLine()might accidentally read as an empty string. Fix: Callinput.nextLine()once afternextInt()to consume the leftover newline.
Challenge
Challenge
Task:
Declare a String variable 'project' with value 'Javapedia' and print 'Welcome to Javapedia'.Java Math Class
Explore the Java Math class. Learn how to perform basic numeric operations like min, max, sqrt, abs, and random numbers.
Java Expressions, Statements and Blocks
In this tutorial, you will learn about Java expressions, Java statements, difference between expression and statement, and Java blocks with the help of examples.
