How to devide Integer value

  • Follow


Helo,

I need to divide Integer value by 2. Is it only one method to create
new Integer? Looks cumbersome.

Integer i = new Integer(10);
....
Integer i = New Integer(i.intValue()/2);

Thank You

0
Reply column.column (17) 3/22/2008 9:17:37 AM

On Mar 22, 5:17=A0am, column.col...@gmail.com wrote:
> Helo,
>
> I need to divide Integer value by 2. Is it only one method to create
> new Integer? Looks cumbersome.
>
> Integer i =3D new Integer(10);
> ...
> Integer i =3D New Integer(i.intValue()/2);
>
> Thank You

Yes, like the documentation says, Integer (and all the primitive
wrapper classes) objects are immutable.  However, if you're using Java
5 or later, you don't need to write out the unpacking to an int and
creation of new Integers yourself: the language will automatically
unbox and box primitives into their respective wrappers.

You could write the above as

Integer i =3D 10;
Integer j =3D i / 2;

and let Java worry about the rest.  Alternately, you could use int
instead of Integer.

-o
0
Reply Owen 3/22/2008 9:29:45 AM


Owen Jacobson wrote:
> On Mar 22, 5:17 am, column.col...@gmail.com wrote:
>> Helo,
>>
>> I need to divide Integer value by 2. Is it only one method to create
>> new Integer? Looks cumbersome.
>>
>> Integer i = new Integer(10);
>> ...
>> Integer i = New Integer(i.intValue()/2);
>>
>> Thank You
> 
> Yes, like the documentation says, Integer (and all the primitive
> wrapper classes) objects are immutable.  However, if you're using Java
> 5 or later, you don't need to write out the unpacking to an int and
> creation of new Integers yourself: the language will automatically
> unbox and box primitives into their respective wrappers.
> 
> You could write the above as
> 
> Integer i = 10;
> Integer j = i / 2;
> 
> and let Java worry about the rest.  Alternately, you could use int
> instead of Integer.

You can also write the OP's expression as
   Integer i = 10;
   i = i / 2;
Which the OP should find is not as "cumbersome".
0
Reply RedGrittyBrick 3/22/2008 11:32:15 AM

column.column@gmail.com wrote:
> I need to divide Integer value by 2. Is it only one method to create
> new Integer? Looks cumbersome.
> 
> Integer i = new Integer(10);
> ...
> /*Integer*/ i = New Integer(i.intValue()/2);

My guess is that you should use:

int i = 10;
i = i / 2;

and wrap in Integer when you need it.

Arne

0
Reply ISO 3/22/2008 3:44:42 PM

On Sat, 22 Mar 2008 02:17:37 -0700 (PDT), column.column@gmail.com
wrote, quoted or indirectly quoted someone who said :

>I need to divide Integer value by 2. Is it only one method to create
>new Integer? Looks cumbersome.
>
>Integer i = new Integer(10);
>...
>Integer i = New Integer(i.intValue()/2);

You can use primitive ints or let autoboxing simplify the code.

see http://mindprod.com/applet/converter.html
-- 

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
0
Reply see_website (4855) 3/25/2008 7:20:45 PM

4 Replies
98 Views

(page loaded in 0.105 seconds)


Reply: