• Post Reply Bookmark Topic Watch Topic
  • New Topic

wild cards  RSS feed

 
Greenhorn
Posts: 14
Android Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I am struggling with these two pieces of code:

this one compiles and runs fine:



and this one doesn't compile (cannot find symbol
symbol : method add(java.lang.Throwable)
location: interface java.util.List<capture#493 of ? super java.lang.Exception>
list.add(new Throwable());):



Can somebody help me to understand what is going on here?

To be more exact, compiler error appears on the 2nd line...
 
Ranch Hand
Posts: 157
1
Android Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Exception is Throwable not otherwise. [Exception extends Throwable]. So it looks fine to me.
 
Radovan Masaryk
Greenhorn
Posts: 14
Android Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
harshvardhan ojha wrote:Exception is Throwable not otherwise. [Exception extends Throwable]. So it looks fine to me.


What do you mean harshvardhan? I know that Exception is subclass of Throwable. But I do not understand why that compiler error apeared on the 2nd line.
 
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you need is:

which allows you to add any object of type Throwable or sub class or Throwable.
 
Radovan Masaryk
Greenhorn
Posts: 14
Android Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Tony,

Yeah that's correct, but this is more about what SCJP/OCPJP needs from me And I am little bit confused why that code doesn't compile.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It doesn't compile because <? super Exception> is saying you have a List that takes an Exception type or any sub class of Exception but you are then adding a Throwable object to it and Throwable is a super type of Exception not a sub type of it.
 
Radovan Masaryk
Greenhorn
Posts: 14
Android Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:It doesn't compile because <? super Exception> is saying you have a List that takes an Exception type or any sub class of Exception but you are then adding a Throwable object to it and Throwable is a super type of Exception not a sub type of it.


Hi Tony,

that is not true. 'super' means sperclass is expected. You are talking about the case if 'extends' keyword would be used.
 
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, but the effect of that in a wild card can be slightly counter-intuitive.

A List<? super Exception> will reference a List of Exception or any class it extends. So It could be a List<Exception>, a List<Throwable> or a List<Object>.

Now, the compiler will stop you adding things to the List that it doesn't know are safe. So what can you safely put in all those collections? You can't add a Throwable, because that can't be added to a List<Exception>. The only things that can be safely added to all of them are instances of Exception or subclasses of Exception.
 
Radovan Masaryk
Greenhorn
Posts: 14
Android Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now I got it! Thank all of you.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!