(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. Windows Vista for Developers——第四部分:用户帐号控制(User Account Control,UAC)

    作者:Kenny Kerr 翻译:Dflying Chen 原文:http://weblogs.asp.net/kennykerr/archive/2006/09/29/Windows-Vista-f ...

  2. CSS:CSS cursor 属性

    ylbtech-CSS:CSS cursor 属性 1.返回顶部 1. 实例 一些不同的光标: span.crosshair {cursor:crosshair;} span.help {cursor ...

  3. bzoj1941

    KD-tree **了这道题 这个估价函数好鬼畜,把min打成max... 关于min的估价函数非常鬼畜,具体我也不知道为什么. #include<bits/stdc++.h> using ...

  4. 关于group_concat函数拼接字符超长的问题

    昨天测试的人火急火燎的找我,跟我说数据不对!说明情况后我去查看,原来是数据上有个子查询出来的字段没有完全展示 问题很明显,就是数据被截断了.下面贴上我写的查询 wyids_是正确的显示,通过它子查询出 ...

  5. MongoDB -- 安装(win 10)

    1. 下载安装包: mongodb-win32-x86_64-2008plus-ssl-4.0.10-signed.msi https://www.mongodb.com/download-cente ...

  6. Unity3D 开发ios时困扰多时游戏开始画面图片的分辨率

  7. java集合框架之HashCode

    参考http://how2j.cn/k/collection/collection-hashcode/371.html List查找的低效率 假设在List中存放着无重复名称,没有顺序的2000000 ...

  8. 5-1条件运算符 & 5-2

    三目运算符 新建类: ConditionDemo 用三目运算符: package com.imooc.operator; public class ConditionDemo { public sta ...

  9. LeetCode: 500 Keyboard Row (easy)

    题目: Given a List of words, return the words that can be typed using letters of alphabet on only one ...

  10. html上传多图并预览

    涉及知识:base64处理图片,ajax,js,thinkphp 效果图: 代码实现: html: <!DOCTYPE html> <html> <head> &l ...