(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中数据数型的转换.供大家学习

  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. public static void main(String[] args)
  41. {
  42. java.sql.Date day ;
  43. day = TypeChange.stringToDate("2003-11-3");
  44. String strday = TypeChange.dateToString(day);
  45. System.out.println(strday);
  46. }
  47. }

字符串、String等转换的更多相关文章

  1. HTML5 Blob与ArrayBuffer、TypeArray和字符串String之间转换

    1.将String字符串转换成Blob对象 //将字符串 转换成 Blob 对象 var blob = new Blob(["Hello World!"], { type: 'te ...

  2. [转] HTML5 Blob与ArrayBuffer、TypeArray和字符串String之间转换

    1.将String字符串转换成Blob对象 //将字符串 转换成 Blob 对象 var blob = new Blob(["Hello World!"], { type: 'te ...

  3. C# 字符串string类型转换成DateTime类型 或者 string转换成DateTime?(字符串转换成可空日期类型)

    在c#中,string类型转换成DateTime类型是经常用到的,作为基本的知识,这里在此做个小结.一般来说可以使用多种方法进行转换,最常用的就是使用Convert.ToDateTime(string ...

  4. c#中字节数组byte[]、图片image、流stream,字符串string、内存流MemoryStream、文件file,之间的转换

    字节数组byte[]与图片image之间的转化 字节数组转换成图片 public static Image byte2img(byte[] buffer) { MemoryStream ms = ne ...

  5. java算法:统计数字-将数字转换成字符串,然后使用字符串String.valueOf()方法进行判断

    题目: 计算数字 k 在 0 到 n 中的出现的次数,k 可能是 0~9 的一个值. 样例 样例 1: 输入: k = 1, n = 1 输出: 1 解释: 在 [0, 1] 中,我们发现 1 出现了 ...

  6. java字符数组char[]和字符串String之间的转换

    java字符数组char[]和字符串String之间的转换 觉得有用的话,欢迎一起讨论相互学习~Follow Me 使用String.valueOf()将字符数组转换成字符串 void (){ cha ...

  7. c/c++日期时间处理与字符串string转换

    转自:https://www.cnblogs.com/renjiashuo/p/6913668.html 在c/c++实际问题的编程中,我们经常会用到日期与时间的格式,在算法运行中,通常将时间转化为i ...

  8. C++中数字与字符串之间的转换 scanf string总结(复习必读)

    1 string的scanf读入操作 C++里面控制台输入直接使用cin操作就可以了:或者getline(istringstream,string); 字符和数字加减就是字符的ASCII码和数字直接加 ...

  9. Swift:字符串(String)分割之Substring优雅转换

    认识Substring类型 这是一个全新的类型,看类名像是String的子类,但是大家千万别被误导了,Substring并不是String的子类,这是两个不同的类型,但是它们都继承了StringPro ...

  10. Java基础——基本类型和包装类、基本类型和字符串之间的转换

    基本类型和包装类之间的转换 基本类型和包装类之间经常需要互相转换,以 Integer 为例(其他几个包装类的操作雷同哦): 在 JDK1.5 引入自动装箱和拆箱的机制后,包装类和基本类型之间的转换就更 ...

随机推荐

  1. C# 获取QQ群数据的实现

    一,分析 1,群数据获取 当访问http://qun.qq.com/air/#mygroup我们通过Fiddler可以查看到QQ群列表是从http://qun.qq.com/air/group/min ...

  2. TX1 ssh配置

    执行: sudo apt-get install openssh-server 验证: sudo ps -e |grep ssh 回车-->有sshd,说明ssh服务已经启,如果没有则输入命令s ...

  3. ubuntu 使用命令行清空回收站

    sudo rm -rf ~/.local/share/Trash/*

  4. Java中next() 与 nextLine() 区别

    next(): 1.一定要读取到有效字符后才可以结束输入. 2.对输入有效字符之前遇到的空白,next() 方法会自动将其去掉. 3.只有输入有效字符后才将其后面输入的空白作为分隔符或者结束符. 4. ...

  5. Eclipse中导入Hadoop源代码工程

    以hadoop 2.7.3为例, jdk1.7.0_80 , Eclipse Mars(4.5), Apache Maven 3.0.5   mvn时需要使用root权限,即县切换到root用户,用命 ...

  6. E - Jolly Jumpers

    E - Jolly Jumpers Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit  ...

  7. linux磁盘存储管理基本命令和工具

    1 磁盘在linux表示方法 (1) IDE硬盘:hd[a~z]x,主设备号+次设备号+磁盘分区编号/hd(0-n,y) (2)SCSI硬盘:sd[a~z]x/hd(0-n,y) 注:主设备号可以唯一 ...

  8. Flutter实战视频-移动电商-17.首页_楼层组件的编写技巧

    17.首页_楼层组件的编写技巧 博客地址: https://jspang.com/post/FlutterShop.html#toc-b50 楼层的效果: 标题 stlessW快速生成: 接收一个St ...

  9. jQuery 字母大小写转换

    "ABC".toLowerCase()//转小写 "abc".toUpperCase()//转大写

  10. Unity动画事件

    动画事件添加 var clip = new AnimationClip();//clip,动画剪辑  储存基于动画的关键帧.这里新建动画剪辑 clip.SetCurve ("", ...