Java Common data type conversions

Source: Internet
Author: User

In the Java development process often involves the conversion of data types, such as digital to character type, character to date type, string to array and so on, and other types of coercion and so on. Often appear, so it is necessary to summarize.

1. How can I convert string strings 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 string is converted to Double, Float, and Long in the same way. 2 How do I convert an integer int to a 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 turn into a string in the same way.
This is an example of the conversion of data numbers in Java. For everyone to learn to lead the 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);
}++++++++++++++++++++++++++++++++++++++++++++++++++++++

Data type

Basic types have the following 8 kinds: Integer types are: Byte (8bits), short (16bits), int (32bits), Long (64bits), float type: single precision (32bits float), double precision (64bits Double) Boolean variable has: Boolean value ture, false character type: Char Unicode character, class type corresponding to 16-bit base type: Integer, Float, Boolean, Character, Double, short , Byte, Longreference data type (Reference type):class, interface, arrayReference type variable name = new reference type name (parameter); The constructor of the class is usually followed by new

Conversion principles

Basic Type Auto-conversion:convert from low precision to high precisionBYTE, 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: Use the class wrapper to new a variable of class type integer a= new integer (2); reverse conversion: Convert int b=a.intvalue () through a class wrapper;

class type to string conversion

Forward conversions: Because each class is a subclass of the object class, and all of the object classes have a ToString () function, you can convert by using the ToString () function: A new class-type variable with the class wrapper newstring to basic typeEg1: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.
Common data type conversions
1. String to array/** * string to array without split symbol * @param str */Public char [] stringtoarry (String str)               {char[] Chararry =str. ToCharArray ();       return chararry; }/** * string to array with split symbol * @param str * @param separator */public string[] Stringtoarry (String str               , String separator) {string[] arry= str.split (separator);       return arry; }2, ArrayList and array interchange eg. /** * List to one-dimensional array * @param arrayList * @return * @author Zhengshuheng */Public St            Ring[] Listtoarry (arraylist<string> ArrayList) {string[] array= new string[arraylist. Size ()];            array= Arraylist.toarray (array); System.            Out.print ("{");                           for (int i = 0; i < Array.Length; i++) {string string = Array[i]; System.                            Out.print (string); if (I<array.length-1) {System. Out.print (",");                           } else {System. Out.print (""); }} System.            Out.print ("}"); System.            Out.println ();    return array;           }/** * List to two-dimensional array * @param arraylist * @return * @author Zhengshuheng                     */Public string[][] Listtoarray (arraylist<string[]> ArrayList) {                     String[][] array= New String[arraylist.size () [];                     Array=arraylist.toarray (array); System.                      Out.print ("{");                           for (int i = 0; i < Array.Length; i++) {string[] objects = array[i]; System.                            Out.print ("{");                        for (int j = 0; J < Objects.length; J + +) {          String string = Objects[j]; System.                                   Out.print (string);                                  if (j<objects.length-1) {System. Out.print (",");                                  } else {System. Out.print (""); }} System.                            Out.print ("}");                           if (i<array.length-1) {System. Out.print (",");                           } else {System. Out.print (""); }} System.                     Out.print ("}"); System.                      Out.println ();              return array; }/** * Array goto list * @param array *@return * @author Zhengshuheng */Public arraylist<string> arrytolist (stri Ng[] array) {arraylist<string> list= new arraylist<string> (Arrays.aslist (arr                     ay)); System.                      Out.print ("[");                           for (int i = 0; i < list.size (); i++) {string string =list.get (i); System.                            Out.print (string);                           if (i<array.length-1) {System. Out.print (",");                           } else {System. Out.print (""); }} System.                     Out.print ("]"); System.                      Out.println ();              return list;          } Test: public static void Main (string[] args) {//TODO auto-generated method stub    arraylist<string[]> arraylist= new arraylist<string[]> ();              String[] array1={"1", "2", "3", "4"};              String[] array2={"A", "B", "C"};              Arraylist.add (array2);              Arraylist.add (array1);              arraylist<string> arraylist2= new arraylist<string> ();              Arraylist2.add ("A");              Arraylist2.add ("B");              Arraylist2.add ("C");              Arraylist2.add ("D");              Demo1 demo= new Demo1 ();              String[][] Array=demo.listtoarray (ArrayList);              String[] Array3=demo.listtoarry (ARRAYLIST2);               Arraylist<string> arraylist3=demo.arrytolist (array1); } output: {{a,b,c},{1,2,3,4}}{a,b,c,d}[1,2,3,4]

  

Java Common data type conversions

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.