wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes int's range is a big confusion...! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "int Watch "int New topic
Author

int's range is a big confusion...!

golu jain
Greenhorn

Joined: Jan 10, 2001
Posts: 19
hi folks!

I have a basic question:-
1.>int i=2147483648;
The above code will generate the compile time error for the value being one more than the positive range.But....
2.>int i=-(-2147483648);
The above code will compile successfully.and the output will be
'-2147483648'.
Why does this things compiles b'coz after negating those minus signs the value is still out of int's range.
Help me
Golu Jain
Paul Anilprem
Enthuware Software Support
Ranch Hand

Joined: Sep 23, 2000
Posts: 3254
    
    2
The reason is the range of signed types is not symetric. For. eg. int's range is -2^31 to 2^31-1. You can see that, it can store 1 negative number more than positive no.
Now, at runtime when you negate the largest -ive no., it's positive value falls outside the range by 1. So, in effect you get the same negative number.
HTH,
Paul.
------------------
Get Certified, Guaranteed!
(Now Revised for the new Pattern)
www.enthuware.com/jqplus


Enthuware - Best Mock Exams and Questions for Oracle/Sun Java Certifications
Quality Guaranteed - Pass or Full Refund!
 
jQuery in Action, 2nd edition
 
subject: int's range is a big confusion...!
 
Similar Threads
int range
Math.abs()
Math.round()
about Math.abs()
Reg. maximim int. value