Javatpoint Logo
Javatpoint Logo

What is Cast Operator in Java?

In Java, the cast operator is used to convert a value from one data type to another. It is denoted by the parenthesis's operator "()".

Syntax:

Within the brackets, the cast operator is used to change the value to the chosen data type. The final result is assigned to the variable with the chosen data type.

Consider the situation when you want to change a double value to an integer data type. The cast operator can be used as shown below:

In this example, the double value "10.5" is cast to an integer data type using the cast operator. The resulting value of "10" is then assigned to the integer variable "intValue".

Casting can cause data loss or truncation if the value being converted cannot be represented in the destination data type, thus it's crucial to keep this in mind. Every time you execute a cast, it's a good idea to check for possible data loss.

Each variable in Java must be defined with a specific data type because it is a strongly typed language. Converting a value between different data types is sometimes essential. The cast operator will now be used in this situation.

In Java, a value can be changed from one data type to another using the cast operator. The target data type is enclosed in brackets after the parentheses-enclosed operator "()" to signify it. As an example, consider the following syntax for the cast operator:

In this case, the cast operator's value is the expression or variable that you want to change to the desired data type. The final result is assigned to the variable with the chosen data type.

Let's imagine, for instance, that you want to convert a double value to an integer data type. The cast operator can be used as shown below:

In this illustration, the cast operator is used to convert the double value "10.5" to an integer data type. The integer variable "intValue" is then given the resultant value of "10".

Here's a complete Java code sample that uses the cast operator to show how to do it:

CastOperatorExample.java

Output:

Double value: 10.5
Int value: 10
Int value: 65
Char value: A

In this example, we first declare a double variable named doubleValue and assign it the value of 10.5. We then use the cast operator to convert this double value to an integer and assign the result to an integer variable named intValue. We then print out the values of both variables to the console.

As we can see from the output, the cast operator successfully converted the double value to an integer and the integer value to a character value.

The ASCII code for the letter "A" is 65, therefore we declare an integer variable named intVal and give it that value. The result is then assigned to the charVal variable via a cast operation, which changes this integer value from a numeric value to a character value. Next, we print to the console the values of both variables.

Casting can cause data loss or truncation if the value being converted cannot be represented in the target data type, so it is crucial to keep this in mind. Any decimal places will be eliminated, for instance, if you cast a double value to an integer data type. Every time you execute a cast, it's a good idea to check for possible data loss.

Not all data types can be cast to one another, which is another crucial thing to bear in mind. For instance, neither a boolean value nor an integer can be cast to the other. Compilation errors will be produced if you try to accomplish this.

The cast operator in Java is a helpful tool for converting values between different data types, to sum up. The parentheses operator is used to indicate it, then the target data type is placed inside of them. But it's crucial to be aware of the possibility of data loss and the restrictions on casting between various data kinds.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA