java中字符串String 转 int

  String -> int
  
  s="12345";
  int i;
  第一种方法:i=Integer.parseInt(s);
  第二种方法:i=Integer.valueOf(s).intValue();
  这两种方法有什么区别呢?作用是不是一样的呢?是不是在任何下都能互换呢?
  
  int -> String
  
  int i=12345;
  String s="";
  第一种方法:s=i+"";
  第二种方法:s=String.valueOf(i);
  这两种方法有什么区别呢?作用是不是一样的呢?是不是在任何下都能互换呢?
  
  
  以下是答案:
  
  
  第一种方法: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中字符串String 转 int(转)的更多相关文章

  1. Java中字符串string的数据类型

    Java中字符串string的数据类型 时间:2017-07-03 08:01:47 YuanMxy 原文:https://blog.csdn.net/YuanMxy/article/details/ ...

  2. Java内存管理-探索Java中字符串String(十二)

    做一个积极的人 编码.改bug.提升自己 我有一个乐园,面向编程,春暖花开! 一.初识String类 首先JDK API的介绍: public final class String extends O ...

  3. java中字符串String、StringBuilder、StringBuffer的常用方法

    String的常用方法: public static void main(String[] args) { String str = "Hello world!"; // 获取字符 ...

  4. 我说精通字符串,面试官竟然问我 Java 中的 String 有没有长度限制?

    String 是 Java 中很重要的一个数据类型,除了基本数据类型以外,String 是被使用的最广泛的了,但是,关于 String,其实还是有很多东西容易被忽略的. 就如本文我们要讨论的问题:Ja ...

  5. Java中的String为什么是不可变的?

    转载:http://blog.csdn.net/zhangjg_blog/article/details/18319521 什么是不可变对象? 众所周知, 在Java中, String类是不可变的.那 ...

  6. Java基础知识强化101:Java 中的 String对象真的不可变吗 ?

    1. 什么是不可变对象?       众所周知, 在Java中, String类是不可变的.那么到底什么是不可变的对象呢? 可以这样认为:如果一个对象,在它创建完成之后,不能再改变它的状态,那么这个对 ...

  7. 【转】Java中字符串中子串的查找共有四种方法(indexof())

    原文网址:http://wfly2004.blog.163.com/blog/static/1176427201032692927349/ Java中字符串中子串的查找共有四种方法,如下:1.int ...

  8. Java中字符串中子串的查找共有四种方法(indexof())

    Java中字符串中子串的查找共有四种方法(indexof()) Java中字符串中子串的查找共有四种方法,如下:1.int indexOf(String str) :返回第一次出现的指定子字符串在此字 ...

  9. JAVA中字符串函数subString的用法小结

    本篇文章主要是对JAVA中字符串函数subString的用法进行了详细的介绍,需要的朋友可以过来参考下,希望对大家有所帮助 String str; str=str.substring(int begi ...

随机推荐

  1. SVN 记录冲突、忽略

    之前对SVN不熟悉,一碰到冲突就怕得要死,不知道应该怎么处理.今天必须要正视这个问题,研究一下. 一.冲突 SVN非常智能,它不像VSS那样,一个人在改的时候必须以独占的方式签出文件,导致其他人不能够 ...

  2. how to extract and decrypt WeChat EnMicromsg.db on Android phone

    One of my friend came to me with an Android phone. She saild somehting wrong with the hardware of he ...

  3. undefined reference to `pthread_create'问题解决

    在编译pthread有关的程序时,出现undefined reference to `pthread_create'这样的错误. 问题原因: pthread 库不是 Linux 系统默认的库,连接时需 ...

  4. sqlserver分区表实践:对时间分区表自动进行管理

    项目问题:有一张日志表,插入和查询为主,每天记录数据为200多万,大小为2G-4G之间.一开始开发人员使用delete语句手动删除,保留7天数据,经常造成阻塞和性能瓶颈.但是如果不删除数据随着表越来越 ...

  5. Composer -- PHP依赖管理的用法

    1:下载 1.1:方法一: 通过PHP来安装 cd /home/composer curl -sS https://getcomposer.org/installer | php  #这个命令会下载c ...

  6. MyEclipse各种操作

    MyEclipse console没有显示的解决方法  1.进window菜单 ->show view->console    2.还是window菜单->preferences-& ...

  7. POJ C++程序设计 编程题#4 字符串操作

    编程题#4: 字符串操作 来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩.) 注意: 总时间限制: 1000ms 内存限制: 65536kB 描述 给 ...

  8. 软件工程 speedsnail 冲刺5

    2015-5-9 完成任务:学习了黑马android教学视频10\11\12集,填写游戏人的姓名功能为明天的记分板准备: 遇到问题: 问题1 Suspicious method call; shoul ...

  9. SQL如何查询对应的object

    SQL如何查询对应的Object   编写人:CC阿爸 2014-6-17 在日常SQL数据库的操作中,常常需要查询那些对象,如那些sp会包括对该表的操作,那些job会对应执行该sp等相关内容.总之是 ...

  10. 未能加载文件或程序集“Oracle.Web, Version=2.112.1.0, Culture=neutral, PublicKeyToken=89b483f429c47342”或它的某一个依赖项

    当前系统环境描述: Win7x64+VS2012+IIS7 当前情况描述: 发布Web服务,在浏览的时候出现以下问题:未能加载文件或程序集“Oracle.Web, Version=2.112.1.0, ...