You are on page 1of 3

7/15/13

Difference between valueOf and parseInt method in Java

Javarevisited
Blog about Java Program Tutorial Example How to, Unix Linux commands, Interview Questions, FIX Protocol, Tibco RV tutorials, Equities trading system, MySQL

Java 6
T H UR S DA Y , A PR IL 25, 2013

Java Help

Java Test Tool

Java Java
Searc h

Difference between valueOf and parseInt method in Java


Recent Posts
Both v a l u e O fand p a r s e I n tmethods are used to convert String to Integer in Java, but there are subtle difference between them. If you look at code of v a l u e O f ( ) method, you will find that internally it calls p a r s e I n t ( )method to convert String to Integer, but it also maintains a pool of Integers from -128 to 127 and if requested integer is in pool, it returns object from pool. Which means two integer objects returned using v a l u e O f ( )method can be same by equality operator. This caching of Immutable object, does help in reducing garbage and help garbage collector. Another difference between p a r s e I n t ( )and v a l u e O f ( )method is there return type. v a l u e O f ( )of j a v a . l a n g . I n t e g e rreturns an I n t e g e robject, while p a r s e I n t ( )method returns an i n tprimitive. Though, after introducing Autoboxing in Java 1.5, this doesn't count as a difference, but it's worth knowing.

When to make a method static in Java Top 5 JQuery books for Beginners and Web developers - Best of lot How SSL, HTTPS and Certificates works in Java web applications Role based Access control using Spring Security and MVC, Mapping LDAP Groups to Authorities for Authorization 10 Java Exception and Error Interview Questions Answers 5 must read books to learn Object Oriented and Java Design patterns - Best of lot

ParseInt vs valueOf in Java


If you look code of p a r s e I n t ( )and v a l u e O f ( )method from j a v a . l a n g . I n t e g e rclass, you will find that actual job of converting String to integer is done by p a r s e I n t ( )method, v a l u e O f ( )just provide caching of frequently used Integer objects, Here is code snippet from v a l u e O f ( )method which makes things clear:
p u b l i cs t a t i cI n t e g e rv a l u e O f ( S t r i n gs )t h r o w sN u m b e r F o r m a t E x c e p t i o n{ r e t u r nI n t e g e r . v a l u e O f ( p a r s e I n t ( s ,1 0 ) ) ; }

This method first calls p a r s e I n t ( )method, in order to convert String to primitive int, and then creates Integer object from that value. You can see it internally maintains an Integer cache. If primitive int is within range of cache, it returns Integer object from pool, otherwise it create a new object.
p u b l i cs t a t i cI n t e g e rv a l u e O f ( i n ti ){ i f ( i> =1 2 8& &i< =I n t e g e r C a c h e . h i g h ) r e t u r nI n t e g e r C a c h e . c a c h e [ i+1 2 8 ] ; e l s e r e t u r nn e wI n t e g e r ( i ) ; }

There is always confusion, whether to use p a r s e I n t ( )or v a l u e O f ( )for converting String to primitive int and j a v a . l a n g . I n t e g e r , I would suggest use p a r s e I n t ( )if you need primitive i n tand use v a l u e O f ( )if you need java.lang.Integer objects. Since immutable objects are safe to be pooled and reusing them only reduces load on garbage collector, it's better to use v a l u e O f ( )if you need Integer object. Related Java Programming Tutorials from Javarevisited Blog How to Convert String to Double in Java Top 10 Puzzles, Riddles, Lateral thinking Questions asked in Programming Interview How to check if a number is even or odd without using modulus operator in Java How to print byte array as Hex String in Java 10 Abstract Class and Interface Interview Questions for Java Programmers

Follow Us
Follow @javinpaul 3,239 follow ers

Javarevisited on
Please share with your friends if like this article

Follow

+3,257

Programming Software
You might like:

Riddle

Job interview questions

Abstract

Programm

Why wait, notify and notifyAll is defined in Object Class and not on Thread class in Java What is Abstraction in java - Java abstraction tutorial and example How to Sort Java ArrayList in Descending Order - Example Tutorial Why Enum Singleton are better in Java

javarevisited.blogspot.in/2013/04/difference-between-valueof-and-parseint-method-java.html

1/3

7/15/13

Difference between valueOf and parseInt method in Java


Recommended by

Posted by Javin Paul at 6:30 AM Labels: coding, core java, Java Programming Tutorials

+11 Recommend this on Google

Environmentally Friendly Programming Software

Primitive Riddle

Langs

Java programming tutorials Abstract

Share With Programm

Job interview questions

6 comments:
Charanpreet Singh said... Thanks for sharing April 25, 2013 at 6:57 AM Daniel said... Hi, I don't understand why pool of Integers from -128 to 127 instead of others number ? April 25, 2013 at 8:06 PM Anonymous said... All these are basic api's and people usually ignore the internals. But, its too good to know the implementations. Thank you. PS: i think there is a typo "parseInt() method to convert Integer to String"- it should be "to convert String to int" April 25, 2013 at 10:24 PM Javin @ Java Classloder Working said... @Anonymous, You are right, good understanding of basic API e.g. java.lang, java.util are extremely important for effective programming in Java. By the way good catch, parseInt() is used for converting String to primitive int :) April 26, 2013 at 7:21 AM Javin @ Must Override super class method Error said... @Daniel, I don't know the exact reason, but two observation make sense : 1) Those are most frequently used numbers, so it make sense to cache them. 2) They are also range of bytes -128 to 127 Cheers April 26, 2013 at 7:23 AM SARAL SAXENA said... @Javin gr8 article , it has clearly explain the concepts ...Actually, valueOf uses parseInt internally. The difference is parseInt returns an int primitive while valueOf returns an Integer object. Consider from the Integer.class source: public static int parseInt(String s) throws NumberFormatException { return parseInt(s,10); } public static Integer valueOf(String s, int radix) throws NumberFormatException { return Integer.valueOf(parseInt(s,radix)); } public static Integer valueOf(String s) throws NumberFormatException { return Integer.valueOf(parseInt(s, 10)); } As for parsing with a comma, I'm not familiar with one. I would sanitize them. int million = Integer.parseInt("1,000,000".replace(",","")); If efficiency is your concern, use int: it is much faster than Integer. Otherwise, class Integer offers you at least a couple clear, clean ways: Integer myInteger = new Integer(someString); Integer anotherInteger = Integer.valueOf(someOtherString); By Javin Paul Subscribe To This Blog Free Posts Comments Followers Join this site
w ith Google Friend Connect
F acebook social plugin

Javarevisited
Like 4,660 people like Javarevisited.

Recommendd Books Java Programming and Design Pattern Spring Framework Subscribe by email: Subscribe

Members (1250) More

Already a member? Sign in

Learn Java Programming To Java Java for Programmers


Blog Archive 2013 (97) July (4) June (9) May (14) April (18) How to Find Runtime of a Process in UNIX and Linux... Eclipse No Java Virtual Machine was found Windows ... What is java.library.path , How to set in Eclipse ... Difference between valueOf and parseInt

javarevisited.blogspot.in/2013/04/difference-between-valueof-and-parseint-method-java.html

2/3

7/15/13

Difference between valueOf and parseInt method in Java


method in ... April 28, 2013 at 1:44 AM 10 Reasons to Learn Java Programming Language and ... What is String args[] in Java main method How to Convert InputStream to Byte Array in Java -... 10 Abstract Class and Interface Interview Question... How to Convert JSON array to String array in Java ... Top 10 Puzzles, Riddles, Logical and Lateral Think...

Post a Comment
E n t e ry o u rc o m m e n t . . .

Comment as: Google Account Publish Preview

How to Check If Number is Even or Odd without usin... How to Compare Two Enum in Java - Equals vs == vs ... How to get current stack trace in Java for a Threa... Spring Framework Tutorial - How to call Stored Pro... How to increment decrement date by days in Java - ... Difference between trunk, tags and branches in SVN... What is maximum Java Heap Size for 32 bit and 64-b...

Newer Post Subscribe to: Post Comments (Atom)

Home

Older Post

JUnit 4 Tutorial - Test Exception thrown by Java M... March (16) February (18) January (18) 2012 (218) 2011 (145) 2010 (33)

References Java API documentation JDK 6 Spring framework doc Struts ANT Maven JDK 7 API MySQL Linux Eclipse Copyright by Javin Paul 2012. Powered by Blogger.

About Me

Privacy Policy

javarevisited.blogspot.in/2013/04/difference-between-valueof-and-parseint-method-java.html

3/3

You might also like