• 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 }
  • 相关阅读:
    js eval函数写一个简单的计算器
    nginx方向代理
    nodejs环境的搭建(linux环境centos6.5)
    vue渲染数据后与owlCarousel轮播插件冲突,失效
    vue 配合vue-resource调用接口,获取数据
    高效生成随机数并去重
    nginx 调整配置文件支持TP框架
    noVNC 搭建
    python基础学习-socket1 初识socket
    python基础学习-面向对象-类的使用
  • 原文地址:https://www.cnblogs.com/tomasman/p/6853528.html
Copyright © 2020-2023  润新知