• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

Question on Lower bounds

 
Ranch Hand
Posts: 157
jQuery Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I am reading Jeanne boook for ocp 1.8 and i found that the following code is not compiling and i am confused in the code below



In the above code, exceptions is the list which can accept either IOException or Exception. But why does it not acceptiong Exception.

I got one more question is what's the difference between new ArrayList<Exception> vs new ArrayList<>.

Thanks & Regards

Swapna
 
Marshal
Posts: 24943
61
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Swapna latha wrote:



This is a List of some unknown class which is a superclass of IOException. Let's call that class X (the unknown). Then X could be IOException, or Exception, or Throwable, or Object. In particular X might be IOException, so adding an Exception object to the list would be wrong.

People often think that a List<? super IOException> can contain any object whose type is a superclass of IOException. That isn't the same thing and it isn't how Java handles it.

Or you might think that because you assigned a List<Exception> to that variable, that means something to the compiler. But it doesn't -- the compiler must ignore that because your code could subsequently assign a different value to the variable.
 
Swapna latha
Ranch Hand
Posts: 157
jQuery Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I havenot understood clearly. As per Kathy Sierra and Bert Bates ocp book , ? extends IOException means anything that extends IOException , so we can have even an Exception right. Why Java is not allowing it ? Am i wrong in understanding the concept. Whats the use of Right hand side of the expression which has generic type of Exceptionn .


Paul Clapham wrote:

Swapna latha wrote:



This is a List of some unknown class which is a superclass of IOException. Let's call that class X (the unknown). Then X could be IOException, or Exception, or Throwable, or Object. In particular X might be IOException, so adding an Exception object to the list would be wrong.

People often think that a List<? super IOException> can contain any object whose type is a superclass of IOException. That isn't the same thing and it isn't how Java handles it.

Or you might think that because you assigned a List<Exception> to that variable, that means something to the compiler. But it doesn't -- the compiler must ignore that because your code could subsequently assign a different value to the variable.

 
Sheriff
Posts: 9671
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Swapna latha wrote:I havenot understood clearly. As per Kathy Sierra and Bert Bates ocp book , ? extends IOException means anything that extends IOException , so we can have even an Exception right. Why Java is not allowing it ? Am i wrong in understanding the concept. Whats the use of Right hand side of the expression which has generic type of Exceptionn .


Your original question is  about ? super IOException, ? extends IOException is a similar case but not exactly the same. Let's look at both cases:

List<? super IOException> means the List can be of type IOException or its ancestors. Note here that List can actually be of type IOException as well. So if Java compiler allows you to add Exception to this list, it would break the type safety as you can see in the example below:

Now let's consider the ? extends IOException case. In this case, the List can be IOException or any of its descendants. If the List is actually of some sub-class of IOException, and compiler allows you to add IOException to the list, the following code will break:

Thus add operation is not allowed in either case. If you do a get operation however, you'll see different behavior. In case of ? super IOException, the List could actually be of type Object as well. But in case of ? extends IOException, you know that the elements in the List are compatible with IOException at least. So the get operation would work like this:
 
Swapna latha
Ranch Hand
Posts: 157
jQuery Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ankit for answerting but still has doubts.


What does the below line of code means

I got a program where it says "? super String" and we can add even object to it.



In the above code addSounds methods can take String and as well as Object since method accepts "? super String" anything ancestor to String.

Likewise in "? super IOException" we can add anything right.

Sorry for asking too many doubts, but i am confused.

Ankit Garg wrote:

Swapna latha wrote:I havenot understood clearly. As per Kathy Sierra and Bert Bates ocp book , ? extends IOException means anything that extends IOException , so we can have even an Exception right. Why Java is not allowing it ? Am i wrong in understanding the concept. Whats the use of Right hand side of the expression which has generic type of Exceptionn .


Your original question is  about ? super IOException, ? extends IOException is a similar case but not exactly the same. Let's look at both cases:

List<? super IOException> means the List can be of type IOException or its ancestors. Note here that List can actually be of type IOException as well. So if Java compiler allows you to add Exception to this list, it would break the type safety as you can see in the example below:

Now let's consider the ? extends IOException case. In this case, the List can be IOException or any of its descendants. If the List is actually of some sub-class of IOException, and compiler allows you to add IOException to the list, the following code will break:

Thus add operation is not allowed in either case. If you do a get operation however, you'll see different behavior. In case of ? super IOException, the List could actually be of type Object as well. But in case of ? extends IOException, you know that the elements in the List are compatible with IOException at least. So the get operation would work like this:

 
Paul Clapham
Marshal
Posts: 24943
61
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Swapna latha wrote:I got a program where it says "? super String" and we can add even object to it.



What you posted there is not the program you say you have. In the addSounds(List<? super String> list) method you only have one line of code which adds to that List. And it adds a String, which is fine. If you change it to add, say, new Object() you'll find that doesn't compile. There's no place in the code you posted which tries to add an Object to a List<? super String>.

Or perhaps you could point to the lines of code which you think are doing that?
 
Paul Clapham
Marshal
Posts: 24943
61
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Let's use a simpler version of what you wrote. Now, which of those lines of code compile and which don't?

Keep in mind that the compiler only knows the type of the variable which refers to the list object. It doesn't know the type of the object itself.
 
Swapna latha
Ranch Hand
Posts: 157
jQuery Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Paul, objects.add(new Object()) compiles and i can understand why it compiles as the list can add only of type Object. But why does not superString add new Object as the generic type says "? super String" and Object is super of String. I am tyring to understand it in deep. Thanks for making me to understand with the help of examples.

Paul Clapham wrote:

Let's use a simpler version of what you wrote. Now, which of those lines of code compile and which don't?

Keep in mind that the compiler only knows the type of the variable which refers to the list object. It doesn't know the type of the object itself.

 
Paul Clapham
Marshal
Posts: 24943
61
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Swapna latha wrote:Thanks for making me to understand with the help of examples.



I'm happy to hear that you understand the concept now.
 
Ankit Garg
Sheriff
Posts: 9671
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

But why does not superString add new Object as the generic type says "? super String" and Object is super of String.



Are you still confused? If you are let me try another example


Let's say I'm writing this API for anyone to call. The second call doesn't compile. Why? Because there is no guarantee that the List I'm passed can store Object. If the compiler allowed me to add Object to this List, it can break type safety. What if I wrote the following code to call this method:

If the  manipulateList method was allowed to add Object to stringSuperList, the above code will end up having an Object in a String list. An Object is-not-a String. If somehow I'm able to add Object to it, when I retrieve the element, I'll get ClassCastException. Generics is supposed to save you from ClassCastException, not let it happen.
 
Swapna latha
Ranch Hand
Posts: 157
jQuery Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Paul.

Paul Clapham wrote:

Swapna latha wrote:Thanks for making me to understand with the help of examples.



I'm happy to hear that you understand the concept now.

 
Swapna latha
Ranch Hand
Posts: 157
jQuery Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Ankit.

Ankit Garg wrote:

But why does not superString add new Object as the generic type says "? super String" and Object is super of String.



Are you still confused? If you are let me try another example


Let's say I'm writing this API for anyone to call. The second call doesn't compile. Why? Because there is no guarantee that the List I'm passed can store Object. If the compiler allowed me to add Object to this List, it can break type safety. What if I wrote the following code to call this method:

If the  manipulateList method was allowed to add Object to stringSuperList, the above code will end up having an Object in a String list. An Object is-not-a String. If somehow I'm able to add Object to it, when I retrieve the element, I'll get ClassCastException. Generics is supposed to save you from ClassCastException, not let it happen.

 
Check your pockets for water buffalo. You might need to use this tiny ad until locate a water buffalo:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!