×

Compile-time Error in Java

In java, the execution of a program is stopped due to the occurrence of some problem known as an error. Errors are illegal operations that are carried out by the user. The errors are not detected till the compilation of a program is completed. A program without errors can only be successfully compiled and executed.

In the field of computer science, we are very familiar with compile-time and run-time errors. The time when the java file (source) is converted into the class file (executable) is called Compile time and the time when the class file (executable) starts executing or running is called run time. Different types of errors occur in both compile-time and run-time.

An error may occur sometimes due to incorrect syntax and sometimes even if the syntax is correct errors occur. The incorrect syntax cause errors in the program that kind of errors are known as compile-time errors because these errors are displayed at the time of compilation and other errors are run-time errors that are displayed at the time of running.

From the above statement, based on the time of occurrence of error the errors on classified into two types they are as shown below.

  1. Compile-time error.
  2. Run-time error.

Compile-time Error

The incorrect syntax cause errors in the program that kind of errors are known as compile-time errors. By the compiler at the time of compilation errors are thrown. Compile time errors are also mentioned as syntax errors.

Compile-time syntax:

Javac filename.java

The above syntax is used to compile the java file (source). If there are any syntax errors in a program then the compiler detects the error and displays them on the console. These kinds of errors are easily detected and pointed out by the compiler. The programmer easily finds and sorts out the error. The compiler specifies the error type and where it has occurred.

Types of Compile-time Errors

The compile time errors are occurred due to missing parenthesis, missing a semicolon, utilizing undeclared variables, etc.

There are 3 types of compile time errors they are as shown below:

1.Syntactical errors: Because syntax was incorrect, these types of errors occur. In the program. The below mentioned are some of the syntactical errors.

  • Error in structure
  • Missing operators
  • Unbalanced parenthesis

Program: This example program is without specifying condition in if statement.

class java18
{
public static void main(String args[])
{
int a=20;
                                            if()            // without condition
System.out.println("number is 20");
else
System.out.println("number is not 20");
}
}

Output:

java18.java:6: error: illegal start of expression

if()

^

1 error

  • Error in structure:    An error occurred, "=" is usedwhen "==" is needed.

Program:

class java18
{
public static void main(String args[])
{
int a=20;
  if(a=20)   // used “=” instead of “==”
System.out.println("number is 20");
else
System.out.println("number is not 20");
}
}

Output:

java18.java:6: error: incompatible types: int cannot be converted to boolean

if(a=20)

^

1 error

  • Missing operators:  An error occurred, when “; ” is missed.

Program:

class java18
{
public static void main(String args[])
{
int a=20     // missed “ ; “ (semicolon)
   if(a=20)   
System.out.println("number is 20");
else
System.out.println("number is not 20");
}
}

Output:

java18.java:5: error: ';' expected

int a=20

^

1 error

  • Unbalanced parenthesis: error occurs when we miss the parenthesis and error in expression.

Program:

class java18
{
public static void main(String args[])
{
int a=20,b=10,c;
                                                      c=a+*b;                    // expression error 
                                           System.out.println("c="+c;     //missing parenthesis
}
}

Output:

java18.java:6: error: illegal start of expression

c=a+*b;

^

java18.java:7: error: ')' expected

System.out.println("c="+c;

^

2 errors

2. Semantic errors: Due to the unclarity of code, these types of errors occurred. The below mentioned are some of the syntactical errors. The below mentioned are some of the semantic errors.

  • Incompatible types of operands
  • Undeclared variable
  • Incompatible types of operands: error occurred due to the incompatibility.

Program:

class java18
{
public static void main(String args[])
{
int a="hello";//the String type and int are not compatible
System.out.println(a);
}
}

Output:

java18.java:5: error: incompatible types: String cannot be converted to int

int a="hello";

^

1 error

  • Undeclared variable: error occurred due to the not initialization.

Program:

class java18
{
public static void main(String args[])
{
int a=10;
System.out.println(b);//undeclared variable
}
}

Output:

java18.java:6: error: cannot find symbol

System.out.println(b);

^

symbol:   variable b

location: class java18

1 error

3. Lexical Errors: In the code, the error occurred due to an invalid character involved. Example program with invalid variable.

Program:

class java18
{
public static void main(String args[])
{
int +a=10;  // invalid variable
System.out.println(a);
}
}

Output:

java18.java:5: error: not a statement

int +a=10;

^

java18.java:5: error: ';' expected

int +a=10;

^

2 errors


Related Topics

Java Boolean logicalAnd() Method

The logicalAnd() method of Java Boolean class returns the result of implementing logicalAND operation on the specified Boolean operands. Syntax:public static boolean logicalAnd (boolean a, boolean b) Parameters:The parameters ‘a’ and ‘b’...

2 minutes read.

Bedrock vs Java

The popularity of Minecraft, a sandbox video game, has skyrocketed. The scope, level of complexity, and variety of gameplay in this game are enormous, and user-generated content has helped to...

4 minutes read.

Java String compareTo() Method

compareTo() method is used to compare the two specified Strings based on the alphabetical order(lexicographical order) of their characters.It returns positive number ,negative number or 0 Syntax: public int compareTo(String anotherString) Parameters: anotherString: the...

2 minutes read.

Java Math log1p() Method

The log1p() method of Math class returns the natural logarithmic sum for the specified double argument and 1. Its value is much closer to result of ln(1 + x). Syntax: public static...

2 minutes read.

Java Read File Line by line

Java provides two options to read files line by line. Each option offers different benefits and has its own set of drawbacks. Following are the ways through which on accomplish...

5 minutes read.

Star Pattern Programs in Java

Star Pattern Programs in Java The star pattern programs in Java is the part of pattern programs in Java, which we discussed earlier. Right Triangle Star Pattern Filename: StarPatternExample.java public class StarPatternExample {              public static void...

4 minutes read.

Automatic Resource Management

In computer programming, resource management refers to the wide set of techniques for the effective management of the system resources. There are two ways of management of resources: The computer program itself...

2 minutes read.

Java Write File

In this post, we'll examine various Java programming methods for writing into files. Since this class is character-oriented due to how it is used in file handling in Java, it...

4 minutes read.

Java Code Coverage Tools

Code coverage testing is a crucial metric that gauges how thoroughly the program's source code has been tested. The market is flooded with Code Coverage Tools, making it difficult to...

7 minutes read.

Missing Number in an Arithmetic Progression in Java

Given an array that shows the elements of an orderly arithmetic progression. Find the missing number to complete the succession of elements. Example:  Input: a [ ] = {2 , 4 , 6...

3 minutes read.

Java Integer rotateLeft() method

The rotateLeft() method of Java Integer class returns the value obtained by rotating the  2’s complement binary representation of the given integer value left by the specified number of bits. Syntax public...

1 minute read.

Java.net.ConnectionException

java.net.ConnectException: Connection rejected: the interface is the most continuous sort of happening, organizing special cases in Java at whatever point the product is in client-server engineering and attempting to make...

3 minutes read.

Java Private keyword

A Java access modifier is a private keyword. It can be used to inner classes, methods, and variables. It is the type of access modifier that is most constrained. Privately declared...

4 minutes read.

Add numbers represented by Linked Lists in Java

For calculating the sum of the two numbers that are represented by a linked list, and then store the result in a new linked list. A linked list's head node...

7 minutes read.

Java HashSet

HashSet implements the set interface. It uses the hash table to make the collection to store different data types. The hash set is the unordered collection of different data types....

6 minutes read.

Shift right zero Fill Operator in Java and Operator Shifting

Left shift operator ( << ) : The left shift operator is an operator which performs its action at the bits level of a binary Operator. That means when you perform...

4 minutes read.

Difference Between in Java and C++

FeatureC++JavaDefinitionC++ is a general programming language created by Bjarne Stroustrup as an extension of c language    Java is class-based, object-based, and designed to have as few implementation dependencies as...

4 minutes read.

Tetranacci Number in Java

This article mainly describes tetranacci number identification and the Java Program for Tetranacci numbers. Tetranacci number Tetranacci numbers and Fibonacci numbers are related. The key contrast is that a Tetranacci number depends...

3 minutes read.

What is string in Java why it's immutable

String in Java Strings are a series of characters commonly used in Java language, which are considered objects in the Java. To create and manipulate strings, java will provide the String...

4 minutes read.

Java Extends keyword

Extends Extends is a keyword which is completely depended on the concept of the inheritance of the java programming language.To understand about of the keyword, we need to learn the concept...

3 minutes read.