views:

175

answers:

8

I am new to programming, I was wondering if there is a way to find the java classes I need for a particular thing other than asking people for them on forums? I have seen the APIs on sun website but there is no good search option to find the class that will work for me.

+1  A: 
  • Are you looking for java libraries? Try google search , e.g. database connection pool java
  • Are you looking for classes inside the core java libraries? Install an IDE and look for classes, e.g. in Eclipse try Ctrl-Shift-T and type names: LinkedList . Note that in Eclipse you can also use wildcards, such as _*List_ to get all classes with names ending in List;
  • Looking for a specific class inside a package? Try Sonatype's Maven repository, click advanced search, and in the new drop-down select class name.
Robert Munteanu
+1  A: 

It depeneds what you mean. E.g. if you want to know the location of an exact class like "Where was BufferedReader again?", I do a Google search on:

site:java.sun.com BufferedReader

If you want to know how to do a task, e.g. "How can I read passwords from the terminal without echoing?" (java.io.Console), sometimes you can only figure it out for the first time by asking for advice.

Matthew Flaschen
+2  A: 

Most of the times if it is in the core of the language ( that is not a 3rd party library ) I use the following link:

http://java.sun.com/javase/6/docs/api/allclasses-frame.html

Which list all the classes in Java SE.

To know if the class may work or not I try by pressing Ctrl-F and guess what class would be the appropiate.

Eventually with time, when you get used to the classes there are, the search will be easier.

For instance, let's say you want to use an array:

Ctrl+F Array yields:

Array 
Array 
ArrayBlockingQueue 
ArrayDeque 
ArrayIndexOutOfBoundsException 
ArrayList 
Arrays 
ArrayStoreException 
ArrayType 
ArrayType

I click on each one and see the documentation. Some of them make no sense, some of them are exactly what I need.

Sometimes it may not work very straight forward. Lets way how to create a Window:

Ctrl+F don't show JFrame which is the correct answer, but it is just matter of tunning your sense :)

I hope this helps.

OscarRyz
The Java api documentation is so much better than MSDN.
Joe Philllips
+1  A: 

I would recommend a book such as Java In A Nutshell which is an excellent reference for the Java SE API (note that it covers Java 5 only, but that should be OK for most needs)

toolkit
+1  A: 

The Java tutorials, http://java.sun.com/docs/books/tutorial/, and the Java API, http://java.sun.com/javase/6/docs/api/, are essential resources to find out how to do something and with what classes.

Beau Martínez
A: 

I agree that the tutorials are good and search engines in general are great (but they now all lead you back to SO). Besides the forums, one resource not to miss is the programmers around you. If you are part of a dev team or in a class, you can get the answers you need or you can collaborate on the search. It is often easier for a human to help you if you are not exactly sure you understand what it is you are looking for.

akf
+2  A: 

If you're new to Java (or programming), it's going to be important to learn how to read and understand the documentation in order to become self-reliant -- it's the first step to getting less dependent from other people, and in most cases, will improve your learning and programming speed.

The following are some tips and suggestions:

Get to know the Java API Specification.

At first, the Javadoc format of the API Specification seem a little bit underpowered without any search ability, but I've found that to be that being able to browse the documentation with a browser to be very powerful.

Since the Javadocs are just HTML files, they can be easily viewed and navigated in a browser, and bookmarking pages are easy as well. Also, the API Specification is available for download, so it not necessary to always go to the web to get information.

As the documentation is just web pages, using the Find feature of the browser can find information quickly in a page, and if that fails, using Google or your favorite search engine to find documentation is very fast and easy.

Google (or your favorite search engine) is your friend.

Need information on the StringBuilder class? Try the following query:

stringbuilder java 6

The first result will be the Java API Specification page for the StringBuilder class. The tip is to append the string "java 6" at the end of the query and that seems to bring the API Specification to the top most of the time.

Get comfortable with the structure of the API Specification.

The presentation structure of the Javadoc tool is very nice and logical:

+-----------+-------------------+
|           |                   |
|  Packages |                   |
|           |                   |
+-----------+      Details      |
|           |                   |
|  Classes  |                   |
|           |                   |
+-----------+-------------------+

The Javadoc is partitioned into three frames.

  • Top-left frame: Listing of all the packages of the Java platform API.
  • Bottom-left frame: Listing of all the classes in the selected package.
  • Right frame: Details of the currently selected package or class.

Generally, looking for a class starts from selecting a package from the top-left hand frame, then looking for the class in the bottom-left frame, and finally browsing through the details on the right hand frame. The idea is:

Package -> Class -> Details

The details in the right hand frame for the classes and packages are also very structured. For classes, the structure are as follows:

  • Class name
  • Inheritance hierarchy
  • Implemented interfaces
  • Related classes
  • Information on and description of the class; usage examples.
  • Field summary
  • Constructor summary
  • Method summary
  • Field/Constructor/Method details.

When I'm looking for information on a method, I will scroll down to the Method Summary, find the method I want, and click on that method name which will jump to the Method Details.

Also, everything about the class is documented in one page -- it may seem like it is too much information for one page, but it does make it easy to get all the information you need about the class from one location. In other documentation formats, it may be necessary to flip between multiple pages to get information about several methods in the same class. With the Javadoc format, it's all in one page.

Learn the Java Platform API

The next step is to learn the important packages of the Java Platform API. This will aid in quickly finding classes from the huge collection of classes in the platform API.

Don't worry, take it slow -- you don't need to learn everything at once!

Some packages of interest are:

  • java.lang -- Contains the classes such as String, System, Integer, and other that are automatically imported in every Java program.

  • java.util -- Contains the Java collections (such as List, Set, Map and their implementations), Date, Random and other utilitarian classes. This is probably the first package to go to in order to find classes that will be useful in performing common operations.

  • java.io -- Classes concerning with input and output, such as File, Readers and Writers and the various Streams.

  • java.awt and javax.swing -- For graphical user interfaces, Java uses Abstract Window Toolkit or Swing, and those two packages are used to deal with GUIs in Java.

Knowing where classes are and starting to get a feel of where to look in the vast amount of documentation will make it easier to find information. It will probably take a while to get used to it, but once you get a hang of it, it should be much easier, and you'll be able to find the information on your own.

Good luck!

coobird
now that's an answer.
Beau Martínez
+1  A: 

I like to use DocJar.

There is even a plugin for eclipse but I have not tried it yet.

http://eclipse-plugins.2y.net/eclipse/plugin_details.jsp?id=959
MKam