• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

wild cards

 
Greenhorn
Posts: 14
Android Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now I got it! Thank all of you.
 
I am not a spy. Definitely. Definitely not a spy. Not me. No way. But this tiny ad ...
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic