source code or Java API

I'm a Java Learner, My instructor said me to look at java source code of class (thread class) that is present in JDK, Its really good and I understand it clearly,, but he also suggests me to look at API. Which would be the best practice? to go for API or source code of class ?? THANKS IN ADVANCE

I choose option c: both of the above are applicable in different situations.

Looking at APIs tells you what classes/methods are available and what they are supposed to do. Often they are easier to read than the code. Well written javadoc contains examples. e.g. java.text.SimpleDateFormat
If you want to learn how to use a library, then read its API.

Looking at the source code can show you what well written code looks like and having an insight into what is going on "under the hood" can be useful. But isn't always necessary.

Personally, I would start with javadoc and then drill into the source code if I want to know more.

Thinking of a car, you as a driver use the car's API. You don't care how a brake pedal works. You simply need to know pressing the brake pedal will (hopefully) stop the car.
On the other hand, if you were building a car, you would take the brakes apart and figure out how things connect from the pedal to the brake pads, etc...
You would investigate the source code of the car and use that as a template, or structural design, for your own braking system.

There will be times when you want to understand how Java works, and other times where you just need to be able to use it.

datta Nadgir wrote:Which would be the best practice? to go for API or source code of class ??

I basically agree with Stefan, but I'd say that it's far more important to learn how to read an API.

Why? Because most of the time you are simply the user of a class, and the API (if it's well written) should tell you everything you need to know about how to use it.

Also: In the real world you simply don't have time to plough through the source code for every class you encounter - if it's even available.

Conclusion:
1. Try the API first.
2. If that doesn't provide enough information, have a look at the source.
3. Never, EVER, rely on what you read in the source, because it may change in the next release.

HIH

Winston

"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here