1 package com.jdk7.chapter5; 2 3 public class BasicToString { 4 /** 5 * 基本数据类型转换为String 6 * 方法一:String.valueOf(基本数据类型) 7 * 方法二:基本数据类型的封装类.valueOf(基本数据类型) 8 * 方法三:直接在输出流中打印 9 */ 10 public static void intToStr(int n){ 11 System.out.println("String.valueOf("+n+")>"+String.valueOf(n)); 12 System.out.println("Integer.valueOf("+n+").toString()>"+Integer.valueOf(n).toString()); 13 System.out.println("空格+"+n+">"+n); 14 } 15 16 /** 17 * String转换为基本数据类型的封装类 18 * 方法一:基本数据类型的封装类.parseXxx(String) 19 * 方法二:new 基本数据类型的封装类(String) 20 * @param str 21 */ 22 public static void strToInt(String str){ 23 System.out.println("Integer.parseInt("+str+")>"+Integer.parseInt(str)); 24 System.out.println("(new Integer("+str+")).toString()>"+(new Integer(str)).toString()); 25 } 26 27 public static void doubleToStr(double dou){ 28 System.out.println("String.valueOf("+dou+")>"+String.valueOf(dou)); 29 System.out.println("Double.valueOf("+dou+").toString()>"+Double.valueOf(dou).toString()); 30 System.out.println("空格+"+dou+">"+dou); 31 } 32 33 public static void strToDouble(String src){ 34 System.out.println("Double.parseDouble("+src+")>"+Double.parseDouble(src)); 35 System.out.println("new Double("+src+")>"+new Double(src)); 36 } 37 public static void main(String[] args) { 38 BasicToString.intToStr(10); 39 BasicToString.strToInt("35536"); 40 BasicToString.doubleToStr(4234.354); 41 BasicToString.strToDouble("7994.46378"); 42 } 43 } 44 45 46 执行结果: 47 String.valueOf(10)>10 48 Integer.valueOf(10).toString()>10 49 空格+10>10 50 Integer.parseInt(35536)>35536 51 (new Integer(35536)).toString()>35536 52 String.valueOf(4234.354)>4234.354 53 Double.valueOf(4234.354).toString()>4234.354 54 空格+4234.354>4234.354 55 Double.parseDouble(7994.46378)>7994.46378 56 new Double(7994.46378)>7994.46378
原文地址:https://www.cnblogs.com/celine/p/8460317.html
时间: 2024-10-12 16:11:47