• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

assignment

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Output:
97
( ASCII value of 'a' is 97 )
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
    Mac Safari Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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!
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    reply
      Bookmark Topic Watch Topic
    • New Topic