• 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
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Generics and Array conversion contradication

 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I am puzzled when I read about generics... for generics, it says that:

ArrayList<String> ls = new ArrayList<String>();
ArrayList<Object> lo ;

lo = ls; //this is illegal...

because if lo.add(obj) and ls.get(0) would return an object instead of string... I accept this argument...

But for Arrays...

String[] as = new String[10];
Object[] ao;

ao = as ; //this is legal!?

but if we assign objects to arrays, and get Strings using as... for example...

ao[1] = new Object();
String s = as[1]; //this will cause errors...

When Java is so ambiguous??

Kev
 
Ranch Hand
Posts: 220
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Kevin Lam:
Hi all,

I am puzzled when I read about generics... for generics, it says that:

ArrayList<String> ls = new ArrayList<String>();
ArrayList<Object> lo ;

lo = ls; //this is illegal...

because if lo.add(obj) and ls.get(0) would return an object instead of string... I accept this argument...

But for Arrays...

String[] as = new String[10];
Object[] ao;

ao = as ; //this is legal!?

but if we assign objects to arrays, and get Strings using as... for example...

ao[1] = new Object();
String s = as[1]; //this will cause errors...

When Java is so ambiguous??

Kev



???
Array<Object> is not a superclass of Array<String> hence it is straightaway invalid

Object is a superclass of String, therefore it is acceptable to assign a reference of String array to a reference of Object array

and what about


String s = as[1]; //this will cause errors...


that will not cause any errors unless you try to assign s= ao[1]; << not valid. if you're sure ao contains only strings, then you must
s= (String) ao[1]; else it will throw a ClassCastException at runtime
 
Kevin Lam
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thx for the reply,

You said that ArrayList<Object> is not a superclass of ArrayList<String>
You are right, but Object[] is not a superclass of String[] either...

I see collections and arrays as "Containers" of "things", therefore, my thinking is that, as long as the "elements" in the containers are legally convertable and the containers are the same type, it doesn't really matter...

So ArrayList<Object> and ArrayList<String> should have the same relationship as Object[] and String[]???
 
Did you just should on me? You should read this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic