Data types in Java and conversions between them

Source: Internet
Author: User

Java The type of data in and the conversion between

of Basic Data Type

There are four basic types of the following:
1) int length data types are: Byte (8bits), short (16bits), int (32bits), Long (64bits),
2) Float length data types are: Single precision (32bits float), double precision (64bits double), in Java the decimal is the default double type, to define the float need to be declared after the data with F;
3) The value of a Boolean variable is: Ture, False
4) Char data types are: Unicode characters, 16 bits
Corresponding class types: Integer, Float, Boolean, Character, Double, short, Byte, Long

The basic data types are from low to advanced: (Byte, short, char)--int--long--float--double

PS: The "level" here refers to the size of the range that represents the value.

Transition between data types

It is divided into the following situations:

1) Low to advanced automatic type conversion;

2) advanced to low-level forced type conversions (which can result in overflow or loss of precision);

3) Basic type conversion to class type;

4) Conversion of basic types to strings;

5) class type to string conversion

Conversion rules between basic data types
1. In arithmetic expressions such as a double operand and a bitwise operation, the low-level data type is automatically converted to an advanced data type based on the type of the operand, divided into the following situations:

1) As long as one of the two operands is of type double, the other is converted to a double type, and the result is also a double type;

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

3) As long as one of the two operands is long, the other is converted to a long type, and the result is a long type;

4) Two operands (including Byte, short, int, char) are converted to the int type, and the result is of type int.

2. If the low-level type is char, converting to the Advanced type (integer) translates to the corresponding ASCII value, and then other types of automatic conversions.

3. For Byte,short,char three types, they are lateral and therefore cannot be automatically converted to each other, and can be cast using the following coercion type. Such as:

shorti=99;
Char c= (char) i;
System.out.println ("Output:" +c);

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

5. Conversions between different levels of data types can result in a drop in overflow or precision.

6. When the byte-type variable participates in the operation, Java is promoted as an automatic data operation type, converting it to an int type. For example: Byte B;

b=3;
B= (Byte) (b*3);//must declare byte.

Conversion between the wrapper data type and the base data type

A simple type of variable is converted to the appropriate wrapper class, which can take advantage of the wrapper class's constructor. That is: Boolean (boolean value), Character (char value), Integer (int value), long (Long value), float (float value), double (double Value
And in each packing class, the total tangible is xxvalue () method, to obtain its corresponding simple type data. This method can also realize the conversion between different numerical variables, for example, for a double-precision real class, intvalue () can get its corresponding integer variable, and Doublevalue () can get its corresponding double-precision real variable.
1. Conversion between strings and other types
⑴ conversion of other types to strings
① the string conversion method of the calling class:x.tostring ();
② Automatic conversion:x+ "";
③ method of using String: string.volueof (X);
⑵ a string as a value, to another type of conversion

The ① is converted to the corresponding wrapper instance before the corresponding method is converted to another type.
For example, the character "32.1" converts the value of a double type to the format: New Float ("32.1"). Doublevalue (). can also be used: double.valueof ("32.1"). Doublevalue ()
② 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 getnumericvalue (char-ch) method
The API can be consulted specifically.

Converting instances

1 ) Basic type conversion to class type

Forward conversion: A new class-type variable with a class wrapper
Integer a= new Integer (2);
Reverse conversions: Converting through a class wrapper
int B=a.intvalue ();

Through the class wrapper--basic data type
Eg1:int I=integer.parseint ("123")
Description: This method can only be used to convert a string into an integer variable
Eg2:float f=float.valueof ("123"). Floatvalue ()
Note: The previous example converts a string into a float object and then calls the object's Floatvalue () method to return its corresponding float value.
Eg3:booleanb=boolean.valueof ("123"). Booleanvalue ()
Note: The previous example converts a string into a Boolean object and then calls the object's Booleanvalue () method to return its corresponding Boolean value.
Eg4:double d=double.valueof ("123"). Doublevalue ()
Note: The previous example converts a string into a double object, and then calls the object's Doublevalue () method to return its corresponding double value.
Eg5:long l=long.valueof ("123"). Longvalue ()
Note: The previous example converts a string into a long object, and then calls the object's Longvalue () method to return its corresponding long value.
Eg6:char=character.valueof ("123"). Charvalue ()
Note: The previous example converts a string into a character object, and then calls the object's Charvalue () method to return its corresponding char value.

2) Conversion of basic types to strings

Forward conversions:
As follows:
System.out.println ("" +2+3);//"" "" to turn 2 into a string operation;
System.out.println (2+3); There is no conversion.
System.out.println (2+3+ "");//The first two values are added and then "" "is converted to a string.
System.out.println ("+3");//The same as the first one.

The output is displayed as: 23,5,5,23

3 ) class type to string conversion

Forward conversions: Because each class is a subclass of the object class, and all object classes have a ToString () function, the tostring () function can be converted to
Reverse conversions: A new class-type variable is created from the class wrapper
Eg1:int i=integer.valueof ("123"). Intvalue ()
Note: The previous example converts a string into an integer object, and then calls the object's Intvalue () method to return its corresponding int value.
Eg2:float f=float.valueof ("123"). Floatvalue ()
Note: The previous example converts a string into a float object and then calls the object's Floatvalue () method to return its corresponding float value.
Eg3:boolean b=boolean.valueof ("123"). Booleanvalue ()

Description: The previous example converts a string into aBoolean object, and then call the object'sThe Booleanvalue () method returns its correspondingA Boolean value.
Eg4:double d=double.valueof (“123 "). Doublevalue ()
Description: The previous example converts a string into a double object, and then calls the object's doublevalue () method to return its corresponding double value.
eg5:long l=long.valueof ( "123< Span lang= "en-us" > "). Longvalue ()
Description: The previous example converts a string into a longvalue () method of this object to return its corresponding long value.
eg6:char=character.valueof ( " "). Charvalue ()
Description: The previous example converts a string into a character object, and then calls the object's charvalue () method to return its corresponding char value.

Data types in Java and conversions between 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.