Once variables and constants of various types will be put together within an expression, they can be changed into a similar type. This technique of transforming a single predefined type into one other is known as type conversion in Java.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
There are 2 different types of conversion that we are using in programming languages.
If the type conversion is conducted instantly through the compiler without having the programmer’s involvement, the type conversion is known as implicit type conversion. The compiler fundamentally encourages every operand towards the data type of the biggest operand.No loss of data occurs throughout the data conversion.No chance of throwing exception through the conversion and thus is known as type-safe. Conversion of smaller sized quantity too much larger number can be implicit conversion. Transformation of integer type data to float.
float i=0; int j=10; i=j;
// This can be implicit transformation since float can be larger than an integer, therefore no lack of data And also no exception.
The type conversion, which can be enforced through the programmer, is known as explicit type conversion. fundamentally, the programmer makes an expression to become of a particular type. Explicit type transformation can be known as typecasting. Loss of data might or might not occur during data conversion. Therefore There exists a probability of details loss. it could throw an error if it perhaps attempted to perform without typecasting. Transformation of a bigger number to smaller sized numbers can be explicit conversion.
float k=123.456 int i= (int) k
// this can be Explicit conversion as well as (int) is typecast, operator. At this point, we might manage to escape an exception, but you can find the visible loss of data. i.e. i=123
// .456 can be dropped in the conversion process
Like other programming languages, there are 2 types of conversion in java:
Examples of type conversion mentioned below in detail:
Code:
We have a simple program here, some declaration at the top, float, double, byte, short, and long, and the variables are each named to help identify what their types are like float is floatVal,
Code:
long is longVal
Code:
And the program just prints out a Success message if it runs.
So let’s just see how some of the type conversion comes into play here. So let’s first of all, let’s just go ahead and create a variable. We have short, and we will just call it the result.
In fact, let’s go ahead and call it result1. And let’s just do a simple assignment. And so first, we will just assign the byteVal to it. Now, as we expect, if we go ahead and run this, so run successfully.
Code:
Output:
We know that’s a valid assignment because a byte can be assigned into a short because that is a widening conversion.
If we take this byteVal, though, and we make it a longVal instead, so now it’s actually a long; if we run this, we get an error message here saying incompatible type, possible loss of conversion from long to short.
Code:
So what we can do here then is we can do an explicit cast. We will just put short in front of this. So now it’s valid, so we can run it.
Code:
And, of course, it works. Because the long could not go into a short because that was a narrowing conversion.
Output:
But by putting the explicit cast in front of it, now it’s valid. If we want to, we can put a cast notation be very explicit and say that you know, we know although a byte conversion is legal, we want to explicitly show that we are casting it by putting the short cast in there, we can do that, and that’s completely legal.
Code:
Output:
So now, let’s take a look at another scenario. We are going to create another variable we will call result2, and result2 is short as well. And what we want to do here is we just going to take our byteVal, and we want to subtract the longVal. Now we know that’s not legal because the result of the expression is going to be the size of the largest integer in it, which is the length.
Code:
So if we run this, we get an error saying that it’s not valid to convert a long to a short.
But let’s say we want to go ahead and keep that result as short. We need to do a cast. But we want to cast this time as the value of the entire result here. So what we are going to do is put the short cast in front of it here.
Put the short cast in front of it here. And wrap the whole thing in parentheses. And run it.
Code:
It will run successfully.
Output:
Now declare another variable called result3, but declare this one as a long. So got result 3, and what will we do here is we will assign that our longVal – floatVal. So we run that, the error gets lost conversion converting from float to long because whenever we have an integer type and any floating-point type, the result is going to be the floating-point type.
Code:
So let’s go ahead and convert our result now to a float. So by making that a float, we should be able to go ahead and run it. And run successfully.
Code:
Output:
But now, if we take the floatVal here and we convert this to be a doubleVal, and if we try to run this, we get the error because it says the result is going to be a double because when you do an integer and a floating-point, it’s the size of the largest floating point in the equation.
Code:
So let’s go ahead and make this result a double, so now we can run it.
Code:
Output:
1. Variable are strongly typed in Java
2. Primitive types
3. Type conversion
The above is the detailed content of Type Conversion in Java. For more information, please follow other related articles on the PHP Chinese website!