Java中的valueOf和parse...()

Java中的valueOf和parse...
String的valueOf()
Double的转换
Double.valueOf()
Double.parseDouble()
区别
Integer的转换
Integer.valueOf()
Integer.parseInt()
valueOf()和parse...()的区别
String的valueOf()
将任何对象转换成字符串类型。

总共有9个重载方法,

对于参数为char,都是通过new String()返回
对于参数为boolean,将返回对应的字符串:true或者false
对于后4个方法,都是调用该对象类型的toString()返回

下面说一下参数为Object的方法

    public static String valueOf(Object obj) {
        return (obj == null) ? "null" : obj.toString();
    }

    public static void main(String[] args) {
        Objects obj = null;
        String s1 = String.valueOf(obj);
        System.out.println("s1:" + s1);

        String s2 = String.valueOf(null);
        System.out.println("s2:" + s2);
    }
1
2
3
4
5
6
7
8
9
10
11
12
执行main方法后,结果为s1:null 和NPE
可见

valueOf()的参数如果是一个Object的引用,而这个引用的指向为null,将返回一个字符串null
valueOf()的参数如果是null,而不是一个空的引用,将会报NPE
Double的转换
Double.valueOf()
    public static Double valueOf(String s) throws NumberFormatException {
        return new Double(parseDouble(s));
    }
 
    public static Double valueOf(double d) {
        return new Double(d);
    }
1
2
3
4
5
6
7
入参为字符串类型的数字(不能是空,也不能是null)或者double基本类型
出参为对象类型Double(不可能出现null)
如果参数为字符串,会调用parseDouble()方法

Double.parseDouble()
    public static double parseDouble(String s) throws NumberFormatException {
        return FloatingDecimal.parseDouble(s);
    }
1
2
3
入参为字符串类型的数字(不能是空,也不能是null)
出参为基本类型double(不可能出现null)

区别
从参数上说
Double.valueOf:参数可以是 double,也可以是 String。
Double.parseDouble:参数只能是 String。

从结果上说
Double.valueOf:返回 Double。
Double.parseDouble:返回 double。

区别

Integer的转换
Integer.valueOf()
    public static Integer valueOf(String s) throws NumberFormatException {
        return Integer.valueOf(parseInt(s, 10));
    }

    public static Integer valueOf(String s, int radix) throws NumberFormatException {
        return Integer.valueOf(parseInt(s,radix));
    }
    
    public static Integer valueOf(int i) {
        if (i >= IntegerCache.low && i <= IntegerCache.high)
            return IntegerCache.cache[i + (-IntegerCache.low)];
        return new Integer(i);
    }
1
2
3
4
5
6
7
8
9
10
11
12
13
valueOf()调用的还是parseInt()方法,返回值为Integer对象,

特点是,IntegerCache类会缓存-128到127之间的Integer对象,当值在这个范围内时,将直接返回,而不是需要new Integer对象。

Integer.parseInt()
    public static int parseInt(String s) throws NumberFormatException {
        return parseInt(s,10);
    }

    public static int parseInt(String s, int radix) throws NumberFormatException
1
2
3
4
5
parseInt方法有两个重载,radix指s的进制类型,范围是2-36,默认为10进制
返回为基本类型int,且为10进制。

10进制数123转换为10进制的过程:

123= 1 * 10 * 10 + 2 * 10 + 3

16进制数123转换为10进制的过程:

291= 1 * 16 * 16 + 2 * 16 + 3

valueOf()和parse…()的区别
Java中的基本类型中,

parse…()返回值都为基本类型
valueOf返回值都为对应的对象类型,且valueOf会调用parse…()
————————————————
版权声明:本文为CSDN博主「一代小雄」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/xiong9999/article/details/87800317

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
This error occurs when trying to parse a JSON string that contains a value for a LocalDateTime object, but the parsing library is unable to convert it into a LocalDateTime object. To resolve this error, you can try using a JSON parsing library that supports LocalDateTime objects, such as Jackson or Gson. Alternatively, you can modify the JSON string to use a format that can be easily converted to a LocalDateTime object, such as ISO-8601 format. Here's an example of parsing a JSON string containing a LocalDateTime object using Jackson: ``` ObjectMapper mapper = new ObjectMapper(); mapper.registerModule(new JavaTimeModule()); // register module to support LocalDateTime objects String json = "{\"dateTime\":\"2021-10-25T10:15:30\"}"; MyClass myObj = mapper.readValue(json, MyClass.class); ``` In this example, the `JavaTimeModule` is registered to support LocalDateTime objects, and the JSON string is parsed into a `MyClass` object. If the JSON string cannot be modified, you may need to manually parse the string and convert it to a LocalDateTime object using a DateTimeFormatter. Here's an example: ``` String json = "{\"dateTime\":\"2021-10-25T10:15:30\"}"; JsonObject jsonObject = JsonParser.parseString(json).getAsJsonObject(); String dateTimeString = jsonObject.get("dateTime").getAsString(); DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME; LocalDateTime dateTime = LocalDateTime.parse(dateTimeString, formatter); ``` In this example, the JSON string is manually parsed using `JsonParser`, the `dateTime` value is retrieved from the `JsonObject`, and a `DateTimeFormatter` is used to convert the string into a `LocalDateTime` object.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值