Type Casting in Java
In Java, type casting is a method or process that converts a data type into another data type in both ways manually and automatically. The automatic conversion is done by the compiler and manual conversion performed by the programmer. In this section, we will discuss type casting and its types with proper examples.
Type castinggv资源整合 散文随笔 E道阅读网 gv资源整合 散文随笔 E道阅读网 ,人和兽xxxx全文免费阅读 人和兽xxxx E道阅读网 人和兽xxxx全文免费阅读 人和兽xxxx E道阅读网 ,色老头人体艺术 全文免费阅读 第72话 E道阅读网 色老头人体艺术 全文免费阅读 第72话 E道阅读网
Convert a value from one data type to another data type is known as type casting.
Types of Type Casting
There are two types of type casting:
Widening Type Casting
Converting a lower data type into a higher one is called widening type casting. It is also known as implicit conversion or casting down. It is done automatically. It is safe because there is no chance to lose data. It takes place when:
For example, the conversion between numeric data type to char or Boolean is not done automatically. Also, the char and Boolean data types are not compatible with each other. Let's see an example.
Before conversion, the value is: 7 After conversion, the long value is: 7 After conversion, the float value is: 7.0
In the above example, we have taken a variable x and converted it into a long type. After that, the long type is converted into the float type.
Narrowing Type Casting
Converting a higher data type into a lower one is called narrowing type casting. It is also known as explicit conversion or casting up. It is done manually by the programmer. If we do not perform casting then the compiler reports a compile-time error.
Let's see an example of narrowing type casting.
In the following example, we have performed the narrowing type casting two times. First, we have converted the double type into long data type after that long data type is converted into int type.
Before conversion: 166.66 After conversion into long type: 166 After conversion into int type: 166
gv资源整合 散文随笔 E道阅读网 gv资源整合 散文随笔 E道阅读网 ,人和兽xxxx全文免费阅读 人和兽xxxx E道阅读网 人和兽xxxx全文免费阅读 人和兽xxxx E道阅读网 ,色老头人体艺术 全文免费阅读 第72话 E道阅读网 色老头人体艺术 全文免费阅读 第72话 E道阅读网 ML