Unfortunately, the returned types can't tell you that arr's generic types are String. It's a subtle difference, but it's important. Because arr is created at runtime the generic types are erased at runtime so you can't figure that out. This might not matter to the user of Generics, but let's say we wanted to create some fancy framework that used reflection to figure out fancy things about how the user declared an instance's concrete generic types. Let's say we wanted a generic factory to create an instance of MySpecialObject because that's the concrete generic type we declared for this instance.
Well Factory class can't interrogate itself to find out the concrete type declared for this instance because Java erased them. Net's generics you can do this because at runtime the object knows it's generic types because the compiler complied it into the binary. With erasures Java can't do this. I could say a few good things about generics, but that wasn't the question. I could complain that they're not available at run time and don't work with arrays, but that's been mentioned.
A big psychological annoyance: I occasionally get into situations where generics cannot be made to work. Arrays being the simplest example. And I cannot figure out whether generics can't do the job or whether I'm just stupid. I hate that. Something like generics ought to work always. Every other time I can't do what I want using Java-the-language, I know the problem is me, and I know if I just keep pushing, I'll get there eventually.
With generics, if I become too persistent, I can waste a lot of time. But the real problem is that generics add too much complexity for too little gain. In the simplest cases it can stop me from adding an apple to a list containing cars. But without generics this error would throw a ClassCastException really quick at run time with little time wasted.
If I add a car with a child seat with a child in it, do I need a compile-time warning that the list is only for cars with child seats that contain baby chimpanzees? A list of plain Object instances starts to look like a good idea.
Generic'ed code can have a lot of words and characters and take up a lot of extra space and take a lot longer to read. I can spend a lot of time getting all that extra code to work right.
When it does, I feel insanely clever. I've also wasted several hours or more of my own time and I have to wonder if anyone else is ever going to be able to figure out the code.
I'd like to be able to hand it off for maintenance to people who are either less smart than myself or who have less time to waste. On the other hand I got a bit wound up there and feel a need to provide some balance , it is nice when using simple collections and maps to have adds, puts, and gets checked when written, and it usually does not add much complexity to the code if someone else writes the collection or map.
And Java is better at this than C. It seems none of the C collections I want to use ever handle generics. I admit I have strange tastes in collections. Sign up to join this community. The best answers are voted up and rise to the top. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Learn more. The result of this balance is less bulky bytecode while maintaining the performance of not inferring those types at runtime.
It also means that generics perform as well at runtime as parameterized types, since these are effectively the same at runtime. Are there alternatives to type erasure?
Yes, reification is an alternative. I wrote about reification in this post. You could say this is a fault of the programmer, who is writing code with compiler warnings. Conceptually, it would be much nicer if a List of String was actually a List of String, and not just a List who is temporarily constrained to work with Strings. Just follow the compiler rules and everything will work great. It gets more complicated if you have external data sources, differing versions of external libraries, or service calls with serialization especially binary serialization.
Suppose we are writing a client gateway for a service with the following data response object. Suppose that the Login Team decides to move the evaluation logic to the client-side and expose hashed strings for the permissions. With the service change, you would think that if someone forgot to update their Client application, they would get a nice exception when deserializing the LoginResponse. List and List are incompatible. You cannot deserialize a String to a Long, and you cannot deserialize a List to List.
InvalidCastException: Unable to cast object of type 'System. String]' to type 'System. The above code will execute without an exception. This is a helper function to allow perfect forwarding of arguments taken as rvalue references to deduced types, preserving any potential move semantics involved. Type erasure enables Java applications that use generics to maintain binary compatibility with Java libraries and applications that were created before generics. However, with generics in C.
NET , there is no type erasure by the compiler, and the type checks are performed during runtime. Polymorphism is the ability of an object to take on many forms. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object.
In Java, all Java objects are polymorphic since any object will pass the IS-A test for their own type and for the class Object. Generics are a facility of generic programming that were added to the Java programming language in within version J2SE 5. Found poems take existing texts and refashion them, reorder them, and present them as poems. A pure found poem consists exclusively of outside texts: the words of the poem remain as they were found, with few additions or omissions.
Decisions of form, such as where to break a line, are left to the poet. In a nutshell, generics enable types classes and interfaces to be parameters when defining classes, interfaces and methods. A Java compiler applies strong type checking to generic code and issues errors if the code violates type safety. In short the underlying compiled classes are not actually generic. They compile down to Object and casts. In effect Java generics are a compile time artifact and can easily be subverted at runtime.
C on the other hand, by virtue of the CLR, implement generics all they way down to the byte code. The Generics version is considerably faster, see below.
0コメント