Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement type conversion and forced type conversion in java

2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces java how to achieve type conversion and mandatory type conversion, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

Java type conversion and forced type conversion

If you have previous programming experience, you already know that it is quite common to assign a value of one type to a variable of another type. If the two types are compatible, Java will convert them automatically. For example, it is always possible to assign a value of type int to a variable of type long. However, not all types are compatible, so not all type conversions can be implemented implicitly. For example, there is no definition of converting double type to byte type. Fortunately, it is still possible to obtain conversions between incompatible types. To do this, you must use a cast that performs explicit conversions between two incompatible types. Let's look at automatic and forced type conversions.

3.9.1 automatic conversion of Java

If the following two conditions are met, automatic type conversion (automatic type conversion) is performed when one type of data is assigned to another type variable:

These 2

The types are compatible.

The range of destination types is larger than that of source types.

When the above two conditions are met, broadening transformation (widening conversion) occurs. For example, the scope of the int type is larger than the legal scope of all byte types, so explicit cast statements are not required.

For widened conversions, numeric types, including integer (integer) and floating point (floating-point) types, are compatible with each other, but numeric types and character types (char) or Boolean types (bollean) are not compatible. Character types (char) and Boolean types (bollean) are also incompatible with each other.

3.9.2 cast of incompatible types

Although automatic type conversion is helpful, it does not meet all programming needs. For example, what would you do if you needed to assign the value of type int to a variable of type byte? This conversion does not occur automatically because the variation range of the byte type is smaller than that of the int type. This conversion is sometimes called a "shrink conversion" (), because you must make the value of the source data type smaller to fit the target data type.

In order to complete the conversion between two incompatible types, you must do a forced conversion. The so-called cast is nothing more than an explicit type conversion. Its general format is as follows:

(target-type) value

Where the target type (target-type) specifies the type to which the specified value is converted. For example, the following program segment casts int type to byte type. If the value of an integer is outside the range of values of type byte, its value will be reduced by modularization of the range of type byte (integer divided by the remainder of byte).

Int a

Byte b

/ /...

B = (byte) a

A different type conversion occurs when a floating-point value is assigned to an integer type: truncation. You know, integers don't have decimal parts. In this way, when a floating-point value is assigned to an integer type, its fractional part is rounded off. For example, if you assign a value of 1.23 to an integer, the resulting value is only 1pm 0.23 discarded. Of course, if the floating point value is too large to fit the target integer type, its value will be reduced by modulating the range of the target type.

The following procedure illustrates casting:

/ / Demonstrate casts.

Class Conversion {

Public static void main (String args []) {

Byte b

Int I = 257

Double d = 323.142

System.out.println ("Conversion of int to byte.")

B = (byte) I

System.out.println ("i and b" + I + "" + b)

System.out.println ("Conversion of double to int.")

I = (int) d

System.out.println ("d and i" + d + "" + I)

System.out.println ("Conversion of double to byte.")

B = (byte) d

System.out.println ("d and b" + d + "" + b)

}

}

The output of the program is as follows:

Conversion of int to byte.

I and b 257 1

Conversion of double to int.

D and i 323.142 323

Conversion of double to byte.

D and b 323.142 67

Let's look at each type conversion. When the value 257 is cast to a byte variable, the result is the remainder of 257 divided by 256, which is the range of variations of the byte type. When the variable d is converted to int, its fractional part is discarded. When the variable d is converted to byte, its fractional part is discarded, and its value is reduced to a modulus of 256, that is, 67.

Thank you for reading this article carefully. I hope the article "how to achieve type conversion and forced conversion in java" shared by the editor will be helpful to you. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report