I tried to print the results, but unable to get the reason behind this.Anyhelp would be greatly appreciated.
Asked
Active
Viewed 1,061 times
-3
-
3What language? How do you "print" it? Show us some code. – Max Truxa Jun 28 '15 at 17:27
-
Uh what language is this? – Nick Bailey Jun 28 '15 at 17:27
-
I tried printing in java: – Gautam Tyagi Jun 28 '15 at 17:38
-
System.out.println( (Integer)1 == (Integer) 1); System.out.println( (Integer)200 == (Integer)200); – Gautam Tyagi Jun 28 '15 at 17:38
1 Answers
-1
I guess we're talking about java? (:
Java has a very weird way of storing integers. I can give you the long answer, but it can be easily found around the forums.
Long story short, when integers pass 127, their length becomes too long to store normally, and thus the default comparing operator between them does not work properly. If you need to make this comparison, I'd advise using some other numeric type, but usually just storing them and using the .equals()
function would do - It compares objects by value. Example:
//in some package , inside some function
// if((Integer)128==(Integer)128)
// {
// // this will never happen
// }
// if((Integer)128).equals((Integer)128))
{
// this will happen.
}

Community
- 1
- 1

A. Abramov
- 1,823
- 17
- 45
-
I tried in java language. So, what exactly happens when it crosses 127 limit. – Gautam Tyagi Jun 28 '15 at 17:44
-
-
3"*when integers pass 127, their length becomes too long to store normally, and thus the default comparing operator between them does not work properly*" - This is not a good/correct explanation of what's going on. – Oliver Charlesworth Jun 28 '15 at 19:58