• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Generics and Legacy code

 
Ranch Hand
Posts: 186
Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I thought : "Here an ArrayList collection of parameter type java.lang.Object will be created, and we can not restrict it to <Integer> parameter-type only". That's why it would give compile time error. But it is not giving actually .Why so...?

Please let me know whats wrong with my thought..,
And how it is different from this:
 
Ranch Hand
Posts: 247
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


The above "myList" List type reference variable refers to only the "List of Integer Objects"... But here it is made to refer to the list which can contain any Object... So, it ends up in an error...



Here, this "myList" reference variable can refer to any "List of Objects"... Its is permissible... Latter case depicts the concept of Polymorphism... myList reference variable can refer to any list of objects....

Former case is restricted with Generic to refer to Integer list... Suppose it was allowed, this would have led to chaotic situation...
 
swaraj gupta
Ranch Hand
Posts: 186
Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ram Narayan.M
The above "myList" List type reference variable refers to only the "List of Integer Objects"... But here it is made to refer to the list which can contain any Object... So, it ends up in an error...



Hello Ram, it doesn't end up with an error, it's is working fine. But why so, please let me know the reason..?
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Swaraj,

Key objective of generics in java is to achieve compile time type safety.

Case I:
Case II:

Q1: Why above two cases compiled successfully with unsafe warnings?
In case I you try to tell the compiler that you are creating a type safe reference to an unsafe ArrayList object.
In case II you try to tell the compiler that you are creating an unsafe reference to a type safe ArrayList object.

Because of the type un-safety, both cases does not cause the compilation to be failed and you will see the successful compilation, but with warnings.

Q2: What is the difference in Case I and Case II?
Since we are talking about compile time only as I mentioned initially , compiler is unable to see the actual objects. So the compiler make decisions using the object references not using the actual objects in the heap.

So you cannot do the following invocation without compile error in Case I

But you can do the same invocation in Case II.

That is

Also note that after everything type-erasure erases all generic declarations at runtime

Regards,
Kushan
 
swaraj gupta
Ranch Hand
Posts: 186
Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kushan,
thanks for the nice explanation !!

I thought by creating a type safe reference to a type-unsafe ArrayList object, we are narrowing the object types that can be really added to it. And that's why the statement should result in compile time error. But against my thought it didn't actually.
What do you say about that..?
 
Kushan Athukorala
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Swaraj,

As you see in the Case I, we are narrowing the object types that can be added to the unsafe list instance.

But my question is, what is the reason the compilation should to be failed if you are not using the list in your code. In Case I, compiler waits till you do some incorrect operation(list1.add("some string")) on the list, but if you do correct operations(list1.add(10)) it will accept as it is.

Thus the compiler does the best to us without giving unwanted failures

Regards,
Kushan
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kushan Athukorala wrote:Thus the compiler does the best to us without giving unwanted failures



And the best is that it gives you a Warning
I have seen legacy code (Pre Java5) which when compiled gives numerous warnings- All related to the Generics. So its always better to treat these warnings with more attention.
 
reply
    Bookmark Topic Watch Topic
  • New Topic