Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2008

Tip: Looking for answers? Try searching our database.

How to devide Integer value

Thread view: 
column.column@gmail.com - 22 Mar 2008 10:17 GMT
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
Owen Jacobson - 22 Mar 2008 10:29 GMT
On Mar 22, 5:17 am, column.col...@gmail.com wrote:
> Helo,
>
[quoted text clipped - 6 lines]
>
> 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.

-o
RedGrittyBrick - 22 Mar 2008 12:32 GMT
> On Mar 22, 5:17 am, column.col...@gmail.com wrote:
>> Helo,
[quoted text clipped - 21 lines]
> 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".
Arne Vajhøj - 22 Mar 2008 16:44 GMT
> 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
Roedy Green - 25 Mar 2008 20:20 GMT
>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
Signature


Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.