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. 学习Git的基本业务逻辑

    1,基本业务逻辑(假设针对index.html文件中内容): 1,在init版本库之前已写好开头部分:index 对index进行git init版本库: 进入到文件夹中,git init git a ...

  2. Java 将Word转为Tiff

    本文以Java代码示例展示如何将Word文档转换为Tiff格式.代码方法比较简单,加载Word文档,并通过saveToTiff(tiffFilename)方法直接保存为tiff格式即可. 1.Java ...

  3. IO流之节点流(字节流)

    节点流可以分为:字节节点流和字符节点流 数据源直接到程序的成为节点流(低级流) 字节流 输入流----InputStream InputStream 是输入流的抽象父类,若创建对象,需new它的子类 ...

  4. 计算机基础-Socket

    计算机基础-Socket 当时明月在,曾照彩云归. 简介:计算机基础-Socket 一.I/O 模型 一个输入操作通常包括两个阶段: 等待数据准备好 从内核向进程复制数据 对于一个套接字上的输入操作, ...

  5. POJ3667 Hotel 题解

    和最大子段和的思路是一样的,可以记 \(lmax,rmax,dat\) 分别表示从当前区间最靠左/右的最大连续空子段和当前区间的最大连续空子段. 需要用延迟标记,每次遇到开房操作先ask,如果能找到就 ...

  6. 解决:CannotAcquireResourceException: A ResourcePool could not acquire a resource from its primary factory or source.

    log4j给出的异常信息有下面几句: Caused by: org.hibernate.exception.GenericJDBCException: Unable to acquire JDBC C ...

  7. 入门Kubernetes-数据存储

    一.Volume介绍: 在k8s中Pod的生命周期可能很短,会被频繁地销毁和创建.容器销毁时,保存在容器内部文件系统中的数据都会被清除. 为了持久化保存容器数据,k8s 提供了卷(Volume)的抽象 ...

  8. 如何使用SQL Server实现SignalR的横向扩展

    一般来说,Web应用的扩展有两种:scale up(纵向扩展)和scale out(横向扩展). 1.纵向扩展 使用配置高(大内存,多处理器)的服务器或者虚拟机. 2.横向扩展 使用多个服务器(Web ...

  9. Input 只能输入正数以及2位小数点

    <input onkeyup="this.value= this.value.match(/\d+(\.\d{0,2})?/) ? this.value.match(/\d+(\.\d ...

  10. Android 自定义属性(attrs)、样式(Style)、主题(Theme)

    Android 自定义属性(attrs).样式(Style).主题(Theme) https://www.cnblogs.com/dandre/p/4507024.html https://blog. ...