9

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?

Rafe Kettler
  • 75,757
  • 21
  • 156
  • 151
  • http://stackoverflow.com/questions/15496/hidden-features-of-java – Brian Jul 25 '10 at 03:41
  • @Brian: that's similar and equally interesting, though most of it has to do with syntax quirks etc. rather than standard classes – Rafe Kettler Jul 25 '10 at 03:59
  • This is a community wiki question, so why is it closed as subjective? – Rafe Kettler Jul 25 '10 at 05:39
  • 2
    CW doesn't mean this question fits with SO as a Q&A site, "best of" questions typically don't. See [your own question on meta](http://meta.stackexchange.com/questions/58380/why-is-my-community-wiki-topic-closed-closed). – Pascal Thivent Jul 26 '10 at 04:52

12 Answers12

25

By definition, Object.

Joshua
  • 40,822
  • 8
  • 72
  • 132
6

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
  • 1,082,665
  • 372
  • 3,610
  • 3,555
4

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 Jul 25 '10 at 00:16
  • 1
    I would say the `Stack` class, Stack's are always useful. –  Jul 25 '10 at 00:19
  • 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 Jul 25 '10 at 04:32
3

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
  • 14,843
  • 23
  • 75
  • 96
2

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
  • 1,635
  • 9
  • 15
  • 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 Jul 25 '10 at 04:30
1

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
  • 6,027
  • 4
  • 37
  • 55
0

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

Adil Mehmood
  • 462
  • 3
  • 13
0

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

Pablo Santa Cruz
  • 176,835
  • 32
  • 241
  • 292
0

java.beans.Expression

user207421
  • 305,947
  • 44
  • 307
  • 483
0

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
  • 75,757
  • 21
  • 156
  • 151
0
  • 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
  • 14,329
  • 4
  • 49
  • 67
0

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,579
  • 9
  • 47
  • 59