http://blog.sina.com.cn/s/blog_4f9d6b1001000bfo.html

int -> String

int i=12345;

String s="";

第一种方法:s=i+"";

第二种方法:s=String.valueOf(i);

这两种方法有什么区别呢?作用是不是一样的呢?是不是在任何下都能互换呢?

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)),也会抛异常,但会多产生一个对象

--------------------------------------------------------------------
1如何将字串 String 转换成整数
int?



A. 有两个方法:



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

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



2). int i = Integer.valueOf(my_str).intValue();



注: 字串转成 Double, Float, Long 的方法大同小异.
2 如何将整数 int 转换成字串 String
?
A. 有叁种方法:



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



2.) String s = Integer.toString(i);



3.) String s = "" + i;



注: Double, Float, Long 转成字串的方法大同小异.







JAVA数据类型转换 



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



package shenmixiaozhu;

import java.sql.Date;

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

   }
}
JAVA中常用数据类型转换函数

虽然都能在JAVA API中找到,整理一下做个备份。

String与Int类型的转换的更多相关文章

  1. 03.枚举和string以及int类型之间的转换

    练习1:   将枚举类型强转成int类型 namespace _04.枚举类型的练习01 { //声明一个QQState类型的枚举 public enum QQState { OnLine, OffL ...

  2. java和python中的string和int数据类型的转换

    未经允许,禁止转载!!! 在平时写代码的时候经常会用到string和int数据类型的转换 由于java和python在string和int数据类型转换的时候是不一样的 下面总结了java和python ...

  3. 【转】java中byte数组与int类型的转换(两种方式)----不错

    原文网址:http://blog.csdn.net/piaojun_pj/article/details/5903009 java中byte数组与int类型的转换,在网络编程中这个算法是最基本的算法, ...

  4. C# enum、int、string三种类型互相转换

    enum.int.string三种类型之间的互转 #代码: public enum Sex { Man=, Woman= } public static void enumConvert() { in ...

  5. 总结:String类型与Int类型的转换【实现插入操作主键自增】

    1.String类型(此类型是数字格式的字符串类型)转换成Int类型 String str = "10000"; 转换成Int类型: int num = Integer.parse ...

  6. java 13-4 Integer和String、int之间的转换,进制转换

    1.int类型和String类型的相互转换 A.int -- String 推荐用: public static String valueOf(int i) 返回 int 参数的字符串表示形式. B. ...

  7. String与其他类型的转换

    首先,对于String类有一点是毫无疑问的:对String对象的任何改变都不影响到原对象,相关的任何change操作都会生成新的对象. 一. String与StringBuilder.StringBu ...

  8. C++ 中 string, char*, int 类型的相互转换

    一.int 1.int 转换成 string 1) to_string函数 —— c++11标准增加了全局函数std::to_string: string to_string (int val); s ...

  9. String,Integer,int类型之间的相互转换

    String, Integer, int 三种类型之间可以两两进行转换 1. 基本数据类型到包装数据类型的转换 int -> Integer (两种方法) Integer it1 = new I ...

随机推荐

  1. 传统.NET 4.x应用容器化体验(4)

    上一篇我们试着将.NET 4.x的镜像推送到harbor私有镜像仓库,本篇我们来使用一下阿里云的镜像仓库服务并了解一下携程的实践. 1 关于阿里云镜像仓库 阿里云容器镜像服务(简称 ACR)是面向容器 ...

  2. 大疆M3508、M2006必备CAN总线知识与配置方法

    使用大疆M3508.M2006的CAN总线知识与配置方法 目录 使用大疆M3508.M2006的CAN总线知识与配置方法 前言: 0x00 需要额外的CAN收发器!!! 0x01 硬件层面分析 为什么 ...

  3. Spring RestTemplate 之中文乱码

    由于RestTemplate的默认构造方法初始化的StringHttpMessageConverter的默认字符集是ISO-8859-1,所以导致RestTemplate请求的响应内容会出现中文乱码. ...

  4. 【模拟】选数 luogu-1037

    AC代码 #include <bits/stdc++.h> using namespace std; #define ms(a,b) memset(a,b,sizeof(a)) typed ...

  5. 本地项目的npm安装方法

    有些node项目如一些工具类的项目,安装以后通过命令行执行其功能.但是而对于本地自建的项目如何通过npm安装,然后通过命令行(项目定义了命令行)工具执行命令调用其功能呢? 对于这种情况,笔者主要通过两 ...

  6. sqlite无法保存数据

    springboot使用连接sqlite数据,使用jdbc:sqlite::resource:db/XXX.db方式连接数据库为只读,使用绝对路径才可以写入.

  7. 简单图解OSI七层网络模型

    Open Systems Interconnection(OSI)定义了一个网络框架:其以层为单位实现了各种协议,同时会将控制权逐层传递. 目前OSI主要作为教学工具被使用,其在概念上将计算机网络结构 ...

  8. Python基础之实现界面和代码分离

    第一步:用QT Designer画一个TreeWidget,存为treeview4.ui,这个处理前面TreeWidget那一节讲过,这里不细讲 treeview4.py # -*- coding: ...

  9. [源码解析] 机器学习参数服务器ps-lite (1) ----- PostOffice

    [源码解析] 机器学习参数服务器ps-lite 之(1) ----- PostOffice 目录 [源码解析] 机器学习参数服务器ps-lite 之(1) ----- PostOffice 0x00 ...

  10. [WinError 10013]以一种访问权限不允许的方式做了一个访问套接字的尝试

    Django报错截图如下: 原因分析:出现这种情况在Windows中很常见,就是端口被占用 解决步骤: 1:进入windows中的命令行窗口(win+R之后输入cmd就可以进去)   2:输入 net ...