Home  >  Article  >  Java  >  Type Conversion in Java

Type Conversion in Java

PHPz
PHPzOriginal
2024-08-30 15:12:18654browse

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

Type Conversion

There are 2 different types of conversion that we are using in programming languages.

Type Conversion in Java

1. Implicit Type Conversion

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.

2. Explicit Type Conversion

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

Type Conversion in Java

Like other programming languages, there are 2 types of conversion in java:

Implicit Type Conversion

  • These are generally what we call Widening conversion, and these can be done automatically because we are moving to wider data types. So if we have a 32-bit integer and we want to move to a 64-bit integer, that’s wider. So the value can be safely moved so that it can be done implicitly. The compiler has to decide how to make these conversions, and the rules are pretty straightforward.
  • If we have an expression with multiple (Mixed) integer sizes in it, if we have short and long, whatever the largest integer size is, things will convert it to. So if we do an operation with a short and a long, the short will be implicitly cast into along.
  • If we perform an operation with mixed floating-point sizes, so we have a float and a double, they will always go to the double because double is the largest floating-point size.
  • And then, if we have an operation that uses mixed integer types and floating-point types, the compiler will cast to whatever the largest floating point in the equation. So if we do an operation with a long and afloat, the long will be cast to afloat.
  • If we do an operation with a long and a double, the long will then be cast to a double.

Explicit Type Conversion

  • We explicitly perform in our code when using this cast operation. When we do that, we are taking responsibility for whatever happens as a result of that type of conversion. Because of that, we can perform both widening conversion and narrow. It widens from a 32-bit to 64-bit, narrowing, going from a 64-bit down to a 32-bit. We should just want to be aware that we know what could potentially happen.
  • If we do an explicit cast from a floating-point to an integer, so floating points can have a fractional portion, integer can’t, so any fractional portion would be dropped when we cast that float down to an integer.
  • You want to be careful when performing a narrowing conversion. If we have a 64-bit integer, it has the ability to hold values that are too large to fit into a 32-bit integer.
  • So if we cast that 64 bit to a 32-bit, the program will actually do it, but if the value is too large to fit into a 32-bit, you will get some odd results from that. So you want to make sure that when you are casting it doing a narrowing cast, you know that what you are doing is safe.
  • And the last one is just that you want to be careful when converting from an integer to a floating-point because if you have an integer with a large number of significant digits, because of the way floating point is stored, you could lose some of those significant digits.

Examples of Type Conversion

Examples of type conversion mentioned below in detail:

Code:

Type Conversion in Java

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:

Type Conversion in Java

long is longVal

Code:

Type Conversion in Java

And the program just prints out a Success message if it runs.

Type Conversion in Java

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:

Type Conversion in Java

Output:

Type Conversion in Java

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:

Type Conversion in Java

Type Conversion in Java

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:

Type Conversion in Java

And, of course, it works. Because the long could not go into a short because that was a narrowing conversion.

Output:

Type Conversion in Java

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:

Type Conversion in Java

Output:

Type Conversion in Java

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:

Type Conversion in Java

So if we run this, we get an error saying that it’s not valid to convert a long to a short.

Type Conversion in Java

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:

Type Conversion in Java

It will run successfully.

Output:

Type Conversion in Java

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:

Type Conversion in Java

Type Conversion in Java

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:

Type Conversion in Java

Output:

Type Conversion in Java

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:

Type Conversion in Java

Type Conversion in Java

So let’s go ahead and make this result a double, so now we can run it.

Code:

Type Conversion in Java

Output:

Type Conversion in Java

Conclusion

1. Variable are strongly typed in Java
2. Primitive types

  • Integers types, floating-point types, char type, Boolean type

3. Type conversion

  • We often have to move between different type because as our programs, as they get more complexity, will likely involve multiple data types.
  • The compiler can handle type conversions that are widening, moving from one type to another that can hold larger values,
  • But you can also use casting to explicitly perform those types of conversions that the compiler can’t perform automatically.

The above is the detailed content of Type Conversion in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Servlet ArchitectureNext article:Servlet Architecture