Well-off study with you JAVA -------- data type conversion, java type conversion

Source: Internet
Author: User
Tags integer division

Well-off study with you JAVA -------- data type conversion, java type conversion

This article describes a common output situation in JAVA: data type conversion.

 

The data type of Java has been determined at the time of definition, so it cannot be converted to other data types at will, but Java allows users to perform type conversion to a limited extent. Data type conversion can be divided into "automatic type conversion" and "forced type conversion.

 

Automatic type conversion

Variables of the data type have been defined in the program. If you want to use another data type, Java will automatically convert the data type when the following conditions are met:

1. The data type before conversion is compatible with the converted data type.

2. The value range of the converted data type is larger than that before the conversion.

For example, if you want to convert a of the short type to the int type, because both short and int are of the integer type, which meets the preceding condition 1, the int value range is larger than that of short, it also meets condition 2. Therefore, Java automatically converts variable a of the short type to the int type.

It is worth noting that the type conversion is limited to the statement of this row, and does not affect the type of the variable originally defined, and the data accuracy can be ensured through automatic type conversion, it does not lose data content due to conversion. This type of conversion is also called extended conversion. As mentioned above, if the integer type is short or byte, Java will automatically convert the short and byte types in the expression to the int type to avoid overflow, to ensure the correctness of the calculation results, which is also the "extended conversion" function provided by Java. In terms of "extended conversion", it may be easier to understand-the character and integer can be converted automatically; the integer and floating point number are also compatible; but the boolean type can only store true or flase, it is incompatible with integers and characters, so it is impossible to convert types. Next, let's take a look at the changes in the calculation results when one of the two numbers is a floating point number?

Example: TestJava3_11.java

01 // The following program declares two variables: integer and floating point.

02 public class TestJava3_11

03 {

04 public static void main (String args [])

05 {

06 int a = 156;

07 float B = 24.1f; // declare the floating point variable f and assign a value

08

09 System. out. println ("a =" + a + ", B =" + B );

10 System. out. println ("a/B =" + (a/B); // The integer is automatically converted to the floating point type.

11}

12}

Output result:

A = 156, B = 24.0

A/B = 6.5

The running result shows that when one of the two numbers is a floating point number, the result of the operation is directly converted to a floating point number. When the types of variables in the expression are different, Java will automatically convert a smaller representation range to a larger representation range before performing operations. That is to say, if there is an integer and a double-precision floating point number for calculation, Java will convert the integer into a double-precision floating point number before the calculation, the calculation result will also become a double-precision floating point number.

 

Forced type conversion

When two integers are computed, the result is also an integer. For example, the result of an integer division of 8/3 is Integer 2, which is not the actual 2.6666 ..., Therefore, if you want to obtain a floating point number in Java, you must make a mandatory conversion of the data type. The conversion syntax is as follows:

[Mandatory conversion syntax for Data Types in the format of 3-1]

(Data type to be converted) variable name;

Because this type of forced conversion is directly written in the program code, it is also called explicit conversion. The following program illustrates how integers and floating-point numbers are converted in Java.

Example: TestJava3_12

01 // The following example shows how to use the automatic and forced conversions.

02 public class TestJava3_12

03 {

04 public static void main (String args [])

05 {

06 int a = 55;

07 int B = 9;

08 float g, h;

09

10 System. out. println ("a =" + a + ", B =" + B );

11g = a/B;

12 System. out. println ("a/B =" + g + "\ n ");

13 System. out. println ("a =" + a + ", B =" + B );

14 h = (float) a/B; // here, the data type is forcibly converted.

15 System. out. println ("a/B =" + h );

16}

17}

Output result:

A = 55, B = 9

A/B = 6.0

A = 55, B = 9

A/B = 6.111111

When two integers are separated, the digits after the decimal point are truncated, so that the calculation result is kept as an integer. However, this is not the expected calculation result. To get the result of the calculation as a floating point number, you must forcibly convert one or both of the two integers to a floating point number, the following three writing methods are correct:

(1) (float) a/B // convert integer a to a floating point number and then divide it with integer B.

(2) a/(float) B // forcibly convert integer B to a floating point number, and then divide it by integer

(3) (float) a/(/float) B // forcibly converts an integer a and B to a floating point number at the same time, then Division

As long as the data type to be converted is added before the variable, the variable in this statement is automatically processed during the runtime, but this does not affect the data type defined previously. In addition, if you assign a value that exceeds the variable's valid range to this variable, this conversion is called a narrow conversion. Since the data accuracy may be lost during the conversion process, Java does not automatically convert these types. At this time, mandatory conversion is required.


Conversion of java Basic Data Types

Because you use byte b1 = (byte) 128;
Forced type conversion is used to assign values. Therefore, the extra values are forcibly removed.
128 of the int type is written in binary format in memory.
00000000 00000000 00000001 00000000
Because you have forced type conversion, it becomes in the memory.
1000000 is converted to decimal format-128.
You can take a good look at the bit operations.
However, these contents are the basis of the foundation, but the actual application is basically unavailable.

Data Types in java

None of the answers above are correct. What people ask is in Java!

In Java, the char variable represents two bytes of the character, regardless of Chinese or English, regardless of the operating system, it is the same (because the variables are consistent, so Java can be cross-platform )!

As for the value range from 127 to-128, it refers to the value range of byte variables.
In Java, only byte variables are 8-bit. Because the computer uses binary to represent data, and Java requires that all variables are signed, the first of the variables is used to represent the symbol, 0 indicates the positive number, and 1 indicates the negative number, therefore, in the byte variable, the maximum positive integer is binary: 0111 1111, Which is 127 in decimal format; the minimum negative number is 1000 000 in decimal format, and-128 in decimal format. Therefore, the value range of byte variables is-128 to 127.

If you do not understand these basic things, just check the teaching materials. If you write more programs, you will be familiar with them.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.