Notes for Class 9 ICSE: Input in Java
Comments in Java
Comments are used to make the code more readable and understandable. Java provides three
types of comments:
1. Single-Line Comments
- Description: Used to comment a single line of code.
- Syntax: // Comment text
2. Multi-Line Comments
- Description: Used to comment multiple lines of code.
- Syntax:
/*
Comment text spanning multiple lines
*/
3. Documentation Comments
- Description: Used to generate documentation for the program.
- Syntax:
/**
* This is a documentation comment.
* It provides details about the code.
*/
Scanner Class
The Scanner class is used to take input from the user during program execution.
- Import Statement:
import java.util.Scanner;
- Example:
import java.util.Scanner;
public class Example {
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 + "!");
Types of Errors
Errors are issues in the program that prevent it from running correctly. The three main types of
errors in Java are:
1. Syntax Errors
- Description: Occurs when the code violates Java syntax rules.
- Example: Missing semicolon, misspelled keywords.
2. Logical Errors
- Description: The program runs but produces incorrect results due to incorrect logic.
- Example: Using the wrong formula in calculations.
3. Runtime Errors
- Description: Occurs during program execution due to invalid operations.
- Example: Division by zero, accessing an invalid array index.