字符串、String等转换

 程序员的店http://paperman.taobao.com/

插个广告,小店刚开张,欢迎各位同道中人的亲光顾,照顾生意哈,我们要做技术上滴大牛,还要享受生活!酷




(1) String 转换为字符串
   例:
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数据类型转换 


        这是一个例子,说的是JAVA中数据数型的转换.供大家学习


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

		}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值