Data type conversions in Java

Source: Internet
Author: User

1. String to 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 string is converted to Double, Float, and Long in a similar way.


2 How do I convert an integer int to a string string?


A. there are three ways:

1.) String s = string.valueof (i);

2.) String s = integer.tostring (i);

3.) String s = "" + I;

Note: Double, Float, Long turn into a string in a similar way.

JAVA Data type Conversions
Keyword type conversions
SOURCE

This is an example of the conversion of data numbers in Java. For everyone to learn

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);
}


}


JAVA Common data type conversion functions in
Although all can be found in the Java API, tidy up to 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 of the following:
int The length data types are: Byte (8bits), short (16bits), int (32bits), Long (64bits) ,
float length data types are: Single precision (32bits float), double precision (64bits double)
Boolean the values of type variables are: ture, false
Char data types are: Unicode characters, 16-bit
Corresponding class types: Integer, Float, Boolean, Character, Double, short, Byte, Long

Conversion principles

Convert from low precision to high precision
byte , short, int, long, float, double, Char
Note: Two char operations are automatically converted to int, and when char and other types are operated, they are automatically converted to int, and then other types of auto-conversions

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 ();

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.

Conversion of a primitive type to a string
Forward conversions:
such as: int a=12;
String b;b=a+ "";

Reverse Conversions:
through the class wrapper
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: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

Transferred from: http://blog.csdn.net/xiaosan1987925/article/details/3245270

Data type conversions in Java

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.