Java中string與int怎么實現(xiàn)互相轉(zhuǎn)換

本篇文章為大家展示了Java中string與int怎么實現(xiàn)互相轉(zhuǎn)換,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。

成都創(chuàng)新互聯(lián)服務(wù)項目包括建鄴網(wǎng)站建設(shè)、建鄴網(wǎng)站制作、建鄴網(wǎng)頁制作以及建鄴網(wǎng)絡(luò)營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢、行業(yè)經(jīng)驗、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,建鄴網(wǎng)站推廣取得了明顯的社會效益與經(jīng)濟效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到建鄴省份的部分城市,未來相信會繼續(xù)擴大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!

int -> String

int i=12345;
String s="";

核心:s=i+"";

String -> int

s="12345";
int i;

核心:i=Integer.parseInt(s);

package com.fry.util;
public class Transfer {
  public void stringToInt(){
    String id="20171091";
    int n=Integer.parseInt(id);
    System.out.println(n);
  }
}

這個方法的測試結(jié)果就是20171091。

以下參考:

java中string和int互相轉(zhuǎ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)成字串的方法大同小異.

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)換

關(guān)鍵字   類型轉(zhuǎn)換

這是一個例子,說的是JAVA中數(shù)據(jù)數(shù)型的轉(zhuǎn)換.供大家學(xué)習(xí)引

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);
}
}

上述內(nèi)容就是Java中string與int怎么實現(xiàn)互相轉(zhuǎn)換,你們學(xué)到知識或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識儲備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

當前題目:Java中string與int怎么實現(xiàn)互相轉(zhuǎn)換
本文鏈接:http://muchs.cn/article8/pphiip.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供響應(yīng)式網(wǎng)站動態(tài)網(wǎng)站、用戶體驗、外貿(mào)網(wǎng)站建設(shè)網(wǎng)頁設(shè)計公司、虛擬主機

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站建設(shè)