(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. spark运行模式之二:Spark的Standalone模式安装部署

    Spark运行模式 Spark 有很多种模式,最简单就是单机本地模式,还有单机伪分布式模式,复杂的则运行在集群中,目前能很好的运行在 Yarn和 Mesos 中,当然 Spark 还有自带的 Stan ...

  2. Java--23种设计模式之decorator模式

    装饰模式:装饰模式以对客户端透明的方式扩展对象的功能,是继承关系的一个替代方案,提供比继承更多的灵活性.动态给一个对象增加功能,这些功能可以再动态的撤消.增加由一些基本功能的排列组合而产生的非常大量的 ...

  3. c++函数模板1

    1 定义: 函数模板 只适用于参数个数相同但是类型不同 而且函数体相同的情况 2 这个例子没有使用模板的情况 #include <iostream> using namespace std ...

  4. TypeScript完全解读(26课时)_17.装饰器

    实验性的特性,需要在tslint里面把这项设置为true 作用域类的声明方法.访问符.属性和参数上 使用@符号加一个名字来定义,名字必须是一个函数,或者求值后是一个函数 装饰器工厂,setPro当做一 ...

  5. Eclipse中建立自己的类库,给不同的工程使用

    win7 进入服务 开始 运行 services.msc 在多个工程当中,可能使用到相同的jar包,这时,如果我们建立一个自己的类库,该类库中存放着所有工程均需要的jar包,就可以免去重复导入的麻烦. ...

  6. org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save()

    org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before ...

  7. u3d shader学习笔记1

    促使我学习SHADER的重要原因是希望深入理解3D渲染的机制,在此基础上可以灵活达到某种效果与性能的平衡,开发出具有良好体验的VR应用. 因为VR应用体验的好坏,直接由游戏的帧率决定,而游戏的帧率则受 ...

  8. 解决MySql报错:1130 - Host 'xxx' is not allowed to connect to this MySQL server的方法

    发现问题 使用Navicat连接MySql数据库时,未能成功,提示信息如下图: 这个错误提示已经很明确了,"不允许主机'desktop-teat9ob'连接到此mysql服务器", ...

  9. PHP 数组序列化,转为字符串

    serialize() <?php $a = array('a' => 'as' ,'b' => 'bs' , 'c' => 'cs'); //序列化数组 $s = seria ...

  10. Some JPR highlights (JPR 2019 March)

    Journal Name:Journal of Proteome Research Issue:2019 March Shared by: Weining Zhao 1.     Acetylome: ...