7

I was wondering that why in Java we have packages not libraries. I had a lot of discussion with my friends over this issue. And they were telling that we can consider packages as libraries. Since C++ already had the concept of libraries then why there was need to create a new name called Packages.

P.S.: Is it really worth to correct someone if he calls packages libraries.

bendaizer
  • 1,235
  • 9
  • 18
SIGSTP
  • 1,125
  • 2
  • 9
  • 21
  • 2
    *why there was need to create a new name called Packages.* Because the team of developers who created C++ were not part of the Java development team , perhaps !!! Though I guess packages and libraries are different things. – AllTooSir Jun 06 '13 at 12:31
  • 1
    In what way is the C++ way of packaging up code more library-like than Java? What do you even mean by library? – devrobf Jun 06 '13 at 12:32
  • Because it is a logical other view. Why has C# Namespaces and libraries? – Uwe Plonus Jun 06 '13 at 12:33
  • @the_New_Idiot: you may be right but i think it is not a logical explanation. – SIGSTP Jun 06 '13 at 12:35

5 Answers5

9

A package is a Java construct used to organise classes. They provide:

  • A namespace, in which to place classes
  • A level of access (package access), that allows classes in that package to share fields

That's it; what implementers do with this organizational feature is up to them.

A library is another organizational concept. However, the intention of a library is to provide a a well-defined interface to the outside world and an internal implementation to execute as needed.

Both are organizational ideas and libraries are often made up of packages (if implemented in Java). However, they have different intent and so it is sensible to give them different names. Often, a small library will fit in a single package, so using one to mean the other is probably not confusing. However, they are not the same.

Dancrumb
  • 26,597
  • 10
  • 74
  • 130
6

Packages are namespaces. There are uncountable "Util" classes, but you can uniquely identify them because of their fully qualified name that is the package + class name.

A library is a set of classes that concur to provide a feature/functionality.

namero999
  • 2,882
  • 18
  • 24
5

A package is not really a library. Closer is it if you consider a jar file as a separate library. Packages are usefull because they give a project a logical structure, like a directory hierarchy. Also Java maintains runtime information in the objects and the package path is a unique way to identify a particular class, as you can have multiple classes with the same name.

In C/C++ you only have limited support for runtime information, so the situation is different.

Devolus
  • 21,661
  • 13
  • 66
  • 113
3

In Java world, logically library is a set of classes usually under some common denominator package name e.g. org.apache.commons.io packed and distributed as JAR file. Sometimes library can consist of several JARs then it is often called e.g. framework, tool kit, platform ...

Of course common library definition also applies here.

Java package is form of namespace - folder organization and part of fully qualified name. Yep, really the class is identified by package + simple class name, except default package which is unnamed package. There is also logic behind it like package scope, importing.. but it's too much for this question.

Here is bigest repository of Java libraries, artifacts or whatever you name them: Maven repository.

gertas
  • 16,869
  • 1
  • 76
  • 58
2

Packages and libraries are different concepts.

Libraries in C/C++ are a collection of related functions/procedures. The library contains string manipulation functions. The library contains functions to deal with regular expressions, etc...

Packages, in Java, were designed to support object oriented design principles (C came before the advent of OO). A package in Java is a logical collection of related Classes that should be able to be reasoned about together in the same way you would reason about individual classes / modules.

Funkytown
  • 520
  • 1
  • 7
  • 16