Mutual conversion between Java variable types

xiaoxiao2021-03-06  55

Mutual conversion between Java variable types

Www.linuxaid.com.cn

? 01-12-16 10: 10? 2505p?

Everywhere

???? We know that Java's data types are divided into three categories, namely Boolean, characters, and numerical types, and numerical types are intellectual and floating-point; relative to data types, Java variable types For Boolean Boolean; character type char; integer, short, int, long; floating-point float, double. Four of four integer variables and two floating-point variables correspond to different precision and ranges, respectively. In addition, we often use two types of variables, names, and DATE. Mutual conversion between these variable types is often used in our programming. In our article today, we will see how to implement these conversions. ???? First, integer, real, character variables, integer, real, characters in Java, is considered as the same class, these types are low-level to advanced respectively ( Byte, short, char) - int - long - float - double, low variables can be directly converted to advanced variables, for example, the following statement can pass directly in Java: ???? byte b; ??? ? INT i = b; ???? When converting advanced variables to low variables, the situation is complex, you can use forced type conversion. That is, you have to use the following statement format: ???? Int i; ???? byte b = (byte) i; ???? can be imagined, this conversion may definitely lead to overflow or accuracy, so We do not recommend using this conversion. ???? II. Java packaging class ???? When we discuss mutual conversion between other variable types, we need to understand Java packaging classes, so-called packaging classes, it is to directly represent the simple type variables as A class that we will use these packaging classes when performing the mutual conversion of variable types. Java has six packaging classes, namely Boolean, Character, Integer, Long, Float and Double, which we can see them from the literal, which correspond to Boolean, CHAR, INT, LONG, FLOAT, and DOUBLE. String and Date itself are class. So there is no concept of packaging classes. ???? Third, the mutual conversion between simple type variables and packaging classes ???? Simple type variables are converted to the corresponding packaging class, which can utilize the constructor of the package class. That is: ???? Boolean (Boolean Value), Character (Integer (int value), long (long value), float (float value), Double (double value) ???? in each package class In the middle, there is a way to be ×× value () to get its corresponding simple type data. With this method, the conversion between different numerical variables can be achieved, for example, for a double-precision real type, intValue () can obtain its corresponding integer variable, while DoubleValue () can get its corresponding double precision Type variable.

转载请注明原文地址:https://www.9cbs.com/read-118007.html

New Post(0)