• 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
  • Ron McLeod
  • Junilu Lacar
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • Carey Brown
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • fred rosenberger
  • salvin francis

Sybex 816 - (Chapter 3, Kindle Page 156) var x2 = new ArrayList<>() not accepting List<Object>

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
List<?> x1= new ArrayList<>();
var x2= new ArrayList<>();

Here x1 is of type List and x2 of type ArrayList
In the book it is mentioned that, we can only assign x2 to a List<Object>.
This might be a potential error as x2 cannot accept List<Object>. Please confirm (x1 can be assigned though)
 
Ranch Foreman
Posts: 623
13
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I didn't find this exact example anywhere in the paper text for the 816.
I believe you slightly modified the example from right near the end of "Using the Diamond Operator" just before "Using Lists, Sets, Maps, and Queues"?

In general, you can not assign a reference variable to a more specific one without an explicit cast.

For example, you can not say:
Integer IOriginal = Integer.valueOf( 5 ); // fine so far
Number N = IOriginal;        // still good
Integer I = N;            // we know this is good but the compiler does not.

jshell> Integer IOriginal = Integer.valueOf( 5 );
IOriginal ==> 5

jshell> Number N = IOriginal;
N ==> 5

jshell> Integer I = N;
|  Error:
|  incompatible types: java.lang.Number cannot be converted to java.lang.Integer
|  Integer I = N;
|              ^

You may be having trouble with something that drove me nuts.

I have been programming for a zillion years, and I swear the way we used the word assign was always:

x = 5 ;   // "assign (the value) 5 to  (variable) x" or more usually for short, "assign 5 to x"
or rarely, but sometimes
x = 5;   // "assign x the value 5"

I have noticed more and more over the years people using English like this (including some major texts in the field by native English speakers)

x = 5;    // "assign x to 5"

I never would have said the sentence like that, in a million conversations and emails with like 10000 colleagues...

Here I am showing this with a constant, I find it even MORE confusing when both are variable references.
I swear that for years and years we always said things the first way, all the time, whatever our first languages (human and computer) were.

I had recently given up on this whole issue and just looked carefully as I read each book, watched each video, etc. to see how they phrase the sentence.

For the benefit of everyone who doesn't have the text at all or doesn't have the Kindle Edition, can you quote the exact spot in the Chapter, rather than just the page reference?

Language around assignment statements is obviously a trigger for me, it always feels like at some point we changed the default phrasing in plain English and nobody told me when I see the "Assign x to 5" idiom.
 
Marshal
Posts: 73252
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you tried the code with var on an IDE? Did it tell you the exact type of the reference? I am a bit suprised that it compiled; it may default to List<Object>.
 
author & internet detective
Posts: 40676
827
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sarath,
I think this example shows the point you are making:


The closest example I can find is in the print book on page 114 with var list = new ArrayList<>(); Which the following paragraph says creates ArrayList<Object>
 
Ranch Hand
Posts: 477
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sarath,

Welcome to The Ranch!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic