๐Ÿ–จ๏ธ Input/Output Statements in Java

Java provides various mechanisms to accept input from the user and display output to the screen, typically using classes from the java.io and java.util packages.


๐Ÿ“ฅ Input in Java

The most common way to accept user input is through the Scanner class.

๐Ÿ”น Example Using Scanner

import java.util.Scanner;

public class InputExample {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter your name: ");
        String name = sc.nextLine();
        System.out.println("Hello, " + name);
    }
}

๐Ÿ“ค Output in Java

Java uses System.out.println() and System.out.print() for output.

  • System.out.println() โ€“ prints with a newline
  • System.out.print() โ€“ prints without a newline

๐Ÿ”น Example

System.out.println("Hello, world!"); // newline
System.out.print("Hello, ");         // no newline
System.out.print("Java!");           // continues on same line

๐Ÿ” Other I/O Options

  • BufferedReader (legacy, more control)
  • Console (secure password entry)
  • FileReader, FileWriter (for file I/O)