淺述int與string類型轉(zhuǎn)換的兩種方法
具體詳情如下所示:
int -> String int i=12345; String s="";
第一種方法:s=i+"";
第二種方法:s=String.valueOf(i);
這兩種方法有什么區(qū)別呢?作用是不是一樣的呢?是不是在任何下都能互換呢?
String -> int s="12345"; int i;
第一種方法:i=Integer.parseInt(s);
第二種方法:i=Integer.valueOf(s).intValue();
這兩種方法有什么區(qū)別呢?作用是不是一樣的呢?是不是在任何下都能互換呢?
以下是答案:
第一種方法:s=i+""; //會產(chǎn)生兩個String對象
第二種方法:s=String.valueOf(i); //直接使用String類的靜態(tài)方法,只產(chǎn)生一個對象
第一種方法:i=Integer.parseInt(s);//直接使用靜態(tài)方法,不會產(chǎn)生多余的對象,但會拋出異常
第二種方法:i=Integer.valueOf(s).intValue();//Integer.valueOf(s) 相當于 new Integer(Integer.parseInt(s)),也會拋異常,但會多產(chǎn)生一個對象
--------------------------------------------------------------------
1如何將字串 String 轉(zhuǎn)換成整數(shù) int?
A. 有兩個方法:
1). int i = Integer.parseInt([String]); 或
i = Integer.parseInt([String],[int radix]);
2). int i = Integer.valueOf(my_str).intValue();
注: 字串轉(zhuǎn)成 Double, Float, Long 的方法大同小異.
2 如何將整數(shù) int 轉(zhuǎn)換成字串 String ?
A. 有叁種方法:
1.) String s = String.valueOf(i);
2.) String s = Integer.toString(i);
3.) String s = "" + i;
注: Double, Float, Long 轉(zhuǎn)成字串的方法大同小異.
JAVA數(shù)據(jù)類型轉(zhuǎn)換
這是一個例子,說的是JAVA中數(shù)據(jù)數(shù)型的轉(zhuǎn)換.供大家學習
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);
}
}
以上內(nèi)容是小編給大家介紹的int與string類型轉(zhuǎn)換的兩種方法,希望能夠幫助到大家。
相關文章
詳解java集成支付寶支付接口(JSP+支付寶20160912)
本篇文章主要介紹了java集成支付寶支付接口,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-12-12
Springboot如何使用filter對request body參數(shù)進行校驗
這篇文章主要介紹了Springboot如何使用filter對request body參數(shù)進行校驗,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03
Java Web項目中實現(xiàn)文件下載功能的實例教程
這篇文章主要介紹了Java Web項目中實現(xiàn)文件下載功能的實例教程,分別講解了通過超鏈接實現(xiàn)下載以及通過Servlet程序?qū)崿F(xiàn)下載的方式,需要的朋友可以參考下2016-05-05
MyBatis-Plus攔截器對敏感數(shù)據(jù)實現(xiàn)加密
做課程項目petstore時遇到需要加密屬性的問題,而MyBatis-Plus為開發(fā)者提供了攔截器的相關接口,本文主要介紹通過MyBatis-Plus的攔截器接口自定義一個攔截器類實現(xiàn)敏感數(shù)據(jù)如用戶密碼的加密功能,感興趣的可以了解一下2021-11-11
淺析Java中String與StringBuffer拼接的區(qū)別
String拼接會創(chuàng)建一個新的String對象,存儲拼接后的字符串,StringBuffer拼接是直接在本身拼接,會即時刷新。下面通過本文給大家介紹Java中String與StringBuffer拼接的區(qū)別,感興趣的朋友一起看看吧2017-06-06
Java Chassis3過載狀態(tài)下的快速失敗解決分析
本文解密了Java Chassis 3快速失敗相關的機制和背后故事,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2024-01-01
springBoot基于webSocket實現(xiàn)掃碼登錄
最近做了個新項目,涉及到掃碼登錄。之前項目使用的是 ajax輪詢的方式。感覺太low了。所以這次用webSocket的方式進行實現(xiàn),感興趣的可以了解一下2021-06-06

