aspose file tools*
The moose likes Beginning Java and the fly likes Problem with Generics Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Problem with Generics " Watch "Problem with Generics " New topic
Author

Problem with Generics

Pubudu Dissanayake
Greenhorn

Joined: Jul 22, 2010
Posts: 7
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
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

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.


[My Blog]
All roads lead to JavaRanch
Soumyajit Hazra
Ranch Hand

Joined: Jun 26, 2007
Posts: 136
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")


Java Programmer | SCJP 1.5 | SCWCD 1.4
Srikanth Kumar
Ranch Hand

Joined: Jun 04, 2008
Posts: 36
Java allows this to use existing code(prior to java 1.5) with generics without any changes to the existing code.


Srikanth Kumar
SCJP 5, SCWCD 5, SCDJWS 5
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19762
    
  20

Every good compiler / IDE should give you a warning about using raw types, and this is exactly the reason.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39869
    
  28
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

Joined: Oct 27, 2005
Posts: 19762
    
  20

And with -Xlint:unchecked:
Pubudu Dissanayake
Greenhorn

Joined: Jul 22, 2010
Posts: 7
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

Joined: Oct 27, 2005
Posts: 19762
    
  20

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: 39869
    
  28
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.
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

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.


SCJP6.0,My blog Ranchers from Delhi
Pubudu Dissanayake
Greenhorn

Joined: Jul 22, 2010
Posts: 7
True guys so true backwards compatibility doing a important role in java without backwards compatibility there's no such a thing called java
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Problem with Generics