• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

char compability...

 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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;
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can a byte or short be implicitly cast into a char? Thanks.
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
With explicit cast and loss of precision,we can convert any form to another

 
reply
    Bookmark Topic Watch Topic
  • New Topic