CASTING - II



Casting to (byte)

Within the range:

A byte is signed and can hold a range of numbers from -128 to 127. If the value of the widest type is within this range, conversion won't produce unexpected results.

Example:

int a = -128;

byte x = (byte)a;

float a = -128.0f;

byte x = (byte)a;

Result in both cases: -128

Outside the range but within the signed byte range:

If the value is between 128 and 255, it will be converted to binary and then to the byte decimal representation of that binary pattern. In fact, this bit-level interpretation always occurs, but you have to be conscious about it for this special case.

Example:

int a = 128;

byte x = (byte)a;

Result: -128

The bit pattern for 128 is 10000000, but 10000000 is considered to be a signed byte. Thus 10000000 is equal to -128. The next binary number, 10000001, equals to -127. If byte was unsigned, as in C/C++, the decimal value of 1000001 would be 129.

Example:

int a = 129;

byte x = (byte)a;

Result: -127

Outside the signed byte range:

If the value is greater than 255 or lower than -128, the lower byte of the value is kept and the rest is just thrown away.

Example #1:

int a = 257;

byte x = (byte)a;

Result: 1

257 = [00000000] [00000000] [00000001] [00000001]

32-bits int value

1 = [00000001]

8-bits byte value

Example #2:

int a = -135;

byte x = (byte)a;

Result: 121

-135 = [11111111] [11111111] [11111111] [01111001]

32-bits int value

121 = [01111001]

8-bits byte value

Casting to (char)


Within the range:

A char is 16-bits wide unsigned type that holds values between 0 and 65535. Conversion will perform as expected if the value is within the valid range.

Example:

int a = 65535;

char x = (char)a;

Result: 65535

Outside the range or negative:

If the value is outside the range because it is lower than 0 or greater than 65535, then the lower 2 bytes will be kept.

Example #1:

int a = 65539;

char x = (char)a;

Result: 3

65539 = [00000000] [00000001] [00000000] [00000011]

32-bits int value

3 = [00000000] [00000011]

16-bits char value

Example #2:

int a = -1;

char x = (char)a;

Result: 65535

-1 = [11111111] [11111111] [11111111] [11111111]

32-bits int value

65535 = [11111111] [11111111]

16-bits char value

Casting to (short) and other signed integer values

Values between -32768 and 32767 are converted flawlessly as they are within the valid range. If the value is lower or greater, the lower 2 bytes of the value will be kept to conform a short value. The behaviour is the same as byte casting.

Other integer values will also behave as expected according to what we've seen at the byte examples.

Casting floats or doubles to narrower types

On some programming languages, conversion from floating-point numbers to decimals "round" the value. This is not the Java case, the integer part is kept and the rest is thrown away.

Example:

double a = 1.9999999;

int x = (int)a;

Result: 1

Object Reference Conversion

Object reference conversion takes place in:

* Assignment
* Method call
* Casting

(There is no arithmetic promotion)

Assignment

Object reference assignment conversion happens when you assign an object reference value to a variable of a different type.

There are 3 general kinds of object reference type:

* A class type, such as Button or TextField
* An interface type, such as Clonable or LayoutManager
* An array type, such as int[][] or TextArea[]
*

Conversion rules for implicit casting on this context:

OLD_Type a = new OLD_Type;

NEW_Type b = a;

1 comments:

Anonymous said...

is downcasting is possible in java...

Post a Comment