posted 14 years ago

I have two questions :

1. I know that the data type "byte" occupied 1 bytes in memory,and the value range is form -127 to +128.The data type "short" occupied 4 bytes in memory,and the value range is form -(2 to the power 31) to +(2 to the power 31)...etc.

Data type "float" occupied 4 bytes in memory,but its' value range is not from -(2 to the power 31) to +(2 to the power 31).It's from -3.4E38 to -1.4E45 in negative value and from 1.4E45 to 3.4E38 in positive value,because use scientific notation.

Although I have already know why the "float" range value is different form "int" is because scientific notation.But I can't imagine how to calculate the "float" value range.

2.Java has two kinds of variables,one is member variable,another is local variable.And the member variable is divide into two parts : static rariable and instance variable.One different from member variable and local variable is that member variable be initialized automatically and the local raviable is not.But how to distinguish member variable and local variable in program? Or how to distinguish static variable and instance variable?

Thanks for your answer.

1. I know that the data type "byte" occupied 1 bytes in memory,and the value range is form -127 to +128.The data type "short" occupied 4 bytes in memory,and the value range is form -(2 to the power 31) to +(2 to the power 31)...etc.

Data type "float" occupied 4 bytes in memory,but its' value range is not from -(2 to the power 31) to +(2 to the power 31).It's from -3.4E38 to -1.4E45 in negative value and from 1.4E45 to 3.4E38 in positive value,because use scientific notation.

Although I have already know why the "float" range value is different form "int" is because scientific notation.But I can't imagine how to calculate the "float" value range.

2.Java has two kinds of variables,one is member variable,another is local variable.And the member variable is divide into two parts : static rariable and instance variable.One different from member variable and local variable is that member variable be initialized automatically and the local raviable is not.But how to distinguish member variable and local variable in program? Or how to distinguish static variable and instance variable?

Thanks for your answer.

posted 14 years ago

short is 2 bytes, not 4.

static and not static can be distinguish by word "static"

Member variables are member of class likes:

class xxx {

int x;

float y;

String z;

}

x,y,z - memeber of class xxx.

Local variable likes:

boolean func(){

int x1;

String z1;

}

x1, z1 - local, will be created in the stack when function "func"

will be called.

[ March 15, 2004: Message edited by: Igor Ko ]

static and not static can be distinguish by word "static"

Member variables are member of class likes:

class xxx {

int x;

float y;

String z;

}

x,y,z - memeber of class xxx.

Local variable likes:

boolean func(){

int x1;

String z1;

}

x1, z1 - local, will be created in the stack when function "func"

will be called.

[ March 15, 2004: Message edited by: Igor Ko ]

posted 14 years ago

The values of a byte are -128 to +127. You have your signs backwards.

a short is -(2 ^ 15) through +((2^15) - 1)

you can read about floating point storage in the IEEE standard here, as well as many other places. just google IEEE floating point.

a short is -(2 ^ 15) through +((2^15) - 1)

you can read about floating point storage in the IEEE standard here, as well as many other places. just google IEEE floating point.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors

posted 14 years ago

To understand the formula for the range of a signed binary number that is T bits long (-2**(T-1) to 2**(T-1)-1), consider a 4 bit signed number.

The highest positive value is 0111 (7).

The lowest negative value is 1000 (-8).

This is because most computers use two's complement arithmetic. To convert 7 to -7, you first take the one's complement of 0111 which is 1000. You then add 1, giving 1001.

So the entire range from -8 to +7 is:

1000 1001 1010 1011 1100 1101 1110 1111 0000 0001 0010 0011 0100 0101 0110

-8 -7 -6 -5 -4 -3 -2 -1 0 +1 +2 +3 +4 +5 +6

0111

+7

The advantage of two's complement notation is that the hardware can do simple unsigned binary arithmetic and it all works out, with the high order bit as the sign (0 is +, 1 is -). The tricky part is that there is always 1 more negative value than the positive values in the range.

You won't need this stuff as a beginning programmer, except to remember the range formula, but once you need to consider the actual binary representations, for casts and for bitwise operators like ^, |, and &, you'll want to master binary arithmetic. Also, remember that adding 1 to the highest positive number gives you the highest negative number with no warning message or exception. 0111 + 0001 = 1000

The highest positive value is 0111 (7).

The lowest negative value is 1000 (-8).

This is because most computers use two's complement arithmetic. To convert 7 to -7, you first take the one's complement of 0111 which is 1000. You then add 1, giving 1001.

So the entire range from -8 to +7 is:

1000 1001 1010 1011 1100 1101 1110 1111 0000 0001 0010 0011 0100 0101 0110

-8 -7 -6 -5 -4 -3 -2 -1 0 +1 +2 +3 +4 +5 +6

0111

+7

The advantage of two's complement notation is that the hardware can do simple unsigned binary arithmetic and it all works out, with the high order bit as the sign (0 is +, 1 is -). The tricky part is that there is always 1 more negative value than the positive values in the range.

You won't need this stuff as a beginning programmer, except to remember the range formula, but once you need to consider the actual binary representations, for casts and for bitwise operators like ^, |, and &, you'll want to master binary arithmetic. Also, remember that adding 1 to the highest positive number gives you the highest negative number with no warning message or exception. 0111 + 0001 = 1000

Mike Gershman

SCJP 1.4, SCWCD in process

posted 14 years ago

Just to add to the confusion:

1. Where a method's variable and class's variable have the same name, the method's variable has the scope in the method. You can refer to the class variable by prefixing it with "this." if it is not static and the method you are in is not static, or with the class name if it is static.

2. In binary two's complement notation it's sometimes helpful to look at the value (weighting) of each column. The weighting of the most significant column is always negative. All the other columns add up to one less than the absolute value of the ms column. So for four bits:

-8 4 2 1

1 1 1 1 = -8 + 4 + 2 + 1 = -1

0 1 0 0 = 4

[ March 15, 2004: Message edited by: Eddie Vanda ]

1. Where a method's variable and class's variable have the same name, the method's variable has the scope in the method. You can refer to the class variable by prefixing it with "this." if it is not static and the method you are in is not static, or with the class name if it is static.

2. In binary two's complement notation it's sometimes helpful to look at the value (weighting) of each column. The weighting of the most significant column is always negative. All the other columns add up to one less than the absolute value of the ms column. So for four bits:

-8 4 2 1

1 1 1 1 = -8 + 4 + 2 + 1 = -1

0 1 0 0 = 4

[ March 15, 2004: Message edited by: Eddie Vanda ]

The nice thing about Standards is that there are so many to choose from!