Java Type Conversions

Java is a strongly typed language because at compile time the type of every variable is known. Type conversion refers to the problem of how the different types interrelate with each other—for example, adding a byte variable to a long variable. There are two kinds of type conversions:

  • Explicit type conversion—This is done using the cast operator. For example

    float a = 1.9;
    float b = 3.2;
    float c = 4.7;
    a = (int)(a/b*c)
    

    The preceding casts the result into an int.

  • Implicit type conversion—This is done automatically by the language as needed.

Note

In Java, an implicit conversion between numeric data types is performed only if it doesn't result in loss of precision or magnitude. Attempts to covert data types that would cause a ...

Get e-Commerce Applications Using Oracle8i and Java from Scratch now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.