tags:

views:

251

answers:

12

I've been using Java 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?

A: 

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.)

Stephen
A: 

Collections framework, Java Utility package esp. the RegExp parsing classes. Really depends on what you want to do!

Adil Butt
+3  A: 

Most of the classes that implement the Collection interface!

Agreed, the various data structures are pretty awesome! +1 for naming a specific one that you really like
Rafe Kettler
I would say the `Stack` class, Stack's are always useful.
LinkedList, because insertion at the front or end or in the middle is constant time, as opposed to ArrayList. It also has a Queue and List interface so you can use it as either.
Chris Dennett
+2  A: 

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.

Jim Tough
A: 

For me, lots of classes in java.nio package.

Pablo Santa Cruz
A: 

java.beans.Expression

EJP
A: 

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.

Rafe Kettler
A: 
  • 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.

YoK
+2  A: 

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.

apiri
InheritableThreadLocal seems pretty neat too. Might use this one myself in the near future for managing thread pool data relating to buffer allocation.http://download.oracle.com/docs/cd/E17409_01/javase/6/docs/api/java/lang/InheritableThreadLocal.html
Chris Dennett
A: 

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.

Ryan
+3  A: 

By definition, Object.

Joshua
+1 for wit and cleverness.
Rafe Kettler
+1  A: 

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.

BalusC