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......
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.
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.
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 .
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
Sheriff
Joined: Oct 13, 2005
Posts: 32830
4
posted
0
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.
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.