15

I know that an API is called a set of functions used to call something, and a library is a collection of classes, but what is actually API in package like java.lang? I can connect to a class like System without using any API here, so why we say as J2SE API rather than J2SE packages?

Charles
  • 50,943
  • 13
  • 104
  • 142
  • I have no clue what you are talking about. Can you restate the question? – dmeister Aug 24 '09 at 14:48
  • Your definition of API and Library are incorect. An API can contain classes, and a Library may not include any classes. – Binary Worrier Aug 24 '09 at 14:51
  • What's the reason for been downvoted with 2 points? –  Aug 24 '09 at 15:04
  • 1
    @Binary Worrier. If the assumptions in the question are incorrect, then it's all the more evident that someone ought to provide a clear explanation of what API and a library mean, and how they relate to each other... ;) – Jonik Aug 24 '09 at 15:18
  • 3
    I didn't downvote, but you got two potential reasons in the comments right up there ^^ – geowa4 Aug 24 '09 at 15:20
  • 1
    I believe that people use the term API so much rather than library because API design is such a significant part of what makes a library useful these days, and also normally the only part a programmer will see (if it's any good). You can write a clever library but no one will use it if the API's rubbish. – jjujuma Aug 24 '09 at 16:30

10 Answers10

21

Straight from Wikipedia:

In computer science, an application programming interface (API) is an interface that defines the ways by which an application program may request services from libraries

Java contains many libraries in those packages (Swing, etc.), and the API is the interface by which we request services (perform actions, etc.).

geowa4
  • 40,390
  • 17
  • 88
  • 107
19

The API (Application Programming Interface) is what a library looks like from the outside for a program that's using it. It's the "face" of a library to other programs. The API of a library is the set of publicly accessible classes, interfaces and methods in the library.

You wrote:

I can connect to a class like System without using any API here...

That's not true - class System is a public class in Java's standard library, so it's part of the API of the standard Java library. You are using the API of the standard Java library if you are using class System.

Why do you think you "are not using any API" when you use class System?

Jesper
  • 202,709
  • 46
  • 318
  • 350
  • "Why do you think you "are not using any API" when you use class System?" Maybe because he's USING Java standard LIBRARY, not API, by CALLING API of that LIBRARY. – Zbyszek Oct 14 '20 at 07:06
7

A quick rule of thumb just to start with: a library is a collection of java classes, usually but not necessarily located in a jar file, and all public methods of those classes form the API of that library.

(the cleaner the code, the more you can rely on this rule ;) )

Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268
4

API is a logical representation of non-empty collection of Java classes and interfaces (add annotations and enums).

Library (being JAR Java library) is a unit of deployment of one, many or part of API.

There is no one-to-one or many-to-one association between two in general since their concerns are orthogonal in nature: API is concerned with logic and functionality; library is concerned with deployment.

Excellent resource that covers these relationships is Robert Martin Principles of OOD - look for last 6 package principles.

topchef
  • 19,091
  • 9
  • 63
  • 102
4

API, Library, and Framework are related-so-confusing terms.

API: Abstraction of Library, offers abstract view of a library which will suggest what are in the library and how can we access them to make use of. It may also provide the classification and description about the functionality and relationship of the components implemented in the library.

Library: Set of actually implemented and ready to use components

Framework: It may be a part of library or some times may use more than one library to offer a particular category of services.

Source: My understanding after reading some books and net sources.

Venkataswamy
  • 1,019
  • 7
  • 7
1

Just to restate what I think others are saying more briefly:

A library is a set classes that are generally organized and packaged in some coherent manner to promote reuse.

An API is the way you access a library (or any set of classes).

Bill K
  • 62,186
  • 18
  • 105
  • 157
1

In object-oriented programming , a class library is a collection of prewritten class es or coded templates, any of which can be specified and used by a programmer when developing an application program. you could use a library in a variety of projects. A framework is a collection of patterns and libraries to help with building an application. An API is an interface for other programs to interact with your program without having direct access.

Kowsigan Atsayam
  • 446
  • 1
  • 9
  • 20
1

api is the set of classes that require for the development and library is part of api for ex.suppose you need the communication api but you need only serial communication so the set of serial communication classes is library and the set of communication classes is api

0

Api is a list of all classes that are part of the JDK .it all includes all Java Packages,classes and interface ,along with their method,field, and constructor.

A java package is a technique for organizing java classes into namespace similar to the modules of Modular,provide modular programming in java packages can be in compresses files called JAR files,allowing classes to be downloaded fast as groups rather than individually.

oshan
  • 1
  • 1
0

package contians limited number of classes and interfaces, where as an API contains "MORE" classes and interfaces.but it is not a java interface.

prasad
  • 11