📢 Join My WhatsApp Channel JavaWithSumit

Get Daily ICSE Java Notes, Programs & Exam Tips 🚀

👉 Join Now

Initialization, Types of Errors & Comments in Java (Explained with Examples)

 

Initialization in Java

Initialization in Java means assigning values to variables. It is one of the most important concepts for ICSE students because it controls how data is used in a program.

In Java, data can be given to a program in three different stages:

  1. Data before execution (Initialization)
  2. Data at the time of execution (Parameters)
  3. Data during execution (Input using Scanner)

1. Data Before Execution (Initialization)

This is the simplest type of initialization.

👉 Here, values are assigned to variables before the program starts running.

Example:

class Example {
public static void main(String args[]) {
int a = 10; // Initialization
int b = 20;

int sum = a + b;
System.out.println("Sum = " + sum);
}
}

Explanation:

  • a = 10 and b = 20 are initialized before execution
  • The program will always give the same output

Best for: Fixed values
Not flexible


2. Parameter at the Time of Execution

In this method, values are passed when the program is executed.

👉 These values are called Command Line Arguments (Parameters).

Example:

class Example {
public static void main(String args[]) {
int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);

int sum = a + b;
System.out.println("Sum = " + sum);
}
}

How to Run:

java Example 10 20

Explanation:

  • args[0] → first value (10)
  • args[1] → second value (20)
  • Values are given at runtime

More flexible than initialization
❌ Slightly difficult for beginners


3. Input During Execution (Scanner Class)

This is the most commonly used method.

👉 Data is entered during program execution using keyboard input.

Example:

import java.util.Scanner;

class Example {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);

System.out.print("Enter first number: ");
int a = sc.nextInt();

System.out.print("Enter second number: ");
int b = sc.nextInt();

int sum = a + b;
System.out.println("Sum = " + sum);
}
}

Important Scanner Methods

Method                                    Description
nextInt()Reads integer value
nextDouble()Reads decimal value
nextFloat()Reads float value
next()Reads single word
nextLine()Reads full line

Explanation:

  • Scanner sc = new Scanner(System.in); → creates input object
  • sc.nextInt() → takes integer input from user

Most flexible method
✅ Used in real programs


Quick Comparison Table

Method                        When Data is Given                Flexibility
InitializationBefore executionLow
ParametersAt executionMedium
Scanner InputDuring executionHigh

Errors in Java

While writing Java programs, we often make mistakes. These mistakes are called Errors.

👉 An error in Java is any problem that causes the program to:

  • Stop working
  • Produce wrong output
  • Crash during execution

Understanding errors is very important for debugging (fixing problems) in programs.


Types of Errors in Java

There are mainly three types of errors:

  1. Syntax Errors
  2. Runtime Errors
  3. Logical Errors

1. Syntax Errors (Compile-Time Errors)

👉 Syntax errors occur when we do not follow the rules (syntax) of Java.

These errors are detected by the compiler before the program runs.


Example:

class Example {
public static void main(String args[]) {
int a = 10
System.out.println(a);
}
}

Error:

  • Missing semicolon ;

Correct Code:

int a = 10;

Common Syntax Errors:

  • Missing semicolon ;
  • Wrong spelling of keywords (publik instead of public)
  • Missing brackets { }
  • Incorrect data types

Detected before execution
✅ Easy to fix


2. Runtime Errors

👉 Runtime errors occur during program execution.

The program compiles successfully but crashes while running.


Example:

class Example {
public static void main(String args[]) {
int a = 10;
int b = 0;

int result = a / b; // Runtime Error
System.out.println(result);
}
}

Error:

  • Division by zero → ArithmeticException

Other Examples:

  • Accessing invalid array index
  • Taking wrong input type
  • Null values

✅ Occur during execution
❌ Program stops suddenly


3. Logical Errors

👉 Logical errors occur when the program runs successfully but gives wrong output.

These are the most difficult errors to find.


Example:

class Example {
public static void main(String args[]) {
int a = 10;
int b = 20;

int sum = a - b; // Logical Error
System.out.println("Sum = " + sum);
}
}

Problem:

  • Wrong operator used (- instead of +)

Correct Code:

int sum = a + b;

✅ Program runs without crash
❌ Output is incorrect


Quick Comparison Table

Error Type         When It Occurs        Example                Result
Syntax ErrorBefore executionMissing ;Compilation fails
Runtime ErrorDuring executionDivide by zeroProgram crashes
Logical ErrorAfter executionWrong formulaWrong output

Comments in Java

In Java programming, comments are used to write notes inside the program.

👉 These notes are ignored by the compiler and are not executed.

Comments help programmers to:

  • Explain code
  • Improve readability
  • Make programs easy to understand

Why Comments are Important?

✔ Makes code easy to read
✔ Helps in debugging
✔ Useful for beginners and teamwork
✔ Explains logic of program


Types of Comments in Java

There are mainly two types of comments in Java:

  1. Single Line Comment
  2. Multi-Line Comment

1. Single Line Comment

👉 A single line comment is used to write a comment in one line only.

Syntax:

// This is a single line comment

Example:

class Example {
public static void main(String args[]) {
// This program prints a message
System.out.println("Hello World");
}
}

Explanation:

  • // is used to start a single line comment
  • Everything after // is ignored

Use Case:

  • Short explanations
  • Quick notes

2. Multi-Line Comment

👉 A multi-line comment is used to write comments in multiple lines.

Syntax:

/* This is a
multi-line
comment */

Example:

class Example {
public static void main(String args[]) {
/* This program
prints Hello World
using Java */
System.out.println("Hello World");
}
}

Explanation:

  • Starts with /*
  • Ends with */
  • Can cover multiple lines

Use Case:

  • Detailed explanation
  • Describing logic
  • Writing large notes

Difference Between Single Line and Multi-Line Comments

Feature                    Single Line Comment            Multi-Line Comment
Symbol///* */
Lines SupportedOne lineMultiple lines
UsageShort notesDetailed notes

Initialization, Types of Errors & Comments in Java (Explained with Examples)

📢 Join My WhatsApp Channel JavaWithSumit

Get Daily ICSE Java Notes, Programs & Exam Tips 🚀

👉 Join Now
Read Also: 

Comments

Popular posts from this blog

Introduction to Object Oriented Programming and Java- ICSE Notes

Introduction to OOP's

Object Modelling – Entities and Their Behaviour in Java