File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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: 9023

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...


Javaranch SCJP FAQ | SCWCD Links
Lucas Smith
Ranch Hand

Joined: Apr 20, 2009
Posts: 717

Loosing of data precision may occur.
char holds only positive values while byte can take negative.


SCJP6, SCWCD5
Hunting for OCE:EJBD6...
Prithvi Sehgal
Ranch Hand

Joined: Oct 13, 2009
Posts: 735
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?
 
WebSphere development made easy
without the weight of IBM tools
http://www.myeclipseide.com