File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generics return type

 
Harikrishna Gorrepati
Ranch Hand
Posts: 423
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, This is from K&B mock exam.
Given answer is although String and StringBuffer are both subtypes of CharSequence, they are not necessarily
the same subtype as whatever is passed in (S)..I am not clear. Please let me know
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 33696
316
Eclipse IDE Java VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) S extends CharSequence. It could be any class that extends CharSequence and not necessarily String. What happens if I call fooC(StringBuffer)? Java needs to return a StringBuffer, but the method has a String.

2) What happens if you try to do return (StringBuilder) s.toString(); without generics. Why doesn't it work?

3) Same reason as #1.

4) Same reason as #2.
 
Paul Clapham
Sheriff
Pie
Posts: 20189
26
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, the given answer is correct.
 
Malte Wannerskog
Ranch Hand
Posts: 92
Java PHP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a question about this particular question aswell...

the line

compiles and runs just fine without any compiler warnings for me. Am I missing something?

Same goes for

No compiler warnings there aswell. Even though this was not an option in my question.

Thanks for your help
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch, Malte Wannerskog !
 
Malte Wannerskog
Ranch Hand
Posts: 92
Java PHP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Raghavan Muthu wrote:Welcome to JavaRanch, Malte Wannerskog !


Thank you, been reading posts here for a while but thought i would be a member now since Im studying for the SCJP exam

Any ideas on my question?
 
Malte Wannerskog
Ranch Hand
Posts: 92
Java PHP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Malte Wannerskog wrote:I have a question about this particular question aswell...

the line

compiles and runs just fine without any compiler warnings for me. Am I missing something?

Same goes for

No compiler warnings there aswell. Even though this was not an option in my question.

Thanks for your help


Ah nevermind, I realized that the compiler error didnt occur until you actually try to use the method .
For example with
 
michael schroeder
Greenhorn
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm, now I think I am missing something:



This program compiles with warnings, but runs fine. This is part of the mock questions of the SCJP Java 6 Study Guide for 310-065 (Quiz #56). The given answer to the question is, that both return statements will not compile or they will not run without errors. But the compiler thinks different about that.

 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic