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. 使用pymysql循环删除重复数据,并修改自增字段偏移值

    创建表: CREATE TABLE `info` ( `id` tinyint NOT NULL AUTO_INCREMENT, `name` varchar(20) NOT NULL, PRIMAR ...

  2. YsoSerial 工具常用Payload分析之URLDNS

    本文假设你对Java基本数据结构.Java反序列化.高级特性(反射.动态代理)等有一定的了解. 背景 YsoSerial是一款反序列化利用的便捷工具,可以很方便的生成基于多种环境的反序列化EXP.ja ...

  3. python + pytest基本使用方法(断言)

    #pytest 的基本用法# 安装: pip install pytest#在当前目录下运行 : 输入 pytest# 1.断言#功能:用于计算a与b相加的和def add(a,b): return ...

  4. 微信小程序云开发-云函数-云函数获取参数并实现运算

    1.编写加法运算的云函数addData 2.在本地小程序页面调用云函数

  5. 在线体验 Windows 11「GitHub 热点速览 v.21.30」

    作者:HelloGitHub-小鱼干 有什么比无需安装系统,检测硬件兼容度,只要打开一个浏览器,输入某个神秘的地址回车,即可体验 Windows 11 更棒的呢?windows11 就是这么一个小工具 ...

  6. Spring事务管理中的配置文件(三)

    在开发中,遇到了sql语句报错,但是并没有回滚的情况. 经过几天的排查,终于找到了事务没有回滚的原因. 原来的项目用的是informix的数据库,原来针对事务回滚的机制都是好用的.我本地用的是mysq ...

  7. 第3天 IDEA 2021简单设置与优化 Java运算符 包机制

    IDEA 2021简单设置与优化 将工具条显示在上方 View–>Appearance–>Toolbar 鼠标悬停显示 File–>setting–>Editor–>Ge ...

  8. 本地Git项目搭建和文件操作

    Git项目搭建 git init ---在该文件夹下进入cmd/terminal git clone [url] ---克隆远程仓库到本地 Git文件操作 文件的四种状态: · Untracked:未 ...

  9. kali linux重启网卡失败:Job for networking.service failed because the control process exited with error code. See "systemctl status networking.service" and "journalctl -xe" for details. 问题排查

    linux菜鸡的时候,总是为了配置网络而烦恼,重启网卡的原因有很多,我这次是因为配置了固定IP[使用第三方工具连接]所以需要重启网卡,出现 Job for networking.service fai ...

  10. CVE-2020-1472

    主要记下流程 我擦,这个洞mimikatz也可以搞 但是我就不记录了 检验漏洞是否存在 https://github.com/SecuraBV/CVE-2020-1472 先获取域控信息 然后测试能否 ...