java中int ,float ,double,String,Interget等數(shù)據(jù)類型的相互轉(zhuǎn)換

2018-10-23 14:26 更新

java數(shù)據(jù)類型的相互轉(zhuǎn)換 -=

1.string和int互相轉(zhuǎn)化

1.1 如何將字串 String 轉(zhuǎn)換成整數(shù) int, double, float, long?

A. 有兩個(gè)方法:

1) int i = Integer.parseInt([String]); 或 int i = Integer.parseInt([String],[int radix]);

2) int i = Integer.valueOf(my_str).intValue(); 注1:這兩種種方法有什么區(qū)別呢?

1) i=Integer.parseInt(s);//直接使用靜態(tài)方法,不會(huì)產(chǎn)生多余的對(duì)象,但會(huì)拋出異常 2) i=Integer.valueOf(s).intValue();//Integer.valueOf(s) 相當(dāng)于 new Integer(Integer.parseInt(s)),也會(huì)拋異常,但會(huì)多產(chǎn)生一個(gè)對(duì)象

注2: String 轉(zhuǎn)成 double, float, long 的方法大同小異.

1)double d = Double.parseDouble("1.0");

2)float f = Float.parseFloat("1.0");;

3)long L = Long.parseLong("1.0");

1.2 如何將整數(shù) int 轉(zhuǎn)換成字串 String ?

A. 有叁種方法:

1.) String s = String.valueOf(i);

2.) String s = Integer.toString(int i); String s = Double.toString(double d); String s = Float.toString(float f); String s = Long.toString(long L);

3.) String s = "" + i; 注1:這兩種種方法有什么區(qū)別呢?

1) s=i+""; //會(huì)產(chǎn)生兩個(gè)String對(duì)象 2) s=String.valueOf(i); //直接使用String類的靜態(tài)方法,只產(chǎn)生一個(gè)對(duì)象

2.string 和 Integer, Double, Float, Long互相轉(zhuǎn)化

2.1 如何將字串 String 轉(zhuǎn)換成整數(shù) Integer, Double, Float, Long?

A. 有兩個(gè)方法:

1) Integer i = Integer.valueOf(String num); 2) Double d = Double.valueOf(String num); 3) Float f = Float.valueOf(String num); 4) Long l = Long.valueOf(String num);

2.2 如何將整數(shù) Integer, Double, Float, Long 轉(zhuǎn)換成字串 String ? 與1.2相同

這是一個(gè)例子,說的是JAVA中數(shù)據(jù)數(shù)型的轉(zhuǎn)換.供大家學(xué)習(xí)引

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

JAVA中常用數(shù)據(jù)類型轉(zhuǎn)換函數(shù) 雖然都能在JAVA API中找到,整理一下做個(gè)備份。

以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)