• Post Reply Bookmark Topic Watch Topic
  • New Topic

Quick Question on Array/ArrayList  RSS feed

 
Paul Clements
Ranch Hand
Posts: 99
1
Chrome Eclipse IDE MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm sure I know the answer to this, but if I do I've forgotten it and it's irritating me. I hate not knowing why things work/don't work! Have a look at the following code section:
My question is simply, why do Array and ArrayList treat the same thing i.e. assigning subclass object ref to superclass object ref differently? Why don't they both give compilation errors, or allow compilation and then give runtime?

Cheers,

PaulC.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Refer to https://docs.oracle.com/javase/tutorial/java/generics/inheritance.html -> List<Dog> is not a subtype of List<Animal> even though Dog is a subtype of Animal.

In the case of the array, that's just normal upcasting behavior. The compiler accepts it but the runtime system, which is less forgiving, double checks the actual types.
 
Paul Clements
Ranch Hand
Posts: 99
1
Chrome Eclipse IDE MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Junilu. I'll take a good look at that link.
 
bob john
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whats the point to write ?
If we can do .

The diffrence is that, if < klassName> then it even cant print these elements.
 
Campbell Ritchie
Marshal
Posts: 56534
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't understand that last question.
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bob john wrote:Whats the point to write ?
If we can do .

The diffrence is that, if < klassName> then it even cant print these elements.


Are you saying you can substitute:
?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I think what bob is saying is "why use klassName instead of String, because klassName doesn't work".

As an analogy, that is like a mechanic saying "why use the car instead of the bus, because the car doesn't work". Well, the answer is... it is the mechanic's job to fix the car!

So, it is the developer's job to fix the klassName class! ...

Henry

 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bob john wrote:The diffrence is that, if < klassName> then it even cant print these elements.

Printing the elements is not the only thing you can or want to do with things that are in a List. Besides, it's not true that you can't print the elements if your generic type is anything other than String. You can print out anything. If you want to print out something sensible, then you just need to make sure the class has a sensible override to its toString() method.
 
Campbell Ritchie
Marshal
Posts: 56534
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hope that isn't what it means, Liutauras. We do occasionally see people who write things like this:-... as if one can change a String to a number automatically. There are valid methods to convert a String to a number, and this might be the most robust way to do it:-But trying to assign a List<String> to  List<Integer> (remembering there is no such thing as a List<int>) would not be possible. You might be able to iterate the List and convert those Strings which represent numbers to a number type, or even use a Stream:-...but you can't simply turn a List<String> into a List<Integer>. Why not? Well, look at this List:-Now try getting a number out of line 5.

For those not familiar with the Java8 construct of Streams, let's have a but more detail.
Line 1: Assuming we have that List, all Lists (well, actually all Collections) have a stream() method, which (would you believe) returns a Stream. That will iterate all elements in the Collection, but is lazily executed, so nothing happens until the last link in the chain is ready to receive something. Since the List is a List<String>, the Stream will be a Stream<String>. The next line takes a risk of an Exception if any of the elements can't be handled as an int or boxed into an Integer.
In line 2 I am using the Stream#map() method to turn the Strings in the first Stream into Integers. You will find the map method takes a Function object as its parameter, but if you look at the Java™ Tutorials, you find you can pass a method reference instead. What I am saying is, “pass the elements of the old Stream to the valueOf() method of the Integer class and create a new Stream with the results, a Stream<Integer>. That will be a Stream<Integer>. Note that you would use a slightly different syntax for an instance method. Also that any elements of the source Stream which don't match an int or Integer will cause a NumberFormatException to be thrown.
Line 3 collects the elements of the second Stream; since that was a Stream<Integer>, it will create a List<Integer>. The collect() method takes a Collector as its parameter; in the case of something used commonly, you will find the Collectors class useful. It has various methods, and this method returns a Collector which turns your Stream<Integer> into a List<Integer>. I think the runtime type is the well‑known ArrayList.

sorry. I pushed the submit button too early (), so the last part of this pst appears as an edit.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!