Win a copy of Kotlin in Action this week in the Kotlin forum!
programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering Languages Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Why a & b are same ?

Shalini Banerjee
Greenhorn
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
Greenhorn
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 :
MIN_VALUE..:-2147483648
MAX_VALUE..:2147483647
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.

Shalini Banerjee
Greenhorn
Posts: 22

Bert Bates
author
Sheriff
Posts: 8945
17
Just a reminder that this is a topic on the 1.4 exam, but not on the 1.5 exam.

Bob Law
Ranch Hand
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
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.
Eg:
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);
}
}

Output:
-2147483648 -2147483648 2147483648

Hope this helps u,

Regards
Naresh