Win a copy of Learning Regular Expressions this week in the General Computing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

assignment  RSS feed

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

Output:
97
( ASCII value of 'a' is 97 )
 
Sheriff
Posts: 11343
Java Mac Safari
  • 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
  • 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
  • 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
    • 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
    • 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!
     
    Don't get me started about those stupid light bulbs.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!