examples of data type casting in java

0

Type casting is a way of converting data from one data type to another data type. This process of data conversion is also known as type conversion or type coercion.

In Java, we can cast both reference and primitive data types. By using casting, data can not be changed but only the data type is changed.

Note: type casting is not possible for a Boolean data type.

There are 13 types of conversion in Java. In this article, we will only look at 2 major types:

  1. Implicit casting.
  2. Explicit casting.

All Contributions

example of casting to float in java

float x = (float)1;
float y = (float)2.8;
float w = (float)4.2;
System.out.println(x);
System.out.println(y);
System.out.println(w);

example of cast float to integer in java

int x = (int)1.9;
int y = (int)2.8;
System.out.println(x);
System.out.println(y);

total contributions (2)

New to examplegeek.com?

Join us