• 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

return types!

 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!
I have a return type for a method as byte.if the value returned is a int, it will give compile time error(even if the int value is within the byte range).But if the value returned is a constant and is withing the byte range, it won't give error even without casting.Am I rite?Can anybody please explain.Thanks
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
In this case implicit casting takes place. There are three conditions which should be satified for implicit casting to take place:
1) The source should be a "constant expression" of any type from byte, char, short and int.
2) The Destincation should be any type from byte, char and short.
3) And the value of the source is with in range at complie time.
Eg
final int i = 12;
byte b = i;
Will not give any compile time error. Whereas,
int i = 12;
byte b = i;
will give.
Hopefully this will solve ur problem.
Cheers~
Sharad
 
Sridhar Srinivasan
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How about long value and double values.
for eg:
final long s=6;
byte b=s;(shouldn't give error) rite?
also final double d=8;
byte b=d; (shouldn't rite?)
 
Sharad Goel
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This implicit down casting doesnot work for Long, double and float.
 
Sridhar Srinivasan
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oh!is it?Thanks a lot for the valuable info..
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Can anyone explain this to me ?
final int i = 12;
byte b = i;
Will not give any compile time error. Whereas,
int i = 12;
byte b = i;
will give an error.
As pointed out, implicit casting needs to be done to remove the error. How does modifier 'final' make the source a constant expression and makes the casting implicit?
Sandeep
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sandeep, the final int i = 12; defines a constant because the final keyword guarantees to the compiler that i is never going to have a value other than 12. So the initial assignment to b can also be done at compiletime (because 12 fits into a byte).
In the second case: the compiler does not really know that the definition b occurs right after the assignment to i, so it does not know that i still contains 12 when int b = i; actually occurs. So the compiler requires the cast of i to an byte.
Remember the compiler does not runthe program when it compiles it. The problem is: our mind tries to do two things at once, compiling and running, and it's sometimes hard to separate them.
[ May 11, 2004: Message edited by: Barry Gaunt ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic