Win a copy of Event Streams in Action this week in the Java in General 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:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Return Type

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Reading OCA study guide for my exam. I'm confused what it says about Return Type.

The book says "When returning a value, it needs to be assignable to the return type." with below example.



It suggests not to return the value directly as below :



However, it doesn't mean we can't return the value directly. Is it because of performance? or any other reason?

Thanks
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This has nothing to do with performance; in fact, both of your examples will most likely compile to the exact same byte code, so there is no difference between them at all.

Your question seems to be about something different than what you mention from the book. All that the book says, is that the value that is returned must be of the right type - for example, when the method returns an int, then what is returned in the method must be assignable to the type int. It does not matter if the return value comes from a variable (as in your first example) or if it is an integer literal (as in your second example).
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Claire Chan wrote:However, it doesn't mean we can't return the value directly. Is it because of performance? or any other reason?


As already pointed out by Jesper, that's not what the book says. The book simply states that the value you want to return from a method must be assignable to the return type. It doesn't matter if it's directly or through a variable.

So given the method signature from your example int integer(), you can return a byte, char, short or int value (because all these can be assign to an int), but not a long, float or double value (because these values can't be assigned to an int). So these code snippets will not compileCan you figure out what's needed to make these code snippets compile successfully? And keep in mind that changing the return type of the method is not an option
 
Claire Chan
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Roel and Jesper. Now I get what the book says. Thank you for the clarification.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Roel De Nijs :



will this work??
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Lavanya Nemani wrote:will this work??


I don't know what your understanding of "will this work" is, but your code snippet will compile successfully.
 
Lavanya Nemani
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Roel De Nijs wrote:

Lavanya Nemani wrote:will this work??


I don't know what your understanding of "will this work" is, but your code snippet will compile successfully.




I was asking if my answer is correct for your question.
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Lavanya Nemani wrote:I was asking if my answer is correct for your question.


No, it's not! For the simple reason that my question clearly stated that you couldn't change the return type of the method (which you've clearly done).
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!