Back to the 2026 paper

Module 1: Introduction to Java

20267m

Analyse Java's primitive data types and type conversion mechanisms. Construct suitable examples showing implicit widening and explicit narrowing conversion, including one case where data loss occurs.

Worked SolutionAI Assisted

Primitive types and conversion

Java has eight primitive types: byte, short, int, long, float, double, char, and boolean. Numeric types differ in range and precision; boolean represents true/false and is not numeric.

Widening converts a type to a type with a broader range, usually implicitly:

int count = 25;
double value = count; // 25.0, no narrowing loss

Narrowing converts to a smaller or less precise type and requires a cast:

double price = 19.95;
int whole = (int) price; // 19: fractional part lost
int n = 130;
byte b = (byte) n; // wraps to -126 for 8-bit signed byte

A cast permits the conversion; it does not guarantee that the original value is preserved. Use appropriate types and validate ranges.

Similar questions

Free AppAndroid App

Coursify Android App

Generate AI courses, take quick quizzes between classes, and master complex engineering concepts on the go.