Java incompatible types Error in AP CSA: Type Mismatch Fix

AP CSAJava Errors › incompatible types
Java Error Help • AP CSA

Java "incompatible types" Error: Type Mismatch Explained (AP CSA)

The compiler reports incompatible types when you try to put a value of one type into a variable of another type that cannot hold it. In AP CSA this most often means assigning a double to an int, or storing the wrong object type in a typed variable.

Error type: Compiler error (the code will not build).

Plain meaning: You assigned a value whose type does not match (and cannot be automatically converted to) the variable's declared type.

Fastest fix: Match the types, or add an explicit cast when narrowing is intended and safe (for example (int) a double).

Why students see this error

Java is statically typed: every variable has a declared type, and the compiler checks that each assignment fits. Java will automatically widen a smaller type to a larger one (int to double), but it refuses to automatically narrow a larger type into a smaller one (double to int) because that can lose data. When the assignment does not fit and no automatic conversion applies, you get incompatible types.

  • Assigning a double result to an int variable without a cast.
  • Storing the wrong object type, such as putting a String into an int variable.
  • Forgetting that integer division and method return types must match the receiving variable.

Broken code that triggers it

Throws incompatible types
public class Convert {
  public static void main(String[] args) {
    double price = 19.99;
    int rounded = price;   // incompatible types: double cannot be int
    System.out.println(rounded);
  }
}

price is a double, and the code tries to store it in an int. Narrowing a double into an int can lose the decimal part, so Java refuses to do it automatically and reports incompatible types: possible lossy conversion from double to int.

Trace: watch where it breaks

The compiler checks the type of each side of the assignment:

Expression Value type Target type Result
double price = 19.99 double double ok
int rounded = price double int incompatible types

This is caught at compile time, so the program never runs and prints nothing. The compiler stops as soon as it finds the mismatched assignment.

The fix

Runs correctly
public class Convert {
  public static void main(String[] args) {
    double price = 19.99;
    int rounded = (int) price;   // explicit cast: truncates to 19
    System.out.println(rounded);
  }
}

Adding the explicit cast (int) tells the compiler you intend the narrowing and accept that the decimal part is truncated (19.99 becomes 19, not 20). The reverse direction (double d = anInt;) needs no cast because widening never loses data.

How College Board tests this concept

AP Connection (Official CED)

Primitive types, widening, narrowing, and casting are core to Using Objects and Methods, and matching method return types to variables matters throughout class design. The exam expects you to know which conversions are automatic and which require a cast.

Read it in the official CED: Unit 1 (CED p.23) · Unit 3 (CED p.73)

Expect MCQs that show an assignment and ask whether it compiles. The key rule: widening (smaller to larger, like int to double) is automatic; narrowing (larger to smaller, like double to int) requires an explicit cast or it is a compile error.

Common MCQ traps (practice set)

Watch out

The trap is assuming Java rounds when narrowing. A cast from double to int truncates (drops the decimal), it does not round. And without the cast, it does not compile at all.

Question 1: Does it compile?

int count = 5;
double avg = count;

What happens with this code?

  • A) Compile error: incompatible types
  • B) Compiles; avg is 5.0
  • C) Compiles; avg is 5
  • D) Runtime exception
Answer: B. Assigning an int to a double is widening, which Java does automatically, so it compiles and avg holds 5.0. A is the trap that assumes any type difference fails; widening is always allowed. C ignores that a double prints with a decimal.

Question 2: Spot the fix

This line gives incompatible types: double d = 7.5; int n = d;

Which change makes it compile?

  • A) int n = (int) d;
  • B) int n = (double) d;
  • C) int n = round(d);
  • D) int n = d.toInt();
Answer: A. An explicit (int) cast permits the narrowing and compiles (n becomes 7). B casts to double, which still cannot fit an int. C is not a valid bare call (it is Math.round and returns a long for a double). D treats a primitive like an object, which does not exist.

Question 3: Which assignments compile?

Consider each assignment independently.

I.    double x = 5;
II.   int y = 5.0;
III.  int z = (int) 5.9;

Which compile without an incompatible types error?

  • A) I and III
  • B) I only
  • C) II and III
  • D) I, II, and III
Answer: A. I widens int to double automatically (x = 5.0), compiles. III casts explicitly, compiles (z = 5, truncated). II tries to put a double literal into an int with no cast, which is incompatible types. So I and III only.

Frequently asked questions

Why does int to double work but double to int does not?

Widening (int to double) never loses information, so Java does it automatically. Narrowing (double to int) can lose the decimal part, so Java requires an explicit cast to confirm you accept that.

Does casting a double to int round the value?

No. It truncates, dropping the decimal part. (int) 5.9 is 5, not 6. Use Math.round if you need rounding.

Is incompatible types a runtime or compile error?

It is a compile error. The program never runs; the compiler refuses to build code with a type mismatch.

Master the concepts behind the error

Work through the full AP CSA lessons, then test yourself.

AP CSA Lessons All Java Errors

Get in Touch

Whether you're a student, parent, or teacher — I'd love to hear from you.

Just want free AP CS resources?

Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.

Typically responds within 24 hours

Message Sent!

Thanks for reaching out. I'll get back to you within 24 hours.

🏫 Welcome, fellow educator!

I offer curriculum resources, practice materials, and study guides designed for AP CS teachers. Let me know what you're looking for — whether it's classroom materials, a guest speaker, or Teachers Pay Teachers resources.

Email

[email protected]

📚

Courses

AP CSA, CSP, & Cybersecurity

Response Time

Within 24 hours

Prefer email? Reach me directly at [email protected]