Home > Computer Tutorials > Computer Knowledge > Application of automatic conversion rules between different data types

Application of automatic conversion rules between different data types

WBOY
Release: 2024-01-20 09:00:06
forward
1039 people have browsed it

Automatic conversion rules for various types of data

Basic type conversion is divided into automatic conversion and forced conversion.

Automatic conversion rules: data types with small capacity can be automatically converted into data types with large capacity, or

It can be said that low-level automatically converts to high-level. The capacity here does not refer to the number of bytes, but to the range of type representation.

Application of automatic conversion rules between different data types

Forced conversion rules: High-level to low-level requires forced conversion.

How to convert:

(1) The conversion on the right side of the assignment operator "=" is automatically converted to the highest-level data type in the expression before operation.

Conversion rules for both sides of assignment operator "=": When the level of the left operand is higher than the level of the right operand, type conversion will be automatically performed; when the levels of the left and right operands are equal, no conversion is required; when the level of the left operand is Lower than the right operand, a cast is required.

In Java, integer constants can be directly assigned to type variables such as byte, short, char, etc., provided that they do not exceed their expression range. If it's out of range, a cast is required.

C language type conversion

Type transfer:

1. It happens in the same compilation system. For example, if you convert int to long int, in VC 6.0, both long int and int are 4 bytes, so there will be no problem. But if you convert int If the type is converted to a short type, it cannot be loaded. In short, the long type cannot be converted to a short type. If the int type is converted to a float, double, or long double type, there will only be a few extra 0s at the end. However, if converted on the contrary, there will be a loss of data, and the decimal part will be omitted.

2. In Turob C2.0, short int and int are both 2 bytes, so there will be no problem in converting between them.

In short, there are two points. "The conversion of the same compilation system class depends on whether it can be saved. If the conversion rate is different, the conversion rate must be converted and then assigned. Different compilation systems should pay attention to the number of bytes of this type given by themselves. The distribution is different between different compilation systems."

If you have any questions, please feel free to ask me 188005370

How to convert between basic data types and characters in Java

For example: String s = "123";

int num = Integer.parseInt(s); Note: When the application attempts to convert a string to a numeric type, but the string cannot be converted to the appropriate format, an exception java.lang.NumberFormatException## will be thrown.

#2. Convert String type to double type variable: use parseDouble(String) method of double class

For example: String s = "123";

idouble num = Double.parseDouble(s);

3. Convert int type variables to String type variables: use the valueOf(int) method of the String class

For example: int num = 123;

String s = String.valueOf(num);

4. Convert Double type variables to String type variables: use the valueOf(int) method of the String class

For example: Double num = 123.0;

String s = String.valueOf(num);

5. Convert the String variable into a character array Char[]: use the toCharArray() method of the String class

For example: String s = "123";

6. Convert characters or character arrays into strings, also through the valueOf() method, and no sample code will be given.

The above is the detailed content of Application of automatic conversion rules between different data types. For more information, please follow other related articles on the PHP Chinese website!

source:docexcel.net
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template