Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# integer Value

Raj Kumar
Ranch Hand
Posts: 65
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);
}
}

Kj Reddy
Ranch Hand
Posts: 1704
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

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

pla. help

Henry Wong
author
Marshal
Posts: 21759
85
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

Chandra Sagi
Ranch Hand
Posts: 162
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
Originally posted by Raj Kumar:

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
I am sorry, int is a 32-bit number, the same argument holds good though.

Chandu

Keith Lynn
Ranch Hand
Posts: 2409
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
01111111111111111111111111111111 ( 2147483647 )
00000000000000000000000000000001 ( 1 )
--------------------------------
10000000000000000000000000000000 ( -ve )

i think i got that Correct from all your help

Raj Kumar
Ranch Hand
Posts: 65
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
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: 2409
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
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: 2409
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 ]