Java
Input and Output: Basics & Best Practices
![]() |
Java IO basics |
Introduction
:
In Java,
performing repetitive tasks efficiently is crucial. Instead of writing the same
code multiple times, we use methods that encapsulate reusable
logic. These methods reside in classes, which are further organized
into packages. This guide explores Java packages, importing, and
handling input/output operations.
Packages
in Java
A package in
Java is a collection of related classes and sub-packages, similar to folders in
a computer. It promotes reusability and helps organize code efficiently. Java
provides built-in packages, such as:
- java.lang – Provides fundamental
classes (e.g., String, Math).
- java.util – Includes utility
classes like Scanner, ArrayList.
- java.net – Used for networking
applications.
Importing
a Package
To use
all classes within a package, we import it using:
import
java.util.*;
This
imports all classes from java.util, though
a more optimized approach is importing only the required class.
Importing
a Class
If only a
specific class is needed, import it directly:
import
java.util.Scanner;
This way,
we avoid unnecessary memory consumption.
Reading
User Input Using Scanner Class
The Scanner class in java.util is widely used for reading user input. It
provides various methods to handle different data types.
Example:
Reading User Input
import
java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner input = new
Scanner(System.in);
String username =
input.nextLine();
System.out.println(username);
input.close();
}
}
Input:
Ram
Output:
Ram
Input
Methods in Scanner
The Scanner class provides methods to read different data
types:
![]() |
Input Types - Java |
Reading a Character
Java
doesn’t have a dedicated method to read a character using Scanner. Instead, we extract the first character from a
string:
import
java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner input = new
Scanner(System.in);
char ch =
input.next().charAt(0);
System.out.println(ch);
input.close();
}
}
Input:
R
Output:
R
Reading
Multiple Inputs
A
single Scanner object can read multiple
inputs of different types.
import
java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner input = new
Scanner(System.in);
String username = input.nextLine();
int age =
input.nextInt();
System.out.println(username);
System.out.println(age);
input.close();
}
}
Input:
Ram
20
Output:
Ram
20
Reading
Multiple Inputs in a Single Line
Unlike
Python, Java allows reading multiple inputs from a single line.
import
java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner input = new
Scanner(System.in);
int firstValue =
input.nextInt();
int secondValue =
input.nextInt();
System.out.println(firstValue);
System.out.println(secondValue);
input.close();
}
}
Input:
34 56
Output:
34
56
Printing
Output in Java
Java
provides two primary methods to display output:
print() Method
Prints
output on the same line.
class Main {
public static void main(String[] args) {
System.out.print("Hello");
System.out.print("Ram");
}
}
Output:
HelloRam
println() Method
Prints
output and moves to the next line.
class Main {
public static void main(String[] args) {
System.out.println("Hello");
System.out.println("Ram");
}
}
Output:
Hello
Ram
Summary
- Packages help organize Java
classes efficiently.
- The import keyword is used to
import specific classes or entire packages.
- The Scanner class reads user input
using methods like nextInt(), nextLine(), etc.
- print() prints output on the
same line, whereas println() moves
to a new line.