Concise Java Data type conversions

Source: Internet
Author: User
Data | data type | Conversion 1 How do I convert string 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: string to Double, Float, Long is a very similar method.








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








A. There are three methods:





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





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





3.) String s = "" + I;





Note: Double, Float, Long into a string of methods are very similar.











Java data type conversion Ynniebo [Favorites]


keyword type conversion


Source





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





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
can be found in the Java API, organize 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








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.