Java Programming

Java Type Conversion Or Java Type Casting

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.


Amazon Purchase Links:

Top Gaming Computers

Best Laptops

Best Graphic Cards

Portable Hard Drives

Best Keyboards

Best High Quality PC Mic

Computer Accessories

*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!

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

Engr Fahad

My name is Shahzada Fahad and I am an Electrical Engineer. I have been doing Job in UAE as a site engineer in an Electrical Construction Company. Currently, I am running my own YouTube channel "Electronic Clinic", and managing this Website. My Hobbies are * Watching Movies * Music * Martial Arts * Photography * Travelling * Make Sketches and so on...

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button