• 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

Why a & b are same ?

 
Greenhorn
Posts: 22
  • 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);
}
}

Can any one explain the answer ?
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.


If any thing wrong with the answer Please correct me..
 
Shalini Banerjee
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not clear from your explanation. But thnx for your reply.
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just a reminder that this is a topic on the 1.4 exam, but not on the 1.5 exam.
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic