JAVA将string转化为int(int怎么转string)

1如何将字串String转换成整数int?A.有两个方法:1).inti=Integer.parseInt([String]);或i=Integer.parseInt([String],[intradix]);2).inti=Integer.valueOf(my_str).intValue();注:字串转成Double,Float,Lo

大家好,又见面了,我是你们的朋友全栈君。

 

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中找到,整理一下做个备份。

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/128587.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Linux进程间通信

    Linux进程间通信

    2021年8月10日
    57
  • synchronousqueue场景_SynchronousQueue原理解析

    synchronousqueue场景_SynchronousQueue原理解析经典的生产者-消费者模式,操作流程是这样的:有多个生产者,可以并发生产产品,把产品置入队列中,如果队列满了,生产者就会阻塞;有多个消费者,并发从队列中获取产品,如果队列空了,消费者就会阻塞;image.pngSynchronousQueue也是一个队列来的,但它的特别之处在于它内部没有容器,一个生产线程,当它生产产品(即put的时候),如果当前没有人想要消费产品(即当前没有线程执行take),此…

    2022年6月22日
    27
  • 前端基础知识1

    前端基础知识1’usestrict’varname=’Jay’varperson={name:’Wang’,pro:{name:’Michael’,getName:function(){returnthis.name;}}}console.log(person.pro.getName());varpepole=person.pro.getName;console

    2022年5月13日
    90
  • python怎么安装matplotlib.pyplot_python安装matplotlib模块

    python怎么安装matplotlib.pyplot_python安装matplotlib模块总结经验,前排感谢CSDN大神…一、在Pycharm中安装matplotlib1、打开AnacondaPrompt,输入pipinstallmatplotlib输入pipinstallmatplotlib==3.3.0限制下载的版本为3.3.0.这是为了防止版本过新,之后在PyCharm运行时出现问题。2、打开PyCharm(1)依次点击File-Settings-…

    2022年8月25日
    10
  • hashmap线程不安全问题_为什么HashMap线程不安全

    hashmap线程不安全问题_为什么HashMap线程不安全HashMap的线程不安全主要体现在下面两个方面:1.在JDK1.7中,当并发执行扩容操作时会造成环形链和数据丢失的情况。2.在JDK1.8中,在并发执行put操作时会发生数据覆盖的情况。JDK1.7在JDK1.7中,扩容数据时要进行把原数据迁移到新的位置,使用的方法://数据迁移的方法,头插法添加元素voidtransfer(Entry[]newTable,booleanrehash){intnewCapacity=newTable.length;     

    2022年10月11日
    6
  • java语言和C语言的区别

    java语言和C语言的区别简单的说就是两种不同的语言.但是它们之间既有联系又有区别

    2022年7月8日
    23

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号