字符串、String等轉換

(1) String 轉換爲字符串java

   例:
String s = "abcde";
char[] a = s.toCharArray();



(2) 字符串轉換爲String
char[] a;
a = {a,b,c,d};
String s;
s = new String(a);



(3) int類型轉換爲String
   例:
int n = 0;
String s = String.valueOf(n)



(4)int -> String


int i=12345;
String s="";
第一種方法:s=i+"";
第二種方法:s=String.valueOf(i);
這兩種方法有什麼區別呢?做用是否是同樣的呢?是否是在任何下都能互換呢?



(5)String -> int


s="12345";
int i;
第一種方法:i=Integer.parseInt(s);
第二種方法:i=Integer.valueOf(s).intValue();

這兩種方法有什麼區別呢?做用是否是同樣的呢?是否是在任何下都能互換呢?
如下是答案:


第一種方法:s=i+"";   //會產生兩個String對象
第二種方法:s=String.valueOf(i); //直接使用String類的靜態方法,只產生一個對象


第一種方法:i=Integer.parseInt(s);//直接使用靜態方法,不會產生多餘的對象,但會拋出異常
第二種方法:i=Integer.valueOf(s).intValue();//Integer.valueOf(s) 至關於 new Integer(Integer.parseInt(s)),也會拋異常,但會多產生一個對象
 


 
    JAVA數據類型轉換 


web

        這是一個例子,說的是JAVA中數據數型的轉換.供你們學習sql

 

 

[java]   view plain copy
  1. public class TypeChange {  
  2.            public TypeChange() {  
  3.            }  
  4.            //change the string type to the int type  
  5.            public static   int stringToInt(String intstr)  
  6.            {  
  7.              Integer integer;  
  8.              integer = Integer.valueOf(intstr);  
  9.              return integer.intValue();  
  10.            }  
  11.            //change int type to the string type  
  12.            public static String intToString(int value)  
  13.            {  
  14.              Integer integer = new Integer(value);  
  15.              return integer.toString();  
  16.            }  
  17.            //change the string type to the float type  
  18.            public static   float stringToFloat(String floatstr)  
  19.            {  
  20.              Float floatee;  
  21.              floatee = Float.valueOf(floatstr);  
  22.              return floatee.floatValue();  
  23.            }  
  24.            //change the float type to the string type  
  25.            public static String floatToString(float value)  
  26.            {  
  27.              Float floatee = new Float(value);  
  28.              return floatee.toString();  
  29.            }  
  30.            //change the string type to the sqlDate type  
  31.            public static java.sql.Date stringToDate(String dateStr)  
  32.            {  
  33.              return   java.sql.Date.valueOf(dateStr);  
  34.            }  
  35.            //change the sqlDate type to the string type  
  36.            public static String dateToString(java.sql.Date datee)  
  37.            {  
  38.              return datee.toString();  
  39.            }  
  40.   
  41.            public static void main(String[] args)  
  42.            {  
  43.              java.sql.Date day ;  
  44.              day = TypeChange.stringToDate("2003-11-3");  
  45.              String strday = TypeChange.dateToString(day);  
  46.              System.out.println(strday);  
  47.            }  
  48.   
  49.         }  
相關文章
相關標籤/搜索