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 assignment Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "assignment" Watch "assignment" New topic
Author

assignment

Vidhya Hari
Greenhorn

Joined: Mar 20, 2006
Posts: 20
is this assignment legal?

byte b2 = 'a';


We cant assign a character to a byte or short even with explicit cast..so will this work?
[ April 05, 2006: Message edited by: Vidhya Hari ]
Naresh Gunda
Ranch Hand

Joined: Oct 15, 2005
Posts: 163

Output:
97
( ASCII value of 'a' is 97 )
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

See this thread for details on narrowing assignment conversions.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Vidhya Hari
Greenhorn

Joined: Mar 20, 2006
Posts: 20
So from what was explained i understand that assigning a character to a byte or vice-versa is not possible w/o an explicit cast..Am i right?
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Vidhya Hari:
So from what was explained i understand that assigning a character to a byte or vice-versa is not possible w/o an explicit cast..Am i right?

No. The example in the other thread used ints, but the idea is the same.

Assignment conversion between bytes and chars can be done without an explicit cast if the value is within range and is either:
  • A literal, or...
  • A final variable whose value is known at compile time.
  • In the code below, the lines commented out will fail for the reasons indicated. But the other lines are fine.
    Vidhya Hari
    Greenhorn

    Joined: Mar 20, 2006
    Posts: 20
    Thanks Marc...i understand it better now...Does the same thing hold good for assignments b/w char & short also?
    marc weber
    Sheriff

    Joined: Aug 31, 2004
    Posts: 11343

    Originally posted by Vidhya Hari:
    ... Does the same thing hold good for assignments b/w char & short also?

    A few adjustment to the above code would demonstrate this. Give it a try!
     
    Consider Paul's rocket mass heater.
     
    subject: assignment
     
    Similar Threads
    WA #1.....word association
    reading STATUS FLAG 0/1
    coversion of final variable
    ternary operator doubt
    Casting Problem