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数据类型转换



keyword   类型转换



这是一个样例,说的是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定位。搜索结果排序是一个备份。

java在string和int相互转化的更多相关文章

  1. Java中String转int型的方法以及错误处理

    应要求,本周制作了一个判断一个年份是否是闰年的程序.逻辑很简单,这里就不贴代码了.可是,在这次程序编写中发现了一个问题. 在输入年份时,如果输入1)字母2)空3)超过Int上限时,就会抛excepti ...

  2. java中string和int互相转化

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

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

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

  4. java中String和int相互转换常用方法详解

    java中int和String的相互转换常用的几种方法: String  > int s="10";int i;第一种方法:i=Integer.parseInt(s);//默 ...

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

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

  6. Java中String和Int的相互转换

    一.将字串 String 转换成整数 intA. 有2个方法:1). int i = Integer.parseInt([String]); 或 i = Integer.parseInt([Strin ...

  7. java string和int之间的相互转化

    java 中string和int之间的相互转化 1 如何将字串 String 转换成整数 int? A. 有两个方法: 1). int i = Integer.parseInt([String]); ...

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

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

  9. java.lang.String

    1.String 是一个类,广泛应用于 Java 程序中,相当于一系列的字符串.在 Java 语言中 strings are objects.创建一个 strings 最直接的方式是 String g ...

随机推荐

  1. Oracle存储过程详解(引用)+补充(转) dbms_output包 good

    执行存储过程时,execute和call的区别 EXEC is a sqlplus command that put its argument as an anonymous pl/sql block ...

  2. HDU 4081-Parsing URL(水)

    Parsing URL Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others) Tota ...

  3. IT只忍者龟Photoshop简单人像的头发抠图过程

    一.导入素材,加入蒙版 1.导入美女图片 2.导入背景图片 如今须要将美女抠出来放在这个背景上,怎么办?一定会有人想到用通道,抽出.(备注:在ps6以后版本号,抽出已经没有这个选项了). 3.将美女图 ...

  4. PowerDesigner教程

    PowerDesigner是一款功能很强大的建模工具软件,足以与Rose比肩,相同是当今最著名的建模软件之中的一个.Rose是专攻UML对象模型的建模工具,之后才向数据库建模发展,而PowerDesi ...

  5. 数据库性能监测工具——SQL Server Profiler

    使用SQL Server Profiler 进行sql监控需要一些设置: 其他的就是进行分析了~ 清除SQL SERVER缓存 常用的方法: DBCC DROPCLEANBUFFERS 从缓冲池中删除 ...

  6. 笔试题&面试题:设计一个复杂度为n的算法找到单向链表倒数第m个元素

    设计一个复杂度为n的算法找到单向链表倒数第m个元素.最后一个元素假定是倒数第0个. 提示:双指针查找 相对于双向链表来说,单向链表仅仅能从头到尾依次訪问链表的各个节点,所以假设要找链表的倒数第m个元素 ...

  7. eclipse 构造 C++ 11 -- ubuntu 12.04

    设备g++ 4.8 sudo apt-get install python-software-properties sudo add-apt-repository ppa:ubuntu-toolcha ...

  8. 手机新闻网站,手持移动新闻,手机报client,jQuery Mobile手机新闻网站,手机新闻网站demo,新闻阅读器开发

    我们坐在地铁.经常拿出新浪手机查看新闻.腾讯新闻,或者看新闻,等刷微信功能.你有没有想过如何实现这些目标. 移动互联网.更活泼. 由于HTML5未来,jQuery Moblie未来. 今天我用jqm的 ...

  9. 阅读INI档 - Delphi一片

    程序往往需要读一些用户设置值.如何完成这一过程? B/S程序一般使用XML档.和C/S程序使用INI档. 前篇<C#迁移之callXBFLibrary - 2(调用非托管DLL)>是C#读 ...

  10. crawler_微信采集方案

    仅供参考