Data type conversion in Java integer string long float double date char

Source: Internet
Author: User
Tags float double
I just learned Java, but I have a poor foundation. I used data type conversion and found it online. Convert from http://hi.baidu.com/loveland/blog/item/eb2d96456e64d13a8694737b.html 1 how to convert string to integer Int? A. There are two methods: 1). Int I = integer. parseint ([String]); or
I = integer. parseint ([String], [int Radix]); 2 ). int I = integer. valueof (my_str ). intvalue (); note: the methods for converting a string to double, float, and long are similar. 2. How do I convert an integer int to a string? A. there are three methods: 1 .) string S = string. valueof (I); 2 .) string S = integer. tostring (I); 3 .) string S = "" + I; note: the conversion methods of double, float, and long into strings are similar. conversion of Java data types to ynniebo [favorites]
Keyword type conversion
Source

This is an example of data type conversion in Java.

Package cn.com. lwkj. ERTs. Register;
Import java. SQL. date;
Public class typechange {
Public typechange (){
}
// Change the string type to the int type
Public static int stringtoint (string intstr)
{
Integer integer;
Integer = integer. valueof (intstr );
Return integer. intvalue ();
}
// Change int type to the string type
Public static string inttostring (INT value)
{
Integer integer = new INTEGER (value );
Return integer. tostring ();
}
// Change the string type to the Float Type
Public static float stringtofloat (string floatstr)
{
Float floatee;
Floatee = float. valueof (floatstr );
Return floatee. floatvalue ();
}
// Change the float type to the string type
Public static string floattostring (float value)
{
Float floatee = new float (value );
Return floatee. tostring ();
}
// Change the string type to the sqldate type
Public static java. SQL. Date stringtodate (string datestr)
{
Return java. SQL. Date. valueof (datestr );
}
// Change the sqldate type to the string type
Public static string datetostring (Java. SQL. Date datee)
{
Return datee. tostring ();
} Public static void main (string [] ARGs)
{
Java. SQL. Date Day;
Day = typechange. stringtodate ("2003-11-3 ");
String strday = typechange. datetostring (day );
System. Out. println (strday );
} Common data type conversion functions in Java
Although they can all be found in Java API, sort out and make a backup. String-> byte
Byte static byte parsebyte (string s) byte-> string
Byte static string tostring (byte B) Char-> string
Character static string to string (char c) string-> short
Short static short parseshort (string s) Short-> string
Short static string tostring (short S) string-> integer
Integer static int parseint (string s) integer-> string
Integer static string tostring (int I) string-> long
Long static long parselong (string s) Long-> string
Long static string tostring (long I) string-> float
Float static float parsefloat (string s) float-> string
Float static string tostring (float f) string-> double
Double static double parsedouble (string s) Double-> string
Double static string tostring (double)

++ ++

Data Type

There are four basic types:
Int data types include: byte (8 bits), short (16 bits), INT (32 bits), long (64 bits ),
Float data types include: single precision (32 bits float) and double precision (64 bits double)
Values of boolean type variables include true and false.
Char data types include: Unicode characters, 16 characters
Corresponding class types: integer, float, Boolean, character, double, short, byte, long

Conversion Principle

Conversion from low-precision to high-precision
Byte, short, Int, long, float, double, char
Note: When two char operations are performed, they are automatically converted to the int type. When char and other types are operated, they are automatically converted to the int type before other types are automatically converted.

Convert basic types to class 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 ();

Class type conversion to string

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.
Eg1: int I = integer. valueof ("123"). intvalue ()
Note: In the above example, a string is converted into an integer object, and then the intvalue () method of this object is called to return the corresponding int value.
Eg2: 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.
Eg3: Boolean B = Boolean. valueof ("123"). booleanvalue ()
Note: The preceding example is to convert a string into a Boolean object, and then call the booleanvalue () method of this object to return its corresponding Boolean value.
Eg4: Double D = double. valueof ("123"). doublue ()
Note: The preceding example is to convert a string into a double object, and then call the doublue () method of this object to return its corresponding double value.
Eg5: Long L = long. valueof ("123"). longvalue ()
Note: In the above example, a string is converted into a long object, and then the longvalue () method of this object is called to return the corresponding long value.
Eg6: Char = character. valueof ("123"). charvalue ()
Note: The preceding example is to convert a string into a character object, and then call the charvalue () method of this object to return the corresponding char value.

Conversion from basic type to string
Forward conversion:
For example, int A = 12;
String B; B = a + "";

Reverse conversion:
Package through class
Eg1: int I = integer. parseint (123 ")
Note: This method can only be used to convert strings into integer variables.
Eg2: 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.
Eg3: Boolean B = Boolean. valueof ("123"). booleanvalue ()
Note: The preceding example is to convert a string into a Boolean object, and then call the booleanvalue () method of this object to return its corresponding Boolean value.
Eg4: Double D = double. valueof ("123"). doublue ()
Note: The preceding example is to convert a string into a double object, and then call the doublue () method of this object to return its corresponding double value.
Eg5: Long L = long. valueof ("123"). longvalue ()
Note: In the above example, a string is converted into a long object, and then the longvalue () method of this object is called to return the corresponding long value.
Eg6: Char = character. valueof ("123"). charvalue ()
Note: In the preceding example, a character string is converted into a character object, and then

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.