×

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 LinkedHashMap

LinkedHashMap implements the Map interface. It inherits the HashMap class. Some essential features of LinkedHashMap are: It contains the values based on the keys. It maintains the order of insertion. It contains unique...

5 minutes read.

Sparse Numbers in Java

In this section, we will be very well acknowledged about the sparse numbers in Java, how a number can be verified if it is a sparse number or not. Sparse Numbers Any...

3 minutes read.

Java Integer doubleValue() method

The doubleValue() method of Integer class returns a double value for this Integer after a widening primitive conversion. Syntax public double doubleValue() Parameters NA Specified by This method is specified by doubleValue in class Number Return Value This...

1 minute read.

Java Clone Array

We frequently need to copy an array to back up its original components. We have a few unique numbers and strings, including Armstrong numbers, palindrome numbers, and palindrome strings. To...

4 minutes read.

Java Math hypot() Method

The hypot() method of Math class returns the square root for the expression x2 + y2 without the intermediate underflow or overflow . Syntax: public static double hypot(double x, double y) Parameters: The parameters...

2 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.

How to Create an API in Java?

Introduction The API can be abbreviated as Application Programming Interface. An API is a combination of set of classes and interfaces. It is also equivalent to a simple java program. To...

12 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 Volatile Keyword

The compiler, runtime, or processors may use any kind of optimization if there aren't any required synchronizations. Although most of the time these improvements are advantageous, they occasionally can result...

6 minutes read.

How to calculate time complexity of any program in Java

Java : Java's syntax and principles are derived from the C and C++ languages. We know that java is one of the programming language. The main feature of java which is...

3 minutes read.

Armstrong Number Program in Java

Armstrong Number Program in Java: A positive number is called an Armstrong number if the sum of the cube of each digit is equal to the number itself. There are...

6 minutes read.

ArrayList Program in Java

ArrayList Program in Java: In Java, ArrayList is a class that belongs to java.util package. It is the dynamic list that grows or shrinks at run-time as per the requirements....

4 minutes read.

Converting Long to Date in Java

What Long and Date are in Java and how are they implemented in the Java programming language are the topics of this article. Additionally, we'll go into great detail on...

4 minutes read.

Java String valueOf() method

Java String valueOf() method converts different types of values into String. Such as : int to String, long to String, boolean to String, character to String, float to String, double to...

2 minutes read.

ArrayDeque in Java

ArrayDeque The ArrayDeque is one of the essential concepts in java to implement the deque interface. It will allow us to apply a resizable array to implement the Deque interface. This...

8 minutes read.

How to add Elements in Array in Java

Consider an array of the size n, in the given size we must add the elements in the given array. In this tutorial we are preferred to learn how to add...

3 minutes read.

Byte to Hex in Java

Java exclusively uses byte data types to store in a byte array, which is an array. Each component of a byte array has a default value of 0. Hex String -...

3 minutes read.

Multithreading in Java

Multithreading is a specialized form of multitasking. It is responsible for executing more than one task at a time of a single program, and each task is a separate thread. A program...

8 minutes read.

Java Float Keyword

Float: In general, there are two categories of data types: primitive data types and non-primitive data types. So, float is the data type which is a primitive data type. Declarating the variables and...

3 minutes read.

Java Generate UUID

What java Generate UUID? A 128-bit long value that is universally unique serves as the basis for the terms UUID (Universally Unique Identifier) and GUID (Globally Unique Identifier). Hexadecimal (octet) digits...

5 minutes read.