转自licoolxue

https://blog.csdn.net/licoolxue/article/details/1533364

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数据类型转换     ynniebo [收藏] 
关键字   类型转换
出处   
 
 这是一个例子,说的是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 中int String类型转换的更多相关文章

  1. JAVA中int、String的类型转换

    int -> String int i=12345;String s="";第一种方法:s=i+"";第二种方法:s=String.valueOf(i); ...

  2. Java中int与String间的类型转换

    int -> String int i=12345;String s=""; 除了直接调用i.toString();还有以下两种方法第一种方法:s=i+"" ...

  3. java中字符串String 转 int(转)

    java中字符串String 转 int String -> int s="12345"; int i; 第一种方法:i=Integer.parseInt(s); 第二种方法 ...

  4. java中int转成String位数不足前面补零

    java中int转成String位数不足前面补零 转载自:http://ych0108.iteye.com/blog/2174134 java中int转String位数不够前面补零 String.fo ...

  5. 关于Java中的基本数据类型转换

    Java中的基本类型有四种,其中整型分为byte.short.int.long,浮点型分为float.double,字符型char,布尔型boolean.8种类型的级别由低到高byte->sho ...

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

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

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

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

  8. java中int,float,long,double取值范围,内存泄露

    java中int,float,long,double取值范围是多少? 写道 public class TestOutOfBound { public static void main(String[] ...

  9. 计算机基础--Java中int char byte的关系

    计算机基础--Java中int char byte的关系 重要:一个汉字占用2byte,Java中用char(0-65535 Unicode16)型字符来存字(直接打印输出的话是字而非数字),当然要用 ...

随机推荐

  1. python 元组tuple - python基础入门(14)

    在上一篇文章中我们讲解了关于python列表List的相关内容,今天给大家解释一下列表List的兄弟 – 元组,俗称: tuple. 元组tuple和列表List类似,元组有如下特点: 1.由一个或者 ...

  2. The request was aborted: Could not create SSL/TLS secure channel

    一.背景: 公司底层服务CDN从Akamai迁移到阿里云之后, 使用该服务的一个应用报错如下: System.AggregateException: One or more errors occurr ...

  3. typescript无法识别vue中的$refs

    例如:vue-fullscreen <template> <div class="Test"> <fullscreen ref="fulls ...

  4. K8S从入门到放弃系列-(8)kube-apiserver 高可用配置

    摘要: 前面几篇文章,就是整个的master节点各组件的部署,上面我们提到过,k8s组件中,kube-controller-manager.kube-scheduler及etcd这三个服务高可用,都是 ...

  5. win10系统,jdk环境变量配置,编辑系统变量窗口显示旧版单行和新版列表问题

    大家好,今天说一下我在配置jdk环境变量时遇到的编辑系统变量窗口显示问题. 首先我们说一下如何配置jdk环境变量. 右击此电脑,点击属性. 跳出如下窗口,点击高级系统设置. 跳出如下窗口,点击环境变量 ...

  6. Markdown 空格详细介绍

      它叫不换行空格,全称No-Break Space,它是最常见和我们使用最多的空格,大多数的人可能只接触了 ,它是按下space键产生的空格.在HTML中,如果你用空格键产生此空格,空格是不会累加的 ...

  7. golang之数组与切片

    数组 数组可以存放多个同一类型数据,数组也是一种数据类型,在Go中,数组是值类型. 数组的定义: var 数组名 [数组大小]数据类型 var a [5]int 赋初值   a[0] = 1    a ...

  8. axios全局配置及拦截器

    axios使用说明文档 axios 全局配置: //axios-init.js import axios from 'axios': let loadingInstance; //创建Loading ...

  9. 原生js上传图片遇到的坑(axios封装)

    后台给我写了一个上传图片的接口,自己用form表单测试成功 接口可以正常跳转 测试的代码: <!doctype html> <html lang="en"> ...

  10. JavaScript_day01

    1.变量声明 怎么声明变量? JavaScript中变量声明用的关键字是 var 变量名称. 变量名称命名有什么限制? 变量名称命名需规范,准则:不能以数字开头,不能含有特殊字符(css的属性),可以 ...