Home Java Java-get-example Java error incompatible types
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Java error incompatible types

Advertisement
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 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
Advertisements

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: November 3, 2008

Ask Questions?    Discuss: Java error incompatible types   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
james
January 27, 2012
java

userName = keyboardIn.nextLine(); my program wont compile due to this piece of code. It comes out as Incompatible type. I'am a first year student doing java programming. I have declare my variable String userName = new String(); Can anyone see the problem from this because I cant
Nehru
March 5, 2012
Error: Incompatible types

textfield = new TextField("First Name:", "", 40, TextField.ANY); error: incompatible types textfield = new TextField("First Name:", "", 40, TextField.ANY); required: Textfield found: TextField
rony
October 29, 2012
java-error-incompatibletypes

public void breadthFirstTraversal(Node rootNode){ Queue q = new LinkedList(); q.add(rootNode); rootNode.visited=true; while(!q.isEmpty()){ Node n = (Node)q.poll(); System.out.print(n.data + " "); for(Node adj : n.adjacentNodes{/*incompatible type required: graphsearch.Node found: java.lang.Object*/ if(!adj.visited){ adj.visited=true; q.add(adj); } } } } How to solve?
Daniel
December 6, 2012
Camilleri

Thx very much it worked Now i can complete my school project
Mohan
August 30, 2013
Incompatible Type error

try { char ch=""; for(;;) { ch=st.charAt(0); st=st.substring(1,st.length()); st=st+ch; repaint(); Thread.sleep(100); } } OUTPUT:char ch=""; ^ required:char found:String
DMCA.com