File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

about return types in method

 
Sri Sanjana
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Matthew Cox
Greenhorn
Posts: 29
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Rob Spoor
Sheriff
Pie
Posts: 20398
47
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
SCJP Sun Certified
Programmer for Java 5
 
salvin francis
Bartender
Pie
Posts: 1230
10
Eclipse IDE Google Web Toolkit Java
  • 0
  • Mark post as helpful
  • send pies
  • 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
?
 
Joanne Neal
Rancher
Pie
Posts: 3742
16
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
Pie
Posts: 3742
16
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Paul Yule
Ranch Hand
Posts: 229
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic