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 转成字串的方法大同小异.

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 cn.com.lwkj.erts.register;

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. java中string和int互相转化

    1 怎样将字串 String 转换成整数 int? A. 有两个方法: 1). int i = Integer.parseInt([String]); 或 i = Integer.parseInt([ ...

  2. java中string和int互相转化 (转)

    1 如何将字串 String 转换成整数 int? A. 有两个方法: 1). int i = Integer.parseInt([String]); 或 i = Integer.parseInt([ ...

  3. Java学习之String与int的相互转换

    •String 转 int 两种方式 int a = Integer.parseInt(s);int b = Integer.valueOf(s).intValue(); 代码 public clas ...

  4. string,char*,int 之间的转化

    c++中经常遇到string,char*,int之间的相互转化,今天就来整理一下. 以下是转载并修改的内容: 以下是常用的几种类型互相之间的转换 string 转 int先转换为char*,再使用at ...

  5. java中String和int的互相转化

    1. String 转 int 方式1:Integer.parseInt(); 方式2: Integer.valueOf(myStr).intValue(); 2.  int 转String 方式1: ...

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

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

  7. C++ int转string / string转int

    c++ 最近标准添加了stringstream类,可以非常简单的对int 和 string 进行相互的转化 //int 转 string void int2str(const int& i, ...

  8. C语言中string char int类型转换

    C语言中string -- ::) 转载 ▼ 标签: 操作符 int char c语言 类型转换 分类: C/Cpp ,char型数字转换为int型 "; printf(]-');//输出结 ...

  9. Cstring转char、string、int等数据类型的方法(转载)

    Cstring转char.string.int等数据类型的方法 (-- ::) 转载 标签: 杂谈 分类: VC CString 转char * CString cstr; char *p = (LP ...

随机推荐

  1. springboot的使用体验和思考

    首先,写这篇博客的背景: 1,通过maven使用springboot创建项目,进行了简单的页面跳转,并未编写service和DAL层,也就是说,并未整合持久化框架 2,阅读了maven的官方文档.sp ...

  2. Mysql 主外键与索引之间的区别和联系

    系数据库依赖于主键,它是数据库物理模式的基石.主键在物理层面上只有两个用途: 惟一地标识一行. 作为一个可以被外键有效引用的对象. 索引是一种特殊的文件(InnoDB数据表上的索引是表空间的一个组成部 ...

  3. Educational Codeforces Round 13——D. Iterated Linear Function(矩阵快速幂或普通快速幂水题)

      D. Iterated Linear Function time limit per test 1 second memory limit per test 256 megabytes input ...

  4. P1282 多米诺骨牌 (差值DP+背包)

    题目描述 多米诺骨牌有上下2个方块组成,每个方块中有1~6个点.现有排成行的 上方块中点数之和记为S1,下方块中点数之和记为S2,它们的差为|S1-S2|.例如在图8-1中,S1=6+1+1+1=9, ...

  5. 【2018.10.20】CXM笔记(思维)

    1. 给你个环状字符串,问从哪个地方拆开能使它的字典序最小. 先预处理任意子串的哈希值. 然后枚举拆点,将它与当前最优的拆点比较谁更优(就是从哪拆的字典序更小),具体方法是二分+哈希找出两串最长的相同 ...

  6. 【noip】noip201503求和(题解可能不完美,但绝对详细)

    3. 求和 难度级别:B: 运行时间限制:1000ms: 运行空间限制:51200KB: 代码长度限制:2000000B 题目描述   一条狭长的纸带被均匀划分出了n个格子,格子编号从1到n.每个格子 ...

  7. 如何解决"The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path"

    今天我在eclipse上搭建新项目时,莫名其妙的出现这个错误,如下: The superclass "javax.servlet.http.HttpServlet" was not ...

  8. Spoj-BLMIRINA Archery Training

    Mirana is an archer with superpower. Every arrow she shoots will get stronger the further it travels ...

  9. jenkins异常解答

    1.安装插件时offline 需要更换插件管理中的升级URL   http://mirror.xmission.com/jenkins/updates/current/update-center.js ...

  10. Python入门--10--序列

    一.与列表.元祖的相同与不同 1.都可以通索引得到元素 2.默认索引从0开始 3.可以通过分片得到一个范围内的元素集合 4.有很多共同的操作符 二. 1.list()这个函数用法 a="we ...