• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Problem with Generics

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Guys ,

I have come across a doubt about Generics that's really twist my mind . Generics offer not real runtime protection I figure out following problem on generics



Please someone tell me why Generics fail this time because this thing will course major issues one day need help......

Thank You
pubudu
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Please someone tell me why Generics fail this time because this thing will course major issues one day


It's not going to bring any major issues, as it's Java has always been working, long before generics came on the scene. Any recent IDE should warn you to use a generic list instead. If you're worried about it, use "public List m(List<Integer> list)" instead.
 
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For generics to work as expected both the reference and the object should be typed.
Change the m() method to

And you will get a compilation error at the line list.add("Abc")
 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java allows this to use existing code(prior to java 1.5) with generics without any changes to the existing code.
 
Sheriff
Posts: 22850
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Every good compiler / IDE should give you a warning about using raw types, and this is exactly the reason.
 
Marshal
Posts: 80756
487
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Prime wrote:Every good compiler / IDE should give you a warning . . .

Like this

campbell@Queeg:~/java/generic$ javac B.java
Note: B.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
campbell@Queeg:~/java/generic$

If you look in books like Effective Java by Bloch, you find that generics guarantees to avoid that sort of error provided there are no compiler warnings.
 
Rob Spoor
Sheriff
Posts: 22850
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And with -Xlint:unchecked:
 
Pubudu Dissanayake
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you guys .... In .net they have compile and runtime protection when you using generics but java only has runtime protection . True but you can't avoid that kind of errors and why java didn't correct this problem .
 
Rob Spoor
Sheriff
Posts: 22850
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Pubudu Dissanayake wrote: Thank you guys .... In .net they have compile and runtime protection when you using generics but java only has runtime protection .


Java has no runtime protection, as all generics information is removed (search for type erasure for more info). It has compile time protection unless you use raw types (as in your example).

True but you can't avoid that kind of errors and why java didn't correct this problem .


Because Java had 5 previous versions (1.0 through 1.4) and code written using those versions shouldn't be invalidated because of new language constructs. .NET (or better C#) was written from scratch with generics built in from the very first moment. There was no need to maintain backwards compatibility because there simply was nothing older. Java could have implemented generics better* but then a lot of old code would no longer work on the new JVMs.


* I've written a post quite recently about extending generics with constructor requirements, like requiring the generic type to have a constructor that takes an int. It would be a nice feature but because of type erasure will not be part of Java ever. It can be found in this thread.
 
Campbell Ritchie
Marshal
Posts: 80756
487
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Pubudu Dissanayake wrote: . . . why java didn't correct this problem .

Maybe, just maybe, back in 1995 when Gosling and team were writing Java, they ran out of time or money.
Maybe, just maybe, whoever wrote C# learned from the shortcomings of Java.

And, as Rob has said, enforcing type-safety would have made 8 years' worth of coding useless.
 
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes that correct generics is included in jdk1.5 but before that java is having a various version and millions of code is written in those version.
so it will make all those code useless.
 
Pubudu Dissanayake
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
True guys so true backwards compatibility doing a important role in java without backwards compatibility there's no such a thing called java
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic