• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

integer Value

 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, in the following code below the maximum value of integer is 2147483647 how when we add 1 to it it's not giving any errors ???


class Q1
{
public static void main(String arg[])
{
byte b=1;
b++;
int i=2147483647+1;
System.out.println(i);
}
}
 
Ranch Hand
Posts: 1704
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Raj Kumar:
Hi, in the following code below the maximum value of integer is 2147483647 how when we add 1 to it it's not giving any errors ???


class Q1
{
public static void main(String arg[])
{
byte b=1;
b++;
int i=2147483647+1;
System.out.println(i);
}
}



But i value will be "-2147483648", instead of 2147483648
 
Raj Kumar
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thnaks for the reply...

but i don't know how to get that ans. manually

pla. help
 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I find it interesting that the compiler didn't complain. I guess int overflows are allowed during compilation as well. Learn something new everyday...

Henry
 
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Integer being a signed 16-bit number holds values between -2^15 t0 2^15-1 range. The maximum value being the one you have given in binary would be 0111 1111 1111 1111. So when you add one the result is 1111 1111 1111 1111. The left most bit being 1 represents -ve value and so the result value is the same with a -ve sign.This is the reason why you are getting that value.

Thanks
Chandu
 
Kj Reddy
Ranch Hand
Posts: 1704
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Raj Kumar:
Thnaks for the reply...

but i don't know how to get that ans. manually

pla. help



Use long instead of int type.
 
Chandra Sagi
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am sorry, int is a 32-bit number, the same argument holds good though.

Chandu
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the issue is that the compiler does not look at the values being added together. The compiler is simply checking syntax in this case. In that line you are trying to assign an int variable to the output of a + between 2 ints. The compiler doesn't perform the + so it can't know the size of the outcome.



Both assignment statements are allowed.
[ February 24, 2006: Message edited by: Keith Lynn ]
 
Raj Kumar
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
01111111111111111111111111111111 ( 2147483647 )
00000000000000000000000000000001 ( 1 )
--------------------------------
10000000000000000000000000000000 ( -ve )


i think i got that Correct from all your help
 
Raj Kumar
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes Keith Lynn ,

when we take this example

byte b = 127 +1;

compile won't say that the size can't fit into byte but it says the outcome of the above is an integer and we are trying to fit that to a byte .
 
Chandra Sagi
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
10000000000000000000000000000000 is correct and it represents the value you got. You need to calculate 2's complement for this number since it has a 1 in the left most place which represents that its a negative number.

To calculate a 2's complement flip all the bits and add 1 to it.Whatever value you get keep a negative sign before it. The answer you are getting is correct and there are no errors anywhere.

Thanks
Chandu
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Raj Kumar:
yes Keith Lynn ,

when we take this example

byte b = 127 +1;

compile won't say that the size can't fit into byte but it says the outcome of the above is an integer and we are trying to fit that to a byte .



By default, a whole number is considered to be an int.

There are cases where the compiler allows a direct assignment of an int to a byte.



But if you try this assignment


then the compiler will complain because what it sees is two ints being added together (even if the addends were bytes, they are going to be promoted to ints), and its not going to allow that to be assigned into a byte without a cast.
 
Chandra Sagi
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
byte b = 127 +1;

compile won't say that the size can't fit into byte but it says the outcome of the above is an integer and we are trying to fit that to a byte .
----------------------------

whenever you perform any operation the result would be minimum of an Integer value. Inorder to avoid an error we need to type cast to the required value.

byte b = 127 + 1; (gives an error) while
byte b = 127;
b+ = 1;
doesn't, this operator does the typecasting automatically

Thanks
Chandu
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note what happens when this code is compiled.



The compiler complains.



Because in a binary operation like this, if either of the types is long, then both addends are converted to long and the type of the result is a long.

Otherwise, the two addends are converted to int and the type of the result is an int.



This also causes a compile-time error because of type.


[ February 24, 2006: Message edited by: Keith Lynn ]
 
Liar, liar, pants on fire! refreshing plug:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic