What is a package in Java? What’s the actual need for it?
In general, Packages means bundles or groups of items. the same applies in java too.
Imagine a public library has thousands of books we need to take particularly Head first java Version-2, what will happen if all the books are not properly in order. It is a critical situation, right?
Java comes with the facility to bundle the Classes, Interfaces and sub packages.Thus makes the way to categorises all the java files.
let us consider a real world example of library books management software, the purpose is to make it easier to access,So we are creating a package named public library , then we have list of sub packages for all the branches of the library, then we have list of sub packages of department of books list, then we have a list of Classes/Interfaces of all the books in each department.
This makes it easier to access the book we need on time. like the same in java we have thousands and thousands of Classes/Interfaces by packaging each based on the purpose of the classes.
for example java.util.ArrayList , here java is a standard package developed by sun microsystems developers. util (utility package )is a sub package and ArrayList is a class.
System.out.println() , here System refers to Class and out refers to instance or object and println is just a method.
Java allows users to create our own packages by the package keyword.we can able to create all the sub packages and classes we need.
we can use import statement to access the list of class in the package.we can also make a static import to access a method without mentioning the full name , that is the one of the coolest feature in java.
Imagine how we feel when we are called by the grandfather name followed by father name followed by our name, kind of weirdness right ?. so we can also call a method directly by its name by making a static import.
What Is The Actual Need For Packages.
In programming languages which invented before java was developed , for example c & c++ are invented before java was introduced to the world.
In c & c++ there is a #include statement which we often use for each and every program, At the low level what it does means it just copies all the lines of code which file we mentioned in the include statement from the c or c++ standard library and then only we can able to use the features of the package.
Let’s make it simple, whenever we try to print “hello world “ in the output console.We need to include the package name called stdio.h . That internally copies all the codes inside the file to our current file and then when we try to access the printf method that is written in the file, we are calling the method, as like we written the method in our file.
Imagine whenever we use the standard lib, lots and lots of lines of code copied and makes our application/file too heavy and also consumes enormous amount of processing power.
Finally our hero comes to rescue. Java is developed with the feature of packages to solve the problem of size of the program and processing time and resources.
In what way packaging concept helps us means, Like c & c++. Java also contains a standard library called JRE(comes with installation). It contains all the library files(class files) inside the package.
JRE is package in .jar format , when we need to access the feature of Java standard we use statement called import Packagename.Class name it actually checks the class name in the jre of that particular package name , as java standards does not allowed any duplicates of name in packages.
When it finds the class name inside a package it compiles it and by using bytecode we can access the classes and methods of the class and soon.
Thus packages in java are the main advantages that made java so popular.
Not only in the architecture of the java uses package. we can also create packages for our project and build it as a jar. jar only contains compiled bytecode for access, not to edit the java source file we can also restrict it.
To avoid naming collision we create packages in the format of reversing the domain name of yours for example- com.zoho.cliq inside we can create our class files, even though the class name is already used in another package.
That makes java really cool than the other programming languages.