# "-" operator

Jason Attin

Ranch Hand

Posts: 232

2

posted 1 month ago

Hi guys, here is another question which I got wrong and I'm not really sure how I can work out the answer to such a question

Glenn, Mitchell. OCAJP Oracle Certified Associate Java SE 8 Programmer Practice Exams (Kindle Locations 18809-18814). Enthuware. Kindle Edition:

Right, so I anwered D but the correct answer is B. I applied some basic logic here and - not knowing by heart the min value of an integer - i picked a large number say -xxxxxxxxxx and stored the value in a and then did

b = -(-xxxxxxxxxx) which gives me a positive number. But, of course, I was wrong. SO the explanation goes into a long description of bits etc, stuff that I thought it wouldn't be needed for the exam. ANy quick and dirty way to figure out what it prints without going into bits?

Glenn, Mitchell. OCAJP Oracle Certified Associate Java SE 8 Programmer Practice Exams (Kindle Locations 18809-18814). Enthuware. Kindle Edition:

What happens when you try to compile and run the following class...

Select 1 option

A. It throws an OverFlowException.

B. It will print two same negative numbers.

C. It will print two different negative numbers.

D. It will print one negative and one positive number of same magnitude.

E. It will print one negative and one positive number of different magnitude.

Right, so I anwered D but the correct answer is B. I applied some basic logic here and - not knowing by heart the min value of an integer - i picked a large number say -xxxxxxxxxx and stored the value in a and then did

b = -(-xxxxxxxxxx) which gives me a positive number. But, of course, I was wrong. SO the explanation goes into a long description of bits etc, stuff that I thought it wouldn't be needed for the exam. ANy quick and dirty way to figure out what it prints without going into bits?

posted 1 month ago

I agree, but am not completely sure, but... yes, I highly doubt that knowledge of the Twos Complement format would be needed for the test...

The short answer is, with the Twos Complement format, the range isn't balanced. The negative of MIN_VALUE is a positive number that is larger than MAX_VALUE, and specifically, it will overflow and become MIN_VALUE again.

If you really want to know why, then I suggest doing some research on Two Complement. If you are *not* interested, and just want a "quick and dirty" way to remember, it is simple. This is the only case where this happens -- meaning, this is the only case where negating will cause an overflow.

Henry

Jason Attin wrote:

Right, so I anwered D but the correct answer is B. I applied some basic logic here and - not knowing by heart the min value of an integer - i picked a large number say -xxxxxxxxxx and stored the value in a and then did

b = -(-xxxxxxxxxx) which gives me a positive number. But, of course, I was wrong. SO the explanation goes into a long description of bits etc, stuff that I thought it wouldn't be needed for the exam. ANy quick and dirty way to figure out what it prints without going into bits?

I agree, but am not completely sure, but... yes, I highly doubt that knowledge of the Twos Complement format would be needed for the test...

The short answer is, with the Twos Complement format, the range isn't balanced. The negative of MIN_VALUE is a positive number that is larger than MAX_VALUE, and specifically, it will overflow and become MIN_VALUE again.

If you really want to know why, then I suggest doing some research on Two Complement. If you are *not* interested, and just want a "quick and dirty" way to remember, it is simple. This is the only case where this happens -- meaning, this is the only case where negating will cause an overflow.

Henry