• Post Reply Bookmark Topic Watch Topic
  • New Topic

source code or Java API  RSS feed

 
datta Nadgir
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI all,

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
 
Stefan Evans
Bartender
Posts: 1837
10
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Chris Barrett
Bartender
Posts: 321
24
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Stefan - both, as they can be used differently.

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.


 
Ivan Jozsef Balazs
Rancher
Posts: 999
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Besides, APIs are more stable than the implementations.

It is well conceivable for the implementation to change under the hood with the interface, with the API remaining the same.
 
Claude Moore
Ranch Hand
Posts: 923
9
IBM DB2 Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Most of times, using API reference is enough. Anyway, looking at core Java classes implementation I learnt a lot of useful programming techniques.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!