Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the differences between Integer.valueOf,parsetInt () String.valueOf in Java

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Editor to share with you what are the differences between Integer.valueOf,parsetInt () String.valueOf in Java. I hope you will get something after reading this article. Let's discuss it together.

Let's take a look at a piece of code.

Public class IntegerDemo {public static void main (String [] args) {String num = null; System.out.println (Integer.parseInt (num)); / / Exception java.lang.NumberFormatException System.out.println (Integer.valueOf (num)); / / Exception java.lang.NumberFormatException System.out.println (String.valueOf (num)); / / output null num = ""; System.out.println (Integer.parseInt (num)) / / Exception java.lang.NumberFormatException System.out.println (Integer.valueOf (num)); / / Exception java.lang.NumberFormatException System.out.println (String.valueOf (num)); / / empty string, nothing output}}

Let's take a look at what is written in String.valueOf ().

String.valueOf () can output normally when it encounters null and empty string, so it doesn't throw an error.

Let's take a look at how it is handled in the packaging type Integer.

Both of these methods need to parseInt (SJ10), which first converts the string s to the decimal int basic type, but valueOf () fetches it from the internal cache of [- 127127] according to the int range (using the shared meta pattern in the design pattern)

Let's take a look at parseInt (s, 10). In the method, it will determine whether the string is a legal number, and will check null, empty string and other formats, so it will throw an error.

Public static int parseInt (String s, int radix) throws NumberFormatException {/ * * WARNING: This method may be invoked early during VM initialization * before IntegerCache is initialized. Care must be taken to not use * the valueOf method. * / if (s = = null) {throw new NumberFormatException ("null");} if (radix

< Character.MIN_RADIX) { throw new NumberFormatException("radix " + radix + " less than Character.MIN_RADIX"); } if (radix >

Character.MAX_RADIX) {throw new NumberFormatException ("radix" + radix + "greater than Character.MAX_RADIX");} int result = 0; boolean negative = false; int i = 0, len = s.length (); int limit =-Integer.MAX_VALUE; int multmin; int digit; if (len > 0) {char firstChar = s.charAt (0) If (firstChar <'0') {/ / Possible leading "+" or "-" if (firstChar = ='-') {negative = true; limit = Integer.MIN_VALUE;} else if (firstChar! ='+') throw NumberFormatException.forInputString (s); if (len = 1) / / Cannot have lone "+" or "-" throw NumberFormatException.forInputString (s) } multmin = limit / radix; while (I < len) {/ / Accumulating negatively avoids surprises near MAX_VALUE digit = Character.digit (s.charAt (iTunes +), radix); if (digit < 0) {throw NumberFormatException.forInputString (s);} if (result < multmin) {throw NumberFormatException.forInputString (s);} result * = radix If (result < limit + digit) {throw NumberFormatException.forInputString (s);} result-= digit;}} else {throw NumberFormatException.forInputString (s);} return negative? Result:-result;} after reading this article, I believe you have a certain understanding of "what are the differences between Integer.valueOf,parsetInt () String.valueOf in Java". If you want to know more about it, please follow the industry information channel. Thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report