intValue() is an inbuilt method in java that returns the value of the specified number as an int. This may involve rounding or truncation. Parameters: This method does not accepts any parameter. Return value: This method returns the numeric value represented by this object after conversion to type int.
doubleValue() is an inbuilt method in java that returns the value of the specified number casted as a double data type. This may involve rounding or truncation. Syntax: public abstract double doubleValue() Parameters: This method does not accepts any parameter.
An IntValue is an object that stores a single signed 64-bit integer. Integers do not include decimal points. The highest value that can be stored is 2^63-1, or around 9.2 quintillion. Attempting to store numbers larger than this may cause integer overflow.
l.get(i);
will return Integer
and then calling intValue();
on it will return the integer as type int
.
Converting an int
to Integer
is called boxing.
Converting an Integer
to int
is called unboxing
And so on for conversion between other primitive types and their corresponding Wrapper classes.
Since java 5, it will automatically do the required conversions for you(autoboxing), so there is no difference in your examples if you are working with Java 5 or later. The only thing you have to look after is if an Integer
is null, and you directly assign it to int
then it will throw NullPointerException.
Prior to java 5, the programmer himself had to do boxing/unboxing.
As you noticed, intValue
is not of much use when you already know you have an Integer
. However, this method is not declared in Integer
, but in the general Number
class. In a situation where all you know is that you have some Number
, you'll realize the utility of that method.
The Object returned by l.get(i) is an instance of the Integer class.
intValue() is a instance method of the Integer class that returns a primitive int.
See Java reference doc... http://docs.oracle.com/javase/6/docs/api/java/lang/Integer.html#intValue()
Java support two types of structures first are primitives, second are Objects.
Method that you are asking, is used to retrieve value from Object to primitive.
All java types that represent number extend class Number. This methods are in someway deprecated if you use same primitive and object type since [autoboxing] was implemented in Java 1.5.
int - primitive
Integer - object
Before Java 1.5 we was force to write
int i = integer.intValue();
since Java 1.5 we can write
int i = integer;
Those methods are also used when we need to change our type from Integer to long
long l = integer.longValue();
Consider this example:
Integer i = new Integer(10);
Integer j = new Integer(10);
if (!(i == j)) {
System.out.println("Surprise, doesn't match!");
}
if (i.intValue() == j.intValue()) {
System.out.println("Cool, matches now!");
}
which prints
Surprise, doesn't match!
Cool, matches now!
That proves that intValue()
is of great relevance. More so because Java does not allow to store primitive types directly into the containers, and very often we need to compare the values stored in them. For example:
oneStack.peek() == anotherStack.peek()
doesn't work the way we usually expects it to work, while the below statement does the job, much like a workaround:
oneStack.peek().intValue() == anotherStack.peek().intValue()
get(i)
will return Integer
object and will get its value when you call intValue()
.In first case, automatically auto-unboxing
happens.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With