This week's book giveaway is in the Performance forum.We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!

# char compability...

Dan Silva
Ranch Hand
Posts: 86
Why are chars compatible w/ ints? And what another things abouta method with a primitive return type, being able to "return any value or variable that can be implicitly converted to the declared return type"? I'm having a hard time with this. Will someone please explain?
P.S. the citation above comes from the Java 5 Certification book, by Bates and Sierra. It's on pg. 124.

Deepak Chopra
Ranch Hand
Posts: 433
Hi Char are actually Unsigned 16 bit integer.

Unsigned means that only positive values

As you must be knowing that int are 32 bit signed values
that means there values may range from -65536 to 65535
Similarly char(16 bit) values range from 0 to 65535

Broadening: Java implicitly convert a small bits primitive to broader bits primitive.
for eg: int(32) converted to long(64 bit)

Same level implicit casting :
Java can also implicit convert same level primitive into each other if and only if there is no loss of data. for eg :

int(32 bit) can be implicitly cast to float(32 bit)
23 ----> 23.0
but we reverse is not possible:

float(32 bit) -----> int(32 bit) //can not possible
Because if
23.56 ----> 23 ( loss = .56 )

So Java can implicitly cast positive integer to char also
as positive integer = 0 to 65535
and char = 0 to 65535

int i = 65535;
char c = i;

But if you change it to
int i = -6532;
char c = i; ( COMPILER ERROR !!!)
so we need explicit cast in this case

char c = (char) i;

Henry Wong
author
Marshal
Posts: 21197
81
As you must be knowing that int are 32 bit signed values
that means there values may range from -65536 to 65535

Actually, the range of 32 bits (signed) is much larger. What you are describing is for a 17 bit signed datatype.

float(32 bit) -----> int(32 bit) //can not possible
Because if
23.56 ----> 23 ( loss = .56 )

Actually, while what you are describing is true -- the implicit casting rule is due to range (not precision). Meaning the range of a float can be smaller than the minimum value of an int, and can be larger than the maximum value of int.

Henry

Dan Silva
Ranch Hand
Posts: 86
Can a byte or short be implicitly cast into a char? Thanks.

Henry Wong
author
Marshal
Posts: 21197
81
Originally posted by Dan Silva:
Can a byte or short be implicitly cast into a char? Thanks.

You do know that it is very simple to just try it out...

To answer your question, No. A byte or short can't be implicitedly casted to a char. The reverse is also disallowed. A char can't be implicitedly casted to a byte or a short.

It has to do with range. A byte or short has negative numbers which a char can't hold. And a char has values at its high range, which is out of range for a byte or short. This means that both directions needs to be casted explicitedly.

Henry

Dean Jones
Ranch Hand
Posts: 129
With explicit cast and loss of precision,we can convert any form to another