wood burning stoves 2.0*
The moose likes Java in General and the fly likes Favouring Integer for addition calculations rather than BigDecimal- precision of addition using int Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Favouring Integer for addition calculations rather than BigDecimal- precision of addition using int " Watch "Favouring Integer for addition calculations rather than BigDecimal- precision of addition using int " New topic
Author

Favouring Integer for addition calculations rather than BigDecimal- precision of addition using int

Amandeep Singh
Ranch Hand

Joined: Jul 17, 2008
Posts: 844
I am getting result of -1,1,0 as a result of compare operation.

Now i want to add this value to db column value, as the above value is in int type thats why now using the Integer.

SnapShot of the code:


Question: Is doing addition operations recommended through int rather than BigDecimal because it will not loose its precision ?

Question: does intValue() loose it precision after certain range.

if not i can change the above code to like this.


SCJP 1.4, SCWCD 5, SCBCD 5, OCPJWSD 5,SCEA-1, Started Assignment Part 2
My blog- http://rkydesigns.blogspot.com
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19695
    
  20

All integer types (byte, char, short, int and long) never lose any precision, but they have limited range. If this is not going to be a problem using ints or longs is just fine.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Amandeep Singh
Ranch Hand

Joined: Jul 17, 2008
Posts: 844
will they loose precision after the limited range. so how much is the limited range?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

I'm pretty sure the API documentation tells you what the value of (for example) Integer.MAX_VALUE is.
W. Joe Smith
Ranch Hand

Joined: Feb 10, 2009
Posts: 710
Amandeep Singh wrote:will they loose precision after the limited range. so how much is the limited range?


If you try to assign a value to a primitive type that is out of its range the JVM will yell at you. You can't assign a value to a primitive that is outside of its range unless you explicitly cast it to the desired type, but it will result in loss of precision.


SCJA
When I die, I want people to look at me and say "Yeah, he might have been crazy, but that was one zarkin frood that knew where his towel was."
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19695
    
  20

If you add an int to an int, and the resulting value would become larger than Integer.MAX_VALUE, it will wrap - becoming negative. If you believe this is a possibility, use long. If the same issue can occur for long (becoming larger than Long.MAX_VALUE), BigInteger and BigDecimal are the only options.
Amandeep Singh
Ranch Hand

Joined: Jul 17, 2008
Posts: 844
Well i will go with BigDecimal. Integer.Max_value has a limit of 2147483647.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Favouring Integer for addition calculations rather than BigDecimal- precision of addition using int