Library vs. Framework?

What is the difference between a Java Library and a framework? The two concepts are important but sometimes confusing for Java developers.

1. Key Difference and Definition of Library and Framework

The key difference between a library and a framework is "Inversion of Control". When you call a method from a library, you are in control. But with a framework, the control is inverted: the framework calls you.

framework-vs-library

A library is just a collection of class definitions. The reason behind is simply code reuse, i.e. get the code that has already been written by other developers. The classes and methods normally define specific operations in a domain specific area. For example, there are some libraries of mathematics which can let developer just call the function without redo the implementation of how an algorithm works.

In framework, all the control flow is already there, and there's a bunch of predefined white spots that you should fill out with your code. A framework is normally more complex. It defines a skeleton where the application defines its own features to fill out the skeleton. In this way, your code will be called by the framework when appropriately. The benefit is that developers do not need to worry about if a design is good or not, but just about implementing domain specific functions.

2. Their Relation

Both of them defined API, which is used for programmers to use. To put those together, we can think of a library as a certain function of an application, a framework as the skeleton of the application, and an API is connector to put those together. A typical development process normally starts with a framework, and fill out functions defined in libraries through API.

3. Examples

1. How to make a Java library?
2. How to design a framework?

Reference:
Martin Fowler

Category >> Framework Concepts  
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
<pre><code> 
String foo = "bar";
</code></pre>
  • Correct. The framework provides the standard boilerplate code but leaves hooks into which you can insert your application-specific code.

  • S Itzik

    And I am the one who need to “fill” the specific code in the subclass right?

  • While a framework may contain a library of functions which you can call in your code, it also has components which call your code. This is usually done with the Template Method Pattern where the skeleton of an algorithm with default methods is defined in an abstract class and application specific code is defined in a subclass.

  • kalaivanan S

    This graph is being made using Draw.io

  • Thanks

  • S Itzik

    I haven’t understand the difference yet. After all, the framework functions will be called by us this way or another…so what is the difference, whether I write “f1()” (as in library) or “do that” (as in framework)..?

  • Giannis Mariettos

    Soooo…jQuery is a library, not Framework!

    (The biggest “Deal” of the web coding world finally SOLVED!!)

  • Siya Nkosi

    Thanks a million man

  • Satya Bhavsar

    Awesome man

  • swagger.json


    Sout("hello world");

  • Thanks this makes things a lot clearer

  • shellbye

    I know the “Inversion of Control” finially

  • India First

    Well thought…not all true.

  • pinky

    perfect…good clear and neat explanation… 🙂

  • Marian Paździoch

    What did you use to make that graph? It should be “Framework callS you”.