The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes why a byte cant be assigned to a char? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Professional Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "why a byte cant be assigned to a char?" Watch "why a byte cant be assigned to a char?" New topic
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?
 
Threads others viewed
Cannot convert char to byte
Assignment
JvalTest
Q how does the above code give error and the code below doesn't???
Casting
MyEclipse, The Clear Choice

cast iron skillet 49er

more from paul wheaton's glorious empire of web junk: cast iron skillet diatomaceous earth rocket mass heater sepp holzer raised garden beds raising chickens lawn care CFL flea control missoula heat permaculture