In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "Java generic instance analysis" related knowledge, Xiaobian through the actual case to show you the operation process, the operation method is simple and fast, practical, I hope this "Java generic instance analysis" article can help you solve the problem.
First, let's look at the classic usage of generic Java:
import java.util.*;
public class UseGeneric
{
public static void main(String[] args)
{
Vectorvi = new Vector();
vi.add(new Integer(24));
vi.add(35);
for(Integer i : vi)
{
System.out.println(i);
}
}
}
The code above demonstrates three new features: generic containers, autoboxing, and enhanced for loops. Indeed, from a code simplification perspective, these new features help--of course, autoboxing shouldn't really be a meaningful feature, just because Java's inherent two typing systems treat int, char, and other primitive types differently from objects, so autoboxing had to be used as a remedy when introducing generic containers.
Decompile the compiled class above 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(SimpleIterator simpleiterator = vector.iterator(); simpleiterator.hasNext(); System.out.println(integer))
integer = (Integer)simpleiterator.next();
}
}
As you can see, all the new features are implemented on top of existing virtual machines, and there is nothing new about them. Indeed, as Joshua Bloch said, it was simply a matter of converting some code previously written by programmers into code written by compilers.
Then I tried to implement some slightly more advanced generic techniques, 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
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.