Java學習筆記——String類型轉換

來源:互聯網
上載者:User

標籤:package   ack   sel   java學習   object   private   log   version   bytes   

 

一滴水裡觀滄海,一粒沙中看世界

              ——一帶一路歡迎宴致辭

 

上代碼:

  1 package cn.stringtoobj;  2   3 public class TypeConversion {  4   5     public static void main(String[] args) {  6         //將String轉int  7         String str = "123";  8         int[] ints = new int[3];  9         ints[0] = Integer.parseInt(str); 10         ints[1] = Integer.valueOf(str); 11         ints[2] = new Integer(str); 12         print(ints); 13         //String轉byte 14         byte[] bytes = new byte[3]; 15         bytes[0] = Byte.parseByte(str); 16         bytes[1] = Byte.valueOf(str); 17         bytes[2] = new Byte(str); 18         print(bytes); 19         //String轉short 20         short[] shorts = new short[3]; 21         shorts[0] = Short.parseShort(str); 22         shorts[1] = Short.valueOf(str); 23         shorts[2] = new Short(str); 24         print(shorts); 25         //String轉long 26         long[] longs = new long[3]; 27         longs[0] = Long.parseLong(str); 28         longs[1] = Long.valueOf(str); 29         longs[2] = new Long(str); 30         print(longs); 31         //String轉double 32         double[] doubles = new double[3]; 33         doubles[0] = Double.parseDouble(str); 34         doubles[1] = Double.valueOf(str); 35         doubles[2] = new Double(str); 36         print(doubles); 37         //String轉float 38         float[] floats = new float[3]; 39         floats[0] = Float.parseFloat(str); 40         floats[1] = Float.valueOf(str); 41         floats[2] = new Float(str); 42         print(floats); 43         //String轉boolean 44         str = "true"; 45         boolean[] booleans = new boolean[3]; 46         booleans[0] = Boolean.parseBoolean(str); 47         booleans[1] = Boolean.valueOf(str); 48         booleans[2] = new Boolean(str); 49         print(booleans); 50         //String轉byte[] 51         byte[] bytes2 = str.getBytes(); 52         print(bytes2); 53         //String轉char[] 54         char[] dstchars =new char[str.length()]; 55         str.getChars(0, str.length(), dstchars, 0); 56         print(dstchars); 57         //Object轉String 58         Object[] obj = new Object[5]; 59         str = String.valueOf(obj); 60         System.out.println(str); 61         str = String.valueOf(dstchars); 62         System.out.println(str); 63     } 64     private static void print(char[] dstchars) { 65         for (char i : dstchars) { 66             System.out.print(i +" "); 67         } 68         System.out.println(); 69     } 70     private static void print(boolean[] booleans) { 71         for (boolean i : booleans) { 72             System.out.print(i +" "); 73         } 74         System.out.println(); 75     } 76     private static void print(float[] floats) { 77         for (float i : floats) { 78             System.out.print(i +" "); 79         } 80         System.out.println(); 81     } 82     private static void print(double[] doubles) { 83         for (double i : doubles) { 84             System.out.print(i +" "); 85         } 86         System.out.println(); 87     } 88     private static void print(long[] longs) { 89         for (long i : longs) { 90             System.out.print(i +" "); 91         } 92         System.out.println(); 93     } 94     private static void print(short[] shorts) { 95         for (short i : shorts) { 96             System.out.print(i +" "); 97         } 98         System.out.println(); 99     }100     private static void print(int[]ints){101         for (int i : ints) {102             System.out.print(i +" ");103         }104         System.out.println();105     }106     private static void print(byte[]bytes){107         for (byte i : bytes) {108             System.out.print(i +" ");109         }110         System.out.println();111     }112 }

 

Java學習筆記——String類型轉換

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.