• 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
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

Possible mistake on page 121 OCP study guide

 
Ranch Hand
Posts: 62
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As you can see in the picture taken of the page 121 from OCP Study Guide, I think there is a mistake because if we have a method whose definition is public static void addSound(List<Object> list) it can't receive a List<String> object because it's like:
List<Object> list = new ArrayList<String>() what does not compile.

Actually the third column says that it's not possible to pass a List<String> therefore it does not compile.
20190929_192022.jpg
[Thumbnail for 20190929_192022.jpg]
 
Master Rancher
Posts: 260
12
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The way I read it is that the second column confirms whether or not the code in column one compiles. When you declare the following method:
it compiles perfectly. Therefore column two is correct.

Now, when you try to use this method and you would want to pass List<String> to the method, then indeed the compiler will be upset.
 
Marshal
Posts: 65806
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brecht is correct. Because a List<String> is not a subtype of a List<Object>, you have incorrect types here and the compiler will only allow the code to compile if it is certain there will be type errors. But a String IS‑AN Object, so you can add a String (in fact any type) to a List<Object>.
A List<String> is however a subtype of a List<? super String>. You can therefore pass that List<String> as a parameter to the addSound() method.
 
passwords must contain 14 characters, a number, punctuation, a small bird, a bit of cheese and a tiny ad.
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!