• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

byte to int??

 
Ranch Hand
Posts: 435
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Consider the following code:

Ans :It will not compile
Explaination:The return type of the method is byte, but it is returning int.
According to me, it will compile.Although the return type is byte, but 1 or 0(though int) can easily fit in byte.
Please let me know.
Sonir
 
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
first try it and see if it compiles.
i dont think it will cause constant numbers in java are considered int (like 5 10 1 etc).
unless you cast it to byte - which u can, it will treat it as int.
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It compiles for me. I think this is a case of implicit narrowing assignment of primative constants; since the compiler can verify that the number is within the range allowed for the return type, it allows it.
Rob
 
Roy Ben Ami
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i guess you r right then
 
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
the literals are only allowed without using casting only for return values for methods (the example in the question) and as Rob said, for conversions which are really valid. we can say,
byte b = 12;
but not byte b = 129;
in method's formal arguments, again implicit conversion is not applied.
so if we have,
void f(byte b) { } and if we try to call it by,
f(1) it won't work.
regards
maulin.
 
reply
    Bookmark Topic Watch Topic
  • New Topic