JAVA DATA TYPES

Java type casting

Type casting converts a value from one data type to another, for example, int to double. Java may convert automatically or require you to specify the target type yourself.

Two ways to convert numeric types

  • Widening conversion (widening) happens automatically when Java allows a value to be assigned to a wider type.
  • Narrowing conversion (narrowing) is specified manually by writing the target type in parentheses before the value.
Numeric type conversions do not form a single straight chain.

byte can widen to short, int, long, float or double. char can widen to int and wider numeric types, but short does not automatically convert to char.

Automatic widening conversion

Java automatically performs permitted widening conversions. In this example, an int value is assigned to a double variable without extra syntax:

int sveikasis = 9;
double trupmeninis = sveikasis; // Automatic int -> double

System.out.println(sveikasis);  // 9
System.out.println(trupmeninis); // 9.0

Meaning 9 can be represented as double, so the result is displayed as 9.0.

byte mazas = 100;
short trumpas = mazas;
int sveikasis = trumpas;
long ilgas = sveikasis;
double platus = ilgas;

Converting char to a number

char values can be automatically assigned to an int variable. The resulting number is the numeric value of the UTF-16 code unit:

char raide = 'A';
int unicodeKodas = raide;

System.out.println(unicodeKodas); // 65

Manual narrowing conversion

When narrowing, a value may no longer fit the target type or may lose information. You must therefore explicitly write the target type in parentheses:

double trupmeninis = 9.78;
int sveikasis = (int) trupmeninis; // Explicit double -> int

System.out.println(trupmeninis); // 9.78
System.out.println(sveikasis);   // 9

When converting 9.78 to int, the fractional part is discarded. The result is 9, rather than the mathematically rounded value 10.

The value may not fit

Type byte stores only values from −128 to 127. When narrowing a larger int, excess bits are discarded and the result may be unexpected:

int skaicius = 130;
byte mazas = (byte) skaicius;

System.out.println(mazas); // -126
Narrowing does not validate business meaning.

Before converting, check whether the value fits within the target type's range.

Automatic does not mean perfectly precise

Java allows automatic conversion of int to float, but float cannot represent every large integer exactly:

int tikslus = 16_777_217;
float apytikslis = tikslus;

System.out.println(tikslus);     // 16777217
System.out.println(apytikslis);  // 1.6777216E7

Widening is allowed without an explicit cast, but converting from int or long to a floating-point type can change precision. Exact monetary calculations often use , not float or double.

What should you remember?

  • Java usually performs permitted widening conversions automatically.
  • When narrowing, write the target type in parentheses, for example, (int).
  • Converting a fractional number to an integer discards the fractional part.
  • A narrowing conversion can overflow the target type's range.
  • Automatic conversion to a floating-point type can reduce precision.