| Author |
why a byte cant be assigned to a char?
|
karun narang
Greenhorn
Joined: Feb 12, 2010
Posts: 6
|
|
can any one tell me why cant we assign a byte to a char? the char is 16 bit and byte is ofcourse 8 bit.why this gives a compiler error.
class a
{
byte b=10;
char c=b;
}
|
 |
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9090
|
|
|
byte is signed i.e. it can have positive or negative value while char cannot have negative values, that's why a cast is required...
|
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
|
 |
Lucas Smith
Ranch Hand
Joined: Apr 20, 2009
Posts: 747
|
|
Loosing of data precision may occur.
char holds only positive values while byte can take negative.
|
SCJP6, SCWCD5, OCE:EJBD6.
|
 |
Prithvi Sehgal
Ranch Hand
Joined: Oct 13, 2009
Posts: 767
|
|
|
Character's can't have -ve values where byte is signed. That's why.
|
Prithvi/Beenish,
My Blog, Follow me on Twitter,Scjp Tips, When you score low in mocks, Generics,Scjp Notes, JavaStudyGroup
|
 |
karun narang
Greenhorn
Joined: Feb 12, 2010
Posts: 6
|
|
|
thank you all!
|
 |
 |
|
|
subject: why a byte cant be assigned to a char?
|
|
|