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 / July 2006

Tip: Looking for answers? Try searching our database.

Overflow/Underflow Exceptions

Thread view: 
rnurse@gmail.com - 14 Jul 2006 14:41 GMT
Hi All,

Is there an exception, error, etc that I can check for in the event an
over-/underflow?  For example, trying to put 129 into a byte variable.
The program will run fine.  But the result will be -127.

Thanks.
IchBin - 14 Jul 2006 15:04 GMT
> Hi All,
>
[quoted text clipped - 3 lines]
>
> Thanks.

Roedy  has something on this:
http://mindprod.com/jgloss/overflow.html

Thanks in Advance...
IchBin, Pocono Lake, Pa, USA              http://weconsultants.phpnet.us
__________________________________________________________________________

'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor,  Regular Guy (1952-)
Paul Davis - 14 Jul 2006 15:30 GMT
The problem occurs because the cast that lets the code compiles
implicitly auto-masks the bits.
For example, with the values you mentioned.
byte b = 129;
will generate a compiler error (Type Mismatch).
but, adding the cast lets it compile:
byte b = (byte)129;

To see this a little better, create a class with the following method
to show bits in a byte:
public void testConvert(int val){
    byte b = (byte)val;
    for(int i = 7;i>=0;i--){
        System.out.print( (b>>i)&1);
    }
    System.out.println("");
    System.out.println(b);
}
then disassemble it with: javap -s -c -classpath $CLASSPATH
yourClassName

You will see the following:
public void testConvert(int);
 Signature: (I)V
 Code:
  0:   iload_1
  1:   i2b
  2:   istore_2
  3:   bipush  7
  5:   istore_3
...

take note of the mnemonic "i2b", (integer to byte) (opcode 0x91) this
converts the integer with the rule intVal & 0xFF. This actually ignores
the sing of the incoming value. The reason you see the negative number
is because "byte" is a signed type, so when the high bit is on, you get
a negative number.
So, I afraid that if you want an exception to occur in this case, you
may want to replace casts with a method call, and let the method throw
the exception.
Along the lines of:
    private byte int2Byte(int val){
        byte b = (byte)val;
        if( (b&0xFF) != val){
            throw new RuntimeException("integer too big");
        }
        return b;
    }
Change the 0xFF to 0x7F to overflow your 129 value (129 does not really
overflow a byte)

hth


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.