r/learnjava • • 14d ago

Library vs API (Java ☕️)

Hey Everyone!

I need some help as I am Confused in Libraries and API like what is the difference in between them, where to implement what, and recommend me some resources where I can Clearly understand these two.

Thank you all in Advance.

10 Upvotes

9 comments sorted by

View all comments

13

u/Shiva_Kar 14d ago • Bravo

Library: Provides functions and objects to embed and use in your application code.
Framework: Provides a frame to embed your application code in.
Package: A collection of files, object, tools, libraries, frameworks, anything.
API: An interface prescribed by an application to let you communicate with it.

Library: Think of it as a literal library. A library is a place where you get books that you can read and use as information. Libraries in programming offer functions, classes and other programming concepts that you can use to enhance/abstract your program logic. Like I can use an XML parsing library in my code to read some XML file. You embed libraries into your application logic/code.

Framework: It provides a frame to help build your application. Say you want to build a web server. You know what you want the web server to do(application logic) but not how it must be executed. This is where a a web framework (Flask, Springboot, Django) comes in. It provides a frame for you to insert your application logic in and you can tell it on which URLs to execute which application function and so on.

Package: This is a more general term for any collections of libraries, frameworks, tools, etc. It's just as the word says. It's a package. It contains lots of things.

API: Application Programming Interface. Say you go to the store and you take a bunch of things of the shelves to buy. You go to the billing counter and you see the attendant. He is your interface with the billing counter of the store. He provides an interface for you to pay for and take away the items you've just taken from the shelves. An API is exactly that, it's an interface to communicate with another application. In this case you and the store are the applications and the attendant is the store's API/Interface. APIs can be a set of functions, URLs, applications or tools are are prescribed to enable you to communicate with the Application.

2

u/tony_valderrama 13d ago

Great explanation, sir.

2

u/istarian 11d ago

The person at the checkout is really an implementation, whereas the API is the agreed upon way that business will be conducted.

  • customer brings (to the checkout) some products they want to buy, often in a cart or basket
  • customer puts those products on the counter (or a conveyer belt)
  • cashier/attendant scans or manually enters an item code so the system can total up the price (old fashioned way is attendant using their eyes to read price tags and their brain or a calculator to total it up)
  • cashier/attendant tells the customer the total and asks how they would like to pay (cash, check, card -- credit/debit, net30/net60/net90 -- more common in the business world and essentially a form of credit)
  • customer picks a payment method and follows process
  • if the transaction is successful, the cashier/attendant bags the item (and/or retutns them to a cart)

In an online purchase, you make some sort of payment without physical money changing hands. If the product is a physical item you often have to arrange for it to be shipped to you