NewGuy
NewGuy

Reputation:

What is the difference between a Java API and a library?

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?

Upvotes: 16

Views: 38546

Answers (10)

Kowsigan Atsayam
Kowsigan Atsayam

Reputation: 453

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.

Upvotes: 1

oshan
oshan

Reputation: 1

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.

Upvotes: 0

Venkataswamy
Venkataswamy

Reputation: 1009

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.

Upvotes: 4

user1305866
user1305866

Reputation:

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

Upvotes: 1

topchef
topchef

Reputation: 19803

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.

Upvotes: 4

prasad
prasad

Reputation: 11

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

Upvotes: 0

Andreas Dolk
Andreas Dolk

Reputation: 114807

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 ;) )

Upvotes: 7

Bill K
Bill K

Reputation: 62789

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).

Upvotes: 1

Jesper
Jesper

Reputation: 206896

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?

Upvotes: 20

geowa4
geowa4

Reputation: 41833

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.).

Upvotes: 22

Related Questions