Skip to main content
PreparationZone Logo
PreparationZone
Back to Fundamentals

Fundamentals · Medium · Free

What is implicit vs explicit type conversion? Give examples that cause bugs in interviews.

FresherFundamentalsProgramming Fundamentals
Model answer

Implicit vs explicit conversion

Implicit conversion (widening/coercion) happens when the compiler or runtime promotes a value automatically—e.g., int to double in arithmetic. Explicit conversion (casting/parsing) is intentional: (int) 3.9 or Integer.parseInt("42").

Bug patternWhy it bitesInterview fix
String "10" vs number 10 (JS)Sort/compare gives wrong orderNormalize types before compare
total / count with intsInteger division truncatesCast numerator to double first
Silent float → int castLoses cents in billingUse BigDecimal or integer cents
java
double avg = (double) total / count;
int port = Integer.parseInt(System.getenv("PORT"));
// Java: widening int→long is implicit; narrowing needs cast
Interview tip: Give one real bug—"I once fixed a leaderboard that compared string ranks to numbers."