• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

negation of integers

 
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class TestClass
{
public static void main(String[] args) throws Exception
{
int a = Integer.MIN_VALUE;
int b = -a;
System.out.println( a+ " "+b);
}
}

Output
-2147483648 -2147483648
can anybody explain me y r v getting -ve value for b.
As b is give -a the output b should be +ve why is it -ve.
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Integer.MIN_VALUE is -(2^32). Integer.MAX_VALUE is (2^32)-1. That minus 1 is important. If you negate (or take the absolute value of) Integer.MIN_VALUE, it silently overflows and results in itself, because an Integer can not represent 2^32. If you cast these into a Long before negation, you will see the behavior you expect.

Hope This Helps,
jdmaddison
 
meena latha
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok joseph....assigning -a to b is just like assigning b with Math.abs(a)
so idly it should result in error.

Y it is not happening.

Pls explain
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

can anybody explain me y r v getting -ve value for b.
As b is give -a the output b should be +ve why is it -ve.


You were asked before to stop using "IM talk" in your posts. Many members of this board have trouble with English words and pronunciation. They will have not understand "y r v" or "+ve", which are based on how words sound.

We want to help everyone pass the SCJP exam, not just people who can understand "IM talk". Please be nice to all your friends in JavaRanch and use regular English.

Don't worry about perfect grammar and spelling - we all make mistakes - just do your best.

Thanks.
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As Ramya said , if negate performs abs() then compiler error should result.
why is it not happening???
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My best guesses as to why Java throws an exception for integer divide by 0 but not for abs(Integer.MIN_VALUE) are:

1. Dividing by zero is much more common and worth the overhead of checking.

2. Taking the absolute value of a class constant is probably deliberate, not an accident, so the programmer should know the result.

3. The hardware throws an interrupt for dividing by zero, so checking for divide by zero is free.

Pick a reason.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As Ramya said , if negate performs abs() then compiler error should result.

Ramya didn't say compiler error. It's possible a run-time exception was intended here. However, has anyone tried replacing

with

to see what happens? I believe your guesses about its behavior are incorrect.

I would say that throwing an AritmeticException would have been a good, reasonable action for the JVM to take here. But that's not what Sun chose to implement. It's really an example of a much larger problem: silent overflow (or underflow). Consider:

This prints -67153019, which is obviously not the answer most people would expect. The problem is that the correct answer 121932631112635269 is implicitly cast to an int (because someone thought it made sense that an int times and int should result in another int), and since it's too big for an int, the higher bits are simply removed. I think it would be much better if an error were thrown here, but Sun chose to do otherwise. I believe this was a poor decision which can easily create bugs. But it's unlikely to change at this point.
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I think it would be much better if an error were thrown here, but Sun chose to do otherwise. I believe this was a poor decision which can easily create bugs. But it's unlikely to change at this point.


Considering that there are many important agorithms for hashing and pseudorandom number generation that depend on fixed-point arithmetic overflow, I agree that any change is unlikely.

You might consider the overhead involved if every integer operation included an overflow check, with no hardware interrupt available to help.

Perhaps you should suggest that Sun add a strictInt modifier to Java 6.
 
get schwifty. tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic