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 . . .
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.
campbell@Queeg:~/java/generic$ javac B.java
Note: B.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
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.
Maybe, just maybe, back in 1995 when Gosling and team were writing Java, they ran out of time or money.
Pubudu Dissanayake wrote: . . . why java didn't correct this problem .
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.