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

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

The following coercion type Conversions. Such as:

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

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 (booleanvalue), Character (charvalue), Integer (intvalue), Long (longvalue), Float (floatvalue), Double ( Doublevalue)
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);
--------------string as a value,-----to Other types of conversions
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 a primitive type to a string
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 ()
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.



Data types in Java and how to convert between them

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.