File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "int Watch "int New topic

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

golu jain

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
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: 3635
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.
Get Certified, Guaranteed!
(Now Revised for the new Pattern)

Enthuware - Best Mock Exams and Questions for Oracle Java Certifications
Quality Guaranteed - Pass or Full Refund!
I agree. Here's the link:
subject: int's range is a big confusion...!
It's not a secret anymore!