r/learnjava • • 12d 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

•

u/AutoModerator 12d ago

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions

  • You include any and all error messages in full - best also formatted as code block

  • You ask clear questions

  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit/markdown editor: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

13

u/Shiva_Kar 12d 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 10d ago

Great explanation, sir.

2

u/istarian 9d 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

5

u/starraven 12d ago

Library uses api. Library is a bunch of code bundled. API is how you use the library.

3

u/FreeLogicGate 12d ago

A library is code created with the intention for it to be used in other programs. Often the motivation to separate out those routines, is the belief or intention of the developer, that the code in the library will be reusable across many programs or projects.

An API is the public interface to the library. With Java being an OO language, many "libraries" consist of class definitions, the "API" would be the java interfaces and public methods.

As a developer using a library, you should only ever need to use the library API.

3

u/gekigangerii 11d ago

Let's say a library is the package that you want to use.

An API is the contract on how the user is expected to use the library, leaving the internal implementation inacessible.

1

u/istarian 9d ago

In principle, you (as a developer) should aim to avoid exposing the internal implementation so that the only dependency is on the API.

Otherwise, other programmers will come to rely on access to the internals and any change you make will break somebody's software that uses that library.

1

u/oldDotredditisbetter 11d ago

what do you know about libraries and API so far?