DEV Community

Harini
Harini

Posted on

Debugging in Java: Practice with 10 Errors in a Simple Program

When learning Java, writing code is just one part. The real skill comes from debugging errors.

Error Example 1: Incorrect Keyword Case

Public class Home{
    Public static void main(String[] arg){
        System.out.println("Hello world");
    }
}
Enter fullscreen mode Exit fullscreen mode

Errors in the Code:

1. Incorrect Keyword Case

  • Java is case-sensitive.
  • Public should be written as public.

2. Main Method Keyword Error

  • Public static void main should be public static void main.

Error Example 2: Missing static Keyword

public class Home{
    public void main(String[] args){
        System.out.println("Hello world");
    }
}
Enter fullscreen mode Exit fullscreen mode

Error in the Code:

1. Missing static Keyword

  • The main method is declared as public void main instead of public static void main.
  • The JVM looks specifically for the static main method as the entry point.

2. Runtime Error

  • The program may compile, but it will not run.
  • You may see an error like:
    • Main method is not static in class Home, please define the main method as: public static void main(String[] args)

Error Example 3: Wrong Data Type (string vs String)

public class Home{
    public static void main(string[] args){
        System.out.println("Hello world");
    }
}
Enter fullscreen mode Exit fullscreen mode

Error in the Code:

1. Incorrect Data Type (string)

  • Java is case-sensitive.
  • string is incorrect and will cause a compilation error.
  • The correct data type is String (with a capital S).

Error Example 4: Incorrect Main Method Parameter

public class Home{
    public static void main(String args){
        System.out.println("Hello world");
    }
}
Enter fullscreen mode Exit fullscreen mode

Error in the Code:

1. Incorrect Main Method Parameter

  • The main method should accept an array of strings.
  • Here, String args is used instead of String[] args.
  • Because of this, the JVM will not recognize this method as the entry point.

Error Example 5: Case-Sensitive Class Name Issue

public class Home{
    public static void main(String[] args){
        system.out.println("Hello world");
    }
}
Enter fullscreen mode Exit fullscreen mode

Error in the Code:

1. Incorrect Class Name (system)

  • Java is case-sensitive.
  • system is incorrect and will cause a compilation error.
  • The correct class name is System (with a capital S).

Error Example 6: Missing Double Quotes

public class Home{
    public static void main(String[] args){
        System.out.println(Hello world);
    }
}
Enter fullscreen mode Exit fullscreen mode

Error in the Code:

1. Missing Double Quotes

  • Hello world is not enclosed in double quotes.
  • Java treats it as a variable or identifier, which is not defined.
  • This will cause a compilation error.

Error Example 7: Unclosed String Literal

public class Home{
    public static void main(String[] args){
        System.out.println("Hello world);
    }
}
Enter fullscreen mode Exit fullscreen mode

Error in the Code:

1. Unclosed String Literal

  • The string "Hello world is missing the closing double quote.
  • This results in a compilation error called unclosed string literal.

Error Example 8: Missing Semicolon

public class Home{
    public static void main(String[] args){
        System.out.println("Hello world")
    }
}
Enter fullscreen mode Exit fullscreen mode

Error in the Code:

1. Missing Semicolon (;)

  • The statement System.out.println("Hello world") does not end with a semicolon.
  • Java requires a semicolon at the end of each statement.

Error Example 9: Missing Parenthesis

public class Home{
    public static void main(String[] args){
        System.out.println("Hello world"
    }
}
Enter fullscreen mode Exit fullscreen mode

Error in the Code:

1. Missing Closing Parenthesis )

  • The method System.out.println() is missing a closing parenthesis.
  • This results in a compilation error.

2. Syntax Issue in Statement

  • Because of the missing ), the statement is incomplete and invalid.

Error Example 10: Missing Curly Brace

public class Home{
    public static void main(String[] args){
        System.out.println("Hello world");
    }

Enter fullscreen mode Exit fullscreen mode

Error in the Code:

1. Missing Closing Curly Brace }

  • The class Home is not properly closed.
  • One closing brace } is missing at the end of the program.
  • This leads to a compilation error.

Correct Code:

public class Home{
    public static void main(String[] args){
        System.out.println("Hello world");
    }
}
Enter fullscreen mode Exit fullscreen mode

Output:

Explanation of the Code:

1. Class Declaration

  • public class Home defines a class named Home.
  • The program execution starts from this class.

2. Main Method

  • public static void main(String[] args) is the entry point of the program.
  • The JVM starts executing the code from this method.

3. Print Statement

  • System.out.println("Hello world"); is used to print output to the console.
  • It displays the message Hello world.

Top comments (1)

Collapse
 
payilagam_135383b867ea296 profile image
Payilagam

Nice!