• Post Reply Bookmark Topic Watch Topic
  • New Topic

intValue Vs ParseInt  RSS feed

 
Mahesh Pinnamaneni
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear All,
can anyone tell me when can i use Interger.parseInt() and Interger.intValue. becoz both of them return the same.
Also pls tell me when i have to use Interger.parseInt() and Interger.intValue.

thanks alot
regards,
Mahesh
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mahesh

Look at the JDK source code for Integer.java and you will know your answers...

Regards
Maulin
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Integer.parseInt() is a static method, that you can use to parse a string into an int without the need to create an instance of class Integer.

Integer.intValue() is a non-static method and is meant to get the int value of an Integer object.
 
Mahesh Pinnamaneni
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for ur reply.
Is there any other diff other than this.
Ex:-
1) int j = Integer.parseInt("0");

2) Integer io = new Integer("0");
int i = io.intValue();

In the above two examples the out pput will be same right?

my doubt is as we can achive convertion from the static parseInt why the SUN has introduced this intValue(). where exactly can we use this.
pls help me.

Thanks & regards,
Mahesh P
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mahesh Pinnamaneni:
my doubt is as we can achive convertion from the static parseInt why the SUN has introduced this intValue(). where exactly can we use this.
pls help me.


What if you don't have a String object? What if the object that was given to you is a Integer object? How would you get the int value back?

On a side note, the intValue() method is part of the Number interface, this means that you can call it on a Integer, Float, Double, Long, objects and get an int value back.

Henry
 
Julien Grenier
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's say you want a List of int, the only way to achieve that is to convert those int into Integer since List don't take primitive type.
so now you have a List of Integer then you want to sum those Integers. tadam you need the Integer.intValue(). that's one of the many reason you might need intValue()
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Integer(String) constructor internally calls parseInt, by the way.
 
amitaryan kumar
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
intValue() method is by number class. which is used for getting the int value from a Integer class object. as like this example where we can get the intValue() of character 'A'.

public class Test1
{
Integer i = new Integer('B');
int n = i.intValue();
void don()
{
System.out.println(n);
}
public static void main(String[] args)
{
Test1 t = new Test1();
t.don();
}
}


This will return the int value of 'B' that is in windows -- 66.

You can try this in different places.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch, amitaryan.

There is a much simpler way to convert a char such as 'B' to an int. Like this:

That does exactly the same as these two lines in your code, but without creating an unnecessary intermediate Integer object:
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!