aspose file tools*
The moose likes Beginning Java and the fly likes about return types in method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "about return types in method" Watch "about return types in method" New topic
Author

about return types in method

Sri Sanjana
Greenhorn

Joined: Aug 12, 2009
Posts: 6
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
Matthew Cox
Greenhorn

Joined: May 25, 2009
Posts: 29
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
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14156
    
  19

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19696
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Sri Sanjana
Greenhorn

Joined: Aug 12, 2009
Posts: 6
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

Joined: Aug 12, 2009
Posts: 6
SCJP Sun Certified
Programmer for Java 5
salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 928

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
?


My Website: [Salvin.in] Cool your mind:[Salvin.in/painting] My Sally:[Salvin.in/sally]
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3566
    
  15
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


Joanne
Sri Sanjana
Greenhorn

Joined: Aug 12, 2009
Posts: 6
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

Joined: Aug 05, 2005
Posts: 3566
    
  15
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.
Paul Yule
Ranch Hand

Joined: May 12, 2008
Posts: 229
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: about return types in method