The Artima Developer Community
Sponsored Link

Java Buzz Forum
How to convert Java String to int or Integer - valueOf vs parseInt

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Javin Paul

Posts: 1090
Nickname: javinpaul
Registered: Jan, 2012

Javin Paul is Java Programmer working on Finance domain.
How to convert Java String to int or Integer - valueOf vs parseInt Posted: Mar 8, 2013 7:28 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Javin Paul.
Original Post: How to convert Java String to int or Integer - valueOf vs parseInt
Feed Title: Java67
Feed URL: http://www.java67.com/feeds/posts/default?alt=rss
Feed Description: Java and technology tutorials, tips, questions for all programmers.
Latest Java Buzz Posts
Latest Java Buzz Posts by Javin Paul
Latest Posts From Java67

Advertisement
There are 3 main ways to convert String to int in Java, using constructor of Integer class, parseInt() method of java.lang.Integer and Integer.valueOf() method. Though all those method returns instance of java.lang.Integer, which is a wrapper class for primitive int value, it's easy to convert Integer to int in Java. From Java 5, you don't need to do anything, autoboxing will automatically convert Integer to int. For Java 1.4 or lower version, you can use intValue() method form java.lang.Integer class, to convert Integer to int. As name suggest, parseInt() is the core method to convert String to int in Java. parseInt() accept a String which must contain decimal digits and first character can be an ASCII minus sign (-) to denote negative integers. parseInt() throws NumberFormatException, if provided String is not convertible to int value. By the way parseInt is an overloaded method and it's overloaded version takes radix or base e.g. 2,8,10 or 16, which can be used to convert binary, octal, hexadecimal String to int in Java. Integer.valueOf() is another useful method to convert String to Integer in Java, it offers caching of Integers from -128 to 127. Internally, valueOf() also calls parseInt() method for String to int conversion. In this Java programming tutorial, we will see all three ways to convert String to int value in Java.
Read more ยป

Read: How to convert Java String to int or Integer - valueOf vs parseInt

Topic: How to Iterate over Java Enum using values and for loop Previous Topic   Next Topic Topic: How to Write Automated Tests for Plugins

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use