"Wild Java", a beautiful Hype
Today, Sun has released a prototype of J2SE 1.5 new features. This plugin running on J2SE 1.4 provides several new features of J2SE 1.5, such as types of security enumerations, automatic packing / unpacking, enhanced for loops, etc., of course, the most attention JSR-14, Generic.
After trying these new features, I finally disappointed Java's pan. First, let's take a look at the classic usage of generic Java:
Import java.util. *;
Public class usegeneric
{
Public static void main (string [] args)
{
Vector
Vi.Add (New Integer (24));
vi.add (35);
For (Integer i: vi)
{
System.out.println (i);
}
}
}
The above code shows the three new features of generic containers, automatic packing and enhance for loops. Indeed, from the perspective of simplified code, these new features have a certain help - of course, the automatic packing should not be considered a meaningful feature, just because the two types of types of types inherent in Java will be int, char, etc. The native type is partially treated, so it has to be used as a remedy when introducing a generic container.
Compile the Class after compile the above code to get the following code:
Import Java.io.printStream;
Import java.util.vector;
Public class usegeneric
{
Public useGeneric ()
{
}
Public static void main (string args [])
{
Vector vector = new vector ();
Vector.Add (New Integer (24));
Vector.Add (Integer.Valueof (35));
Integer integer;
FOR (SimpleITOR SimpleIterator = Vector.iterator (); SimpleItemrator.hasnext (); System.out.Println (Integer)
Integer = (Integer) SimpleITerator.next ();
}
}
It can be seen that all new features are implemented on the basis of existing virtual machines, without any freshness. As Joshua Bloch said, it is only to write some code written by programmers to the compiler.
Subsequently I tried to implement some slightly high generic technology, such as Type Traits. I wrote the following code:
// general traits
Class Numtraits
{
Public void dosomething ()
{
System.out.println ("General Traits");
}
}
// Specialized Traits
Class Numtraits { Public void dosomething () { System.out.println ("Traits for Integer"); } } Unfortunately, this code cannot be compiled, the compiler prompts "Duplicate Class". Obviously, the compiler does not use the type parameters as part of the class name, so Traits is impossible. Of course, in the member method, it is possible to prepare a code similar to the para-specialization and even the partial-specialization, but the following code will prove that this thing is meaningless. Public Class Happy { PRIVATE T SUBJECT = New T (); Public { SUBJECT.BEHAPPY (); } Public static void main (string [] args) { Happy O1.happy (); Happy O2.happy (); } } There are two compilation errors here. First, "Private T Subject = New T ();" This statement cannot be compiled, maybe I have not found the correct way to instantiate the type parameters. More importantly, the compiler prompt "I can't find the HAPPY () method in java.lang.object. Since Java implements generics using the Wiping Method, all types of parameters (unless explicitly declared superclass or interface) will be wiped as Object, so the contract is still fully dependent on the object system. In other words, the skills similar to template specialization are not only universally efficient, but it is unable to rely on compiler as a compiler as C or even compiler. Typelist? SELECT template? Still don't think about it. 喏, this is the so-called "generic Java". There is no dynamic binding, no type-traits, no (true) template, and everything is still thrown to RTTI to complete. I am more willing to call it "java with some type-way", not "generic java". Wild java, a beautiful hype - if you have too much expectations to it.