Basic Input and Output operations
Basic Input and Output operations
OUTPUT
System Class
The System class in Java is a part of the java.lang package and provides access to system-level resources
such as standard input, output, and error streams. It contains several static methods and fields, one of which is
out.
out Field
System.out is a static member of the System class that refers to the standard output stream. By default, this
stream outputs data to the console. It is an instance of the PrintStream class, which handles text data display.
PrintStream Methods
The PrintStream class provides several methods for printing text, including:
print(): Outputs the given text without adding a newline at the end.
println(): Outputs the given text and moves the cursor to the next line (i.e., appends a newline character).
printf(): Allows formatted output similar to printf in C, where you can format strings, integers, floating-point
numbers, etc.
OUTPUT
Common Printing Methods
System.out.print("Hello");
System.out.print("World");
// Output: HelloWorld
System.out.println("Hello");
System.out.println("World");
In Java, certain characters are considered "escape sequences," which are used to represent non-
printing characters like newlines (\n), tabs (\t), and backslashes (\\).
\n: Newline
\t: Tab
\\: Backslash Example:
System.out.println("Hello\nWorld");
Advantages:
Disadvantages:
Advantages:
More efficient for reading large blocks of input.
Good for reading line-based input (e.g., reading from files).
Disadvantages:
All input is read as a string, so parsing is necessary if you need other data types.
Needs exception handling because readLine() throws IOException.
INPUT
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class Main {
public static void main(String[] args) {
// Create BufferedReader object
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
try {
// Reading string input
System.out.println("Enter a string: ");
String str = reader.readLine();
// Reading integer input
System.out.println("Enter an integer: ");
int num = Integer.parseInt(reader.readLine());//Need to parse string input into an integer
// Output the inputs
System.out.println("String: " + str);
System.out.println("Integer: " + num);
} catch (IOException e) {
System.out.println("Error reading input: " + e.getMessage());
}
}
}
INPUT
Using the Console Class
The Console class is used for secure input, especially when you want to mask input (such as passwords). It’s part of the
java.io package and is available only when running the program from a console or command-line environment.
Advantages:
Allows secure input for sensitive data like passwords.
Avoids the newline issue found in Scanner.
Disadvantages:
Only works in a real console environment (not available in some IDEs like Eclipse or IntelliJ).
INPUT
import java.io.Console;
public class Main
{
public static void main(String[] args) {
// Get the system console
Console console = System.console();
if (console != null) {
// Reading a line of input
String name = console.readLine("Enter your name: ");
Key Points:
args[0], args[1], etc., contain the command-line arguments passed when running the program.
You need to convert the string arguments into appropriate types if needed (e.g., using Integer.parseInt() for integers).
Advantages:
Useful for passing configuration options or parameters without prompting the user during runtime.
Disadvantages:
Requires user to run the program with specific arguments at the command line.
INPUT
public class Main
{
public static void main(String[] args)
{
if (args.length > 0)
{
// Output command-line arguments
System.out.println("First argument: " + args[0]);
}
else
{
System.out.println("No arguments passed.");
}
}
}
INPUT
Method Class Use Case Advantages Disadvantages
Requires manual
Efficient reading of large Fast, can handle large
BufferedReader java.io.BufferedReader parsing, needs exception
text input blocks of input
handling