File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

assignment

 
Vidhya Hari
Greenhorn
Posts: 20
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 163
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Output:
97
( ASCII value of 'a' is 97 )
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See this thread for details on narrowing assignment conversions.
 
Vidhya Hari
Greenhorn
Posts: 20
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
    Posts: 20
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks Marc...i understand it better now...Does the same thing hold good for assignments b/w char & short also?
     
    marc weber
    Sheriff
    Posts: 11343
    Java Mac Safari
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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!
     
    I agree. Here's the link: http://aspose.com/file-tools
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic