aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes char compability... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "char compability..." Watch "char compability..." New topic
Author

char compability...

Dan Silva
Ranch Hand

Joined: Sep 05, 2007
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.
Sunny Jain
Ranch Hand

Joined: Jul 23, 2007
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;


Thanks and Regards,
SCJP 1.5 (90%), SCWCD 1.5 (85%), The Jovial Java, java.util.concurrent tutorial
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19070
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Dan Silva
Ranch Hand

Joined: Sep 05, 2007
Posts: 86
Can a byte or short be implicitly cast into a char? Thanks.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19070
    
  40

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

Joined: Dec 29, 2007
Posts: 129
With explicit cast and loss of precision,we can convert any form to another

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: char compability...