Java学习笔记——String类型转换

Posted Tomas曼

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java学习笔记——String类型转换相关的知识,希望对你有一定的参考价值。

 

一滴水里观沧海,一粒沙中看世界

              ——一带一路欢迎宴致辞

 

上代码:

  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类型转换的主要内容,如果未能解决你的问题,请参考以下文章

Java:Effective java学习笔记之 请不要在新代码中使用原生态类型

java学习笔记—— 常见面试题

java学习笔记—— 常见面试题

java学习笔记—— 数据类型数据类型转换

[原创]java WEB学习笔记67:Struts2 学习之路-- 类型转换概述, 类型转换错误修改,如何自定义类型转换器

无法将 java.lang.String 类型的属性值转换为所需的 java.time.LocalDateTime 类型