Conversion between Java data types), java data type conversion

Source: Internet
Author: User

Conversion between Java data types (conversion) and java data type conversion

This is my first note. I will update the content from time to time, and I hope you can correct it and make common progress!

I. Basic Data Types

There are four basic data types:

1> int data types: byte (8 bits), short (16 btis), int (32 bits), long (64 bits );

2> float length data types include: single precision (32 bits float) and double precision (64 bits double). in Java, decimals are of the double type by default, to define a float, use the f statement later;

3> values of boolean variables include true and false;

4> char data types: unicode characters, 16 characters

Basic data types: int, float, boolean, char, double, short, byte, long,

Encapsulation type: Integer, Float, Boolean, Character, Double, Short, Byte, Long

Conversions between two data types

It can be divided into the following situations:

1> low-to advanced automatic upgrade type conversion;

2> advanced to low-level forced type conversion (will cause overflow or loss of precision );

3> convert the basic type to the class type (encapsulation type;

4> conversion of basic types to strings;

5> class type conversion to string;

3. conversion rules between Basic Data Types

1. In a double-operand or bitwise arithmetic operation, the low-level data type is automatically converted to the advanced BIT data type based on the operand type, which is divided into the following situations:

1> as long as one of the two operands is of the double type, the other will be converted to the double type, and the result is also of the double type;

2> as long as one of the two operands is of the float type, the other will be converted to the float type and the result is of the float type;

3> as long as one of the two operands is of the long type, the other will be converted to the long type and the result is also of the long type;

4> both operands (including byte, short, int, and char) are converted to the int type and the result is also of the int type;

2. If the low-level type is char, the conversion to the advanced type (integer) will be converted to the corresponding ASCII code value, and then the automatic conversion of other types.

3. For the byte, short, and char Types, they are of the same level, so they cannot be automatically converted to each other. You can use the following forced type conversion.

4. You cannot force type conversion between a Boolean value and any numeric type.

5. Forced conversion between different data types may cause overflow or decrease in precision.

6. When a byte type variable is involved in the operation, Java performs an automatic data operation to convert it to the int type. For example:

Int b7 = 5;
Byte b8 = (byte) b7 * 3 );

  Iv. Conversion between packaging data types and basic data types

  You can use the constructor of a simple type to convert a variable to a corresponding packaging class.

Boolean (boolean value), Character (char value), Integer (int value), Long (long value), Float (float value), Double (double value)

In each packaging class, there is always a method of xxValue () to get the corresponding simple type data. this method can also be used to convert different numeric variables. For example, for a double-precision instance class, intValue () can get the corresponding integer variable.

1. Conversion between strings and Other Types

1> conversion from other types to strings

Call the String Conversion Method of the class: X. toString ();

Automatic conversion: X + "";

Use the String method: String. valueOf (X );

2> convert a string to another type as a value

First, convert it to the corresponding package instance, and then call the corresponding method to convert it to another type, such as: double d = Double. valueOf (31.1 );

    

Static parseXXX Method

String s = "1 ";
Byte B = Byte. parseByte (s );
Short t = Short. parseShort (s );
Int I = Integer. parseInt (s );
Long l = Long. parseLong (s );
Float f = Float. parseFloat (s );
Double d = Double. parseDouble (s );

 

Character's getNumericValue (char ch) ----> convert char data to int type

  5. Conversion instance

  1. Conversion Between Basic Types

Forward conversion: Use the class wrapper to generate a new class type variable.

Integer a = new Integer (2 );

Reverse conversion: using the class package

Int B = a. intValue ();

Use the class package --> Basic Data Type

Ps1: int I = Integer. parseInt ("123 ");

Note: This method can only be used to convert strings into integer variables.

Ps2: float f = Float. valueOf ("123"). floatValue ();

Note: In the above example, a string is converted into a Float object, and then the corresponding float value is returned by calling the floatValue () method of this object.

Ps3: boolean B = Boolean. valueOf ("123"). booleanValue ();

Note: In the above example, a string is converted into a boolean object, and then the corresponding blooean value is returned by calling the booleanValue () method of this object.

Ps4: double d = Double. valueOf ("123"). doubleValue ();

Description: converts a string to a double object, and then calls the doubleValue () method of this object to return its corresponding double value.

Ps5: long l = Long. valueOf ("123"). longValue ();

Note: convert a string into a long object, and then call the longValue () method of this object to return its corresponding long value.

Ps6: char = Character. valueOf ("123"). cahrValue ()

Note: convert a string into a Character object, and then call the charValue () method of this object to return the corresponding char value.

2 conversion from basic type to string

Forward conversion:

System. out. println ("1" + 2 + 3); --> 123 convert to string operation
System. out. println (2 + 3); --> 5 conversion does not exist

System. out. println (2 + 3 + "1"); --> after adding the first two values, splice the strings

Convert three types to strings

Forward conversion: because each class is a subclass of the object class, And all object classes have a toString () function, you can use the toString () function to convert it.

Reverse conversion: a new class type variable is generated through the class package.

Int I = Integer. valueOf ("123"). intValue ()

Float f = Float. valueOf ("123"). floatValue ()

Boolean B = Boolean. valueOf ("123"). booleanValue ()

Double d = Double. valueOf ("123"). doubleValue ();

Long l = Long. valueOf ("123"). longValue ()

Charc = Character. valueOf ("123"). charValue ()

Description: converts a string into a Character object, and then calls the charvalue () method of this object to return its corresponding char value.

Related Article

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.