I've been using Ja开发者_StackOverflow中文版va for a year or so now, and I constantly find myself discovering new things in the language. Most of this cool stuff, interestingly enough, doesn't come from 3rd-party APIs or libraries, but rather from the classes that ship in the JDK.
So I'm wondering, partly out of curiosity and partly for the education of others and myself, what classes that come in the JDK are the most interesting/useful/your favorite?
By definition, Object.
Since you specifically mentioned JDK, I think it's allowed to mention an API which actually isn't available in the JRE and is also less known among most of us: javax.tools
.
Here's a full demo snippet:
package test;
import java.io.File;
import java.io.FileWriter;
import java.io.Writer;
import java.net.URL;
import java.net.URLClassLoader;
import javax.tools.JavaCompiler;
import javax.tools.ToolProvider;
public class Test {
public static void main(String[] args) throws Exception {
// Prepare source somehow.
String source = "public class Test { static { System.out.println(\"test\"); } }";
// Save source in .java file.
File root = new File("/test");
File sourceFile = new File(root, "Test.java");
Writer writer = new FileWriter(sourceFile);
writer.write(source);
writer.close();
// Compile source file.
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
compiler.run(null, null, null, sourceFile.getPath());
// Load compiled class.
URLClassLoader classLoader = URLClassLoader.newInstance(new URL[] { root.toURI().toURL() });
Class<?> cls = Class.forName("Test", true, classLoader); // Prints "test".
}
}
Useful? Not sure. Interesting? Yes, interesting to know :)
For the remnant, I like the Collections, Reflection, Concurrent and JDBC API's. All of which are already mentioned before here.
Most of the classes that implement the Collection
interface!
One thing that I have seen new-to-Java people miss is the SimpleDateFormat class. I found a bunch of legacy code on my current project that was written by a C++ programmer who didn't really know Java and he basically did all of the date-to-string formatting with custom code. Talk about re-inventing the wheel.
I recently started using the zip/unzip support that is part of the stock JDK. It works great! I'm using it to create KMZ archives in a webapp.
ThreadLocal is probably near the top of the list. This class is the main way a lot of the magic happens in higher level frameworks, and if used properly, provides an interesting way of sharing references amongst a thread.
The Reflection package is also pretty powerful and a worthwhile tool to use in moderation.
Boring, but somehow it keeps coming back to the ole System
. Not for anything cool or exciting (as I said, boring), but for that one command I probably use more than anything else - System.out.println
(or just print, if that's more your kettle of fish.)
Collections framework, Java Utility package esp. the RegExp parsing classes. Really depends on what you want to do!
For me, lots of classes in java.nio
package.
java.beans.Expression
I'll weigh in on my own question. My personal favorite class is java.util.Random. I think it's incredibly useful for probability simulation, card games, and other little programs. I also find the idea of (pseudo)randomness fascinating.
- Base of all "Object" for equals() and toString() methods.
- Exception class. You can't do away with it.
- Collections API. Almost everything from Collections API is useful and used in your code.
- System class for System.out.println. But it is almost replaced by logger API.
- ThreadPoolExecutor if your product really uses threads and you really care about them being controlled :).
- JDBC API. Solely based on if we are talking about usage. But even this is going in background due to use of Hibernate (ORM tools)
Tried to list few useful ones, But I think list will really go long.
SwingWorker
is definitely a great class. It allows you to conveniently create worker threads in your Swing applications. This will return the final result of the processing and can even provide interim results to the EDT based on processing.
精彩评论