Krithi K
last year
Krithicountry asked

Why do we use java.util.Scanner and why do we write class Main in Java?

Abhay Jajodia
Expert
last year
Abhay Jajodia answered

Hi Krithi,

java.util.Scanner is used to take input from the user, like reading a number or a word from the keyboard.

class Main is the main container for your program. Java code must be inside a class, and the program starts running from the main() method inside that class.

Example:

import java.util.Scanner;

class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int x = sc.nextInt();
    System.out.println(x);
  }
}

If you have more questions, I am here to help 😊

Java
This question was asked as part of the Learn Java Basics course.