wood burning stoves
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Why a & b are same ? 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 "Why a & b are same ?" Watch "Why a & b are same ?" New topic

Why a & b are same ?

Shalini Banerjee

Joined: Dec 06, 2005
Posts: 22
public class TestClass
public static void main(String[] args) throws Exception
int a = Integer.MIN_VALUE;
int b = -a;
System.out.println( a+ " "+b);

Can any one explain the answer ?
Viswanathan Ramasamy

Joined: Nov 14, 2005
Posts: 23
hi Shalini ,

The actual range for int is -2147483648 to 2147483647.
According to your program it is exceeding the range (Size of int).
So Implicitly type Casting is happening here.
Because of that the �a� and �b� are equal.

If you execute the following code you�ll get clear.

1 public class TestClass{
2 public static void main(String[] args) throws Exception
3 {
4 int a = Integer.MIN_VALUE;
5 int c = Integer.MAX_VALUE;
6 System.out.println("MIN_VALUE..:"+a);
7 System.out.println("MAX_VALUE..:"+c);
8 int b = -a;
9 int d=(c+1);
10 System.out.println( "a(MIN_VALUE)="+a+ ",b(-MIN_VALUE)= "+b);
11 System.out.println( "d(MAX_VALUE+1)="+d+ ",-d(MAX_VALUE+1)="+(-d));
12 }
13 }

Out put is :
a(MIN_VALUE)=-2147483648 , b(-MIN_VALUE)= -2147483648
d(MAX_VALUE+1)=-2147483648 ,- d(MAX_VALUE+1)=-2147483648

In the above programs output See the MAX_VALUE(2147483647) vale and MAX_VALUE+1(-2147483648) value.

If any thing wrong with the answer Please correct me..

With Regards<br />Viswa
Shalini Banerjee

Joined: Dec 06, 2005
Posts: 22
I am not clear from your explanation. But thnx for your reply.
Bert Bates

Joined: Oct 14, 2002
Posts: 8898
Just a reminder that this is a topic on the 1.4 exam, but not on the 1.5 exam.

Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Bob Law
Ranch Hand

Joined: Dec 16, 2005
Posts: 50
Let me try to explain a differnt way. Int has a range of -2147483648 to 2147483647 right. So when you make the Integer.MIN_VALUE positive it exceeds the the MAX_VALUE by 1. As Viswana said in the earlier post the value is implicitly type cast to fit into the int variable. Implicit type conversion, is an automatic type conversion by the compiler so value is cut inorder to fit into the memory allocated for an int variable. From low level view point the binary value 2147483647 is 01111111 11111111 11111111 11111111 but if you add one to that value in binary it becomes 1000000 0000000 0000000 0000000 which is eqaul to -2147483648 because the first bit is used for the sign. Does that make any more sense or did I just confuse you more.
Naresh Gunda
Ranch Hand

Joined: Oct 15, 2005
Posts: 163
Hi Shalini,
Integer arithmetic always returns a value that is in range. A valid value does not necessarily mean that the result is correct. Integer Arithmetic wraps if the result is out of range.
int a=Integer.MAX_VALUE +1 (i.e. 2147483647 + 1 )
but here the value of a is -2147483648, which is Integer.MIN_VALUE

long a= Integer.MAX_VALUE +1L
here the value of a is 2147483648L //in range of long

try this code

public class TestClass
public static void main(String[] args) throws Exception
int a = Integer.MIN_VALUE;
int b = -a;
long c= a * -1L;
System.out.println( a+ " "+b+" "+c);

-2147483648 -2147483648 2147483648

Hope this helps u,

I agree. Here's the link: http://aspose.com/file-tools
subject: Why a & b are same ?
It's not a secret anymore!