• 字符串、String等转换


    (1) String 转换为字符串

       例:
    String s = "abcde";
    char[] a = s.toCharArray();



    (2) 字符串转换为String
    char[] a;
    a = {a,b,c,d};
    String s;
    s = new String(a);



    (3) int类型转换为String
       例:
    int n = 0;
    String s = String.valueOf(n)



    (4)int -> String


    int i=12345;
    String s="";
    第一种方法:s=i+"";
    第二种方法:s=String.valueOf(i);
    这两种方法有什么区别呢?作用是不是一样的呢?是不是在任何下都能互换呢?



    (5)String -> int


    s="12345";
    int i;
    第一种方法:i=Integer.parseInt(s);
    第二种方法:i=Integer.valueOf(s).intValue();

    这两种方法有什么区别呢?作用是不是一样的呢?是不是在任何下都能互换呢?
    以下是答案:


    第一种方法:s=i+"";   //会产生两个String对象
    第二种方法:s=String.valueOf(i); //直接使用String类的静态方法,只产生一个对象


    第一种方法:i=Integer.parseInt(s);//直接使用静态方法,不会产生多余的对象,但会抛出异常
    第二种方法:i=Integer.valueOf(s).intValue();//Integer.valueOf(s) 相当于 new Integer(Integer.parseInt(s)),也会抛异常,但会多产生一个对象
     


     
        JAVA数据类型转换 


            这是一个例子,说的是JAVA中数据数型的转换.供大家学习

     

     

    1. public class TypeChange {  
    2.            public TypeChange() {  
    3.            }  
    4.            //change the string type to the int type  
    5.            public static   int stringToInt(String intstr)  
    6.            {  
    7.              Integer integer;  
    8.              integer = Integer.valueOf(intstr);  
    9.              return integer.intValue();  
    10.            }  
    11.            //change int type to the string type  
    12.            public static String intToString(int value)  
    13.            {  
    14.              Integer integer = new Integer(value);  
    15.              return integer.toString();  
    16.            }  
    17.            //change the string type to the float type  
    18.            public static   float stringToFloat(String floatstr)  
    19.            {  
    20.              Float floatee;  
    21.              floatee = Float.valueOf(floatstr);  
    22.              return floatee.floatValue();  
    23.            }  
    24.            //change the float type to the string type  
    25.            public static String floatToString(float value)  
    26.            {  
    27.              Float floatee = new Float(value);  
    28.              return floatee.toString();  
    29.            }  
    30.            //change the string type to the sqlDate type  
    31.            public static java.sql.Date stringToDate(String dateStr)  
    32.            {  
    33.              return   java.sql.Date.valueOf(dateStr);  
    34.            }  
    35.            //change the sqlDate type to the string type  
    36.            public static String dateToString(java.sql.Date datee)  
    37.            {  
    38.              return datee.toString();  
    39.            }  
    40.   
    41.            public static void main(String[] args)  
    42.            {  
    43.              java.sql.Date day ;  
    44.              day = TypeChange.stringToDate("2003-11-3");  
    45.              String strday = TypeChange.dateToString(day);  
    46.              System.out.println(strday);  
    47.            }  
    48.   
    49.         }  
  • 相关阅读:
    深入正则表达式(0):正则表达式概述
    讲透学烂二叉树(二):图中树的定义&各类型树的特征分析
    讲透学烂二叉树(一):图的概念和定义—各种属性特征浅析
    Gzip之后继者Brotli浅析之CDN厂商的智能压缩,服务器Brotli设置
    ECMAScript进化史(1):​话说Web脚本语言王者JavaScript的加冕历史
    nginx网站限速限流配置——网站被频繁攻击,nginx上的设置limit_req和limit_conn
    linux添加用户,修改用户密码,修改用户权限,设置root用户操作
    nginx 限制ip访问,禁止非法域名指向本机ip——防止被别人绑定域名到自己IP的方法
    centos8 新增ssh自定义端口与屏蔽默认22端口。
    1g云主机升级centos8不满足centos 8 至少2g内存要求,linux虚拟内存来凑
  • 原文地址:https://www.cnblogs.com/Cherry-B/p/3338171.html
Copyright © 2020-2023  润新知