• 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

about return types in method

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello frnds,
I have small observation here,
In kathy sierra chapter 2 and section named "Returning a Value" it was stated "In a method with a primitive return type, you can return any value or
variable that can be implicitly converted to the declared return type".
Actually it is not true even we can return values which we can be forcibly(explicity) converted into lower data types.
For example I can return a value which is double and explicity typecasted to int.
Regards
Sri
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let's not waste forum space on posts like this. It's inappropriate and every CS/IS major, dev, tech etc.... has enough braggarts to deal with as is and no one is interested in having you report on an inaccurate detail in some book you forgot to mention the title of. To top it all off, it is not very stimulating as a topic as is ... not even open-ended.

regards,

Cox
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What's the problem, Matthew? Please remember one of JavaRanch's most important rules: Be Nice.

There is nothing inappropriate about Sri Sanjana's question.
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sri could have given more information (like the exact book), but as Jesper said, there is no need to snap. You can also politely ask for more information, or simply ignore the post altogether.
 
Sri Sanjana
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Please apolozize me .I havent mentioned the name of book.
The book name is SCJP Sun Certified
Programmer for Java 5.
I have mentioned my observation ,reason is most people
study the book for certification purpose.Some may not test the correctness of the mentioned points
through lab work.Even thought it is of less importance ,My observation may help them.
Regards
Sri
 
Sri Sanjana
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
SCJP Sun Certified
Programmer for Java 5
 
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
isnt the OP refering to a book by

kathy sierra

?
I googled and found that kathy is a co-author for "Head First Java"
are you referring to that book sri ??


could you throw some light on:

Actually it is not true even we can return values which we can be forcibly(explicity) converted into lower data types.
For example I can return a value which is double and explicity typecasted to int

?
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess the statement is open to interpretation, but in my view, if you explicitly cast a double to an int as part of the return, then you are actually returning an int and the statement is correct.
i.e.

is the same as


In fact, if you change the above code so that the method returns a long, you will have an explicit cast followed by an implicit cast

 
Sri Sanjana
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi francis,
In addition to HeadFirstJava there is one another book return by kathy named SCJP Sun Certified
Programmer for Java 5.
Hey Joanne Neal Next another thing what I want to say not only implicit casting even explicit casting of return types is
possible and compiler dont give any error.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sri Sanjana wrote:Hey Joanne Neal Next another thing what I want to say not only implicit casting even explicit casting of return types is
possible and compiler dont give any error.



I'm sorry, but I have no idea what point you are trying to make there. Perhaps you could try rephrasing it or showing an example.
 
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think his observation is that you can also return any value that can be explicitly converted to the return type.

Not that you're incorrect, but I think you miss the point of the statement in the book when you say that, though.

It's really more-so referring to this example given by Joanne:



The point being that you do not need to explicitly cast the value to the appropriate return type if the value is smaller than the return type.

The reason you have to explicitly cast something is because you have to tell the compiler that you understand the value may actually not cast appropriately so it tries to warn you ahead of time that when you cast your double to int you might lose precision. The point of the statement in the book is that you can return any primitive that is smaller than the return type without having to explicitly cast it because there is no chance of the conversion doing something incorrectly.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic