Java Type Conversion Or Java Type Casting

Java Type conversion

Java Type Casting Or java Type Conversion:

Java Type Conversion– In this tutorial you will learn about type casting or type conversion. In computer science, type conversion, type casting, type coercion, and type juggling are different ways of changing expression from one data type to another. An example would be the conversion of an integer value into a floating-point value or its textual representation as a string, and vice versa. Type conversions can take advantage of certain features of type hierarchies or data representations. Two important aspects of a type conversion are whether it happens implicitly (automatically) or explicitly, and whether the underlying data representation is converted from one representation into another, or a given representation is merely reinterpreted as the representation of another data type. In general, both primitive and compound data types can be converted.



Numeric Primitive Java Type Conversion or Type Casting:

Numeric primitives data can be cast in two ways. Implicit type casting happens when the source type has a smaller range than the target type.

Implicit casting

Explicit type casting has to be done when the source type has a larger range than the target type.

Explicit casting

When casting floating-point primitives data type (float, double) to whole number primitives, the number is rounded down.


Basic Numeric Promotion Java Type Conversion or Type Casting:

One special case of implicit type conversion is type promotion, where the compiler automatically expands the binary representation of objects of integer or floating-point types. Promotions are commonly used with types smaller than the native type

Non-numeric primitive Java Type Conversion or Type Casting

A boolean type cannot be cast to/from any other primitive type. the char can be cast to/from any numeric type by using the code-point mappings specified by Unicode. A char is represented in memory as an unsigned 16-bit integer value (2 bytes), so casting to byte (1 byte) will drop 8 of those bits (this is safe for ASCII characters). The utility methods of the Character class use int (4 bytes) to transfer to/from code-point values, but a short (2 bytes) would also suffice for storing a Unicode code-point.


Object Java Type Conversion or Type Casting

As with primitives, objects can be cast explicitly and implicitly. Implicit type casting happens when the source type extends or implements the target type (casting to a superclass or interface). Explicit type casting has to be done when the source type is extended or implemented by the target type (casting to a subtype). This can produce a runtime exception (ClassCastException) when the object being cast is not of the target type (or the target’s subtype).

Testing if an object can be cast using instanceof

Java delivers the instanceof operator to test if an object is of a certain type, or a subclass of that type. The program can then choose to cast or not cast that object consequently.



java type conversion or type casting Examples:

Example how to use java type conversion or type casting in Binary Numeric Promotion:

OutPut:

Java Type conversion

Example how to use java type conversion or type casting in Unary Numeric Promotion:

OutPut:
Java Type conversion

 Example how to use java type casting for Array Types in Java:

OutPut:

Java Type conversion

 

Related Article:

Java Hello World First Program And Java Vm Error Fixing

Recommended For You

Leave a Reply

%d bloggers like this: