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.
- Compile-time error.
- 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