Java error incompatible types

Java Error incompatible types occurred when a compiler found a variable and expression whose data type is not compatible to perform an operation on them.

Java error incompatible types

Java error incompatible types

     

Java Error incompatible types occurred when a compiler found a variable and expression whose data type is not compatible to perform an operation on them.

Understand with an Example

In this Tutorial we want to describe you a code that help you in understanding the java error incompatible type. For this we have a class name Incompatobletype.Inside the main method we declare a variable name roll num  and sec int and char  data type. Checklist condition is used to evaluate the expression. The assignment operator = is used to show you an incompatible type error in java rather than using = = in assignment operator. The Java Compiler show an error in the assignment operator printed by System.out.println.

 

Incompatitabletype.java


public class Icompatobletype {

  public static void main(String[] args) {

  int rollnum = 76;
  char sec;
  if (rollnum = 90) {
  sec = 'A';
  else {
  sec = 'B';
  }
  System.out.println("Section with this rollno is:  = " + sec);
  }
}

Output

Compiling source file to /home/girish/NetBeansProjects/errors/build/classes
/home/girish/NetBeansProjects/errors/src/Icompatobletype.java:8: incompatible types
found : int
required: boolean
  if (rollnum = 90) {
error
BUILD FAILED (total time: seconds)

To resolve this error give if (rollnum = =90)instead of if (rollnum = 90)

Download code