About the conversion mechanism in Java collation

Source: Internet
Author: User


The following are the most basic data type comparisons.


650) this.width=650; "src=" Http://www.grabsun.com/uploads/images/201206-2/144721_08_738.GIF "alt=" 144721_08_738. GIF "/>

Explain the points:

1. It can also be divided into two main categories: Boolean and numeric (mainly for data conversion)

(Note that Boolean cannot be converted with other types, and it is not possible to assign a Boolean to a type such as int)


A 2.String string is not a basic data type, a string is a class, that is, a reference data type.


3. If you need to use a higher-precision floating-point number, consider using the BigDecimal class.


The 4.Java provides three special floating-point values: positive infinity, negative infinity, and non-number to indicate overflow and error. For example, using a positive float divided by 0 will get positive infinity (positive_infinity), minus infinity (negative float) divided by 0 to get negative infinite (negative_infinity). 0.0 divided by 0.0 or a negative number to get a non-numeric (NaN). (All belong to double or float packing class)


5. All positive infinity values are equal, all negative infinity values are equal, and Nan is not equal to any numeric value.



I. Automatic type conversion of a basic numeric type

This is a good understanding, that is, in the basic type (except the Boolean), you can automatically convert the system to a small range of directly assigned to a large range of variables.

Generally, the following conversions are carried out without special marking

char→int→long→float→double

Byte→short

public class autoconversion{public static void Main (string[] args) {int a = 6;       Float F = A;//int can be automatically converted to float byte B = 9; Char c = b;//error, byte cannot be converted to char type double d = b;//byte can be converted to double}}


PS: There is a special automatic type conversion, that is, the basic type (Boolean line) and a null character, you can form a corresponding string.


public class conversion{public static void Main (string[] args) {Boolean b = true; String str = b + ""; System.out.print (str);//Output True}}


Ii. Compulsory type conversion

The above automatic type can only convert a small number of tables to a large size, if there is no explicit representation of the large to small, a compilation error occurs.


public class conversion{public static void Main (string[] args) {int b = 233; byte c = (byte) b;//forces an int to a byte type Syste M.out.println (c);//output -23 Float a = 5.6;//error because 5.6 default is double type float a = (float) 5.6;//correct, to be forced conversions double d = 3.98; int e = (int) d;//forces a double to be converted to int type System.out.println (e);//Output 3}}


As above, to perform a large range of tables to small, the need for explicit declaration, if the forced conversion after the value is too large, resulting in loss of precision.


Three, string conversion to base type

Typically, a string cannot be converted directly to the base type, but it can be implemented by a wrapper class that corresponds to the base type.

Each wrapper class will have a parsexxx (string str) static method that is used to convert the string to the base type

Sting a = "45";      Use the integer method to convert a string to int ivalue = Interger.parseint (a); The Boolean is special, only true if the string is true, and the other is false Boolean B = boolean.valueof (a);



Iv. converting a base type to a string (string)

Each wrapper class has a toString method, such as double.tostring (double D), which can be converted to string strings.


V. Conversion of basic data types and packaging classes


Basic data types Packing class
Boolean Boolean
Char Character
Byte Byte
Short Short
Integer Integer
Long Long
Float Float
Double Double

The following example converts each other between the two

int i = 1;integer iwrap = new Integer (i);//packing int unWrap = Iwrap.intvalue ();//Unpacking



About the conversion mechanism in Java collation

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.