Jump to content

Java int value out of range check or exception

- - - - -

  • Please log in to reply
1 reply to this topic

#1
Marcinnnn

Marcinnnn

    Newbie

  • Members
  • PipPip
  • 17 posts
Hi,

In Java some types has limited range. For example int is between -2 147 483 648 and 2 147 483 647. So if I do something like this:
int z = 24*100000000;
System.out.println(z);
I will get -1894967296 which is of wrong. Is there anyway to implement anykind of exceptions for that? Is there any other way then casting to long and checking number range? And what about the situation if I'm already working with long types.

Thanks in advance!

#2
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
If you're overflowing longs, then you might try using the BigInteger class.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users