package in Java is a way to organize related functionality in single place. If you look on File System package in Java represent a directory where Java source file is stored before compilation and class files are stored after compilation. For example if you create a class HelloWorld in a package called com.java67.welcome; than it will reside under directory com/java67/welcome in source tree and you can view that in your IDE like Eclipse or Netbeans or even by navigating to file system. once you compile your Java program either by using your IDE, Ant build Script or maven compile plugin; it creates class files under same package structure. For example maven will create target/classes/com/java67/welcome directory and place HelloWorld.class inside that. Its mandatory that class files reside on same package or directory as declared in there source file using package keyword, failure to do will result in java.lang.NoClassDefFoundError in Java. In summary answer of question What is package in Java can be as simple as that package is a keyword in Java which is used to specify directory structure for particular class file.
How to create package in JavaIf you are using IDE like Eclipse for developing your Java program than you don't need to do anything. Just click on new-->package and Eclipse will ask you name of the package, put name of the package and you are done. Now if you want to create Java class on that package, just select the package in package explorer and create new-->Java Class. If you are not using any IDE than you manually need to create directories corresponding to package in Java.
Predefined package in Java
All the java classes are structured in to different package based upon there functionality. for example java.lang package contains classes which are essential to Java programming language e.g. Thread, Exception Error, Object etc. On the other hand package like java.util contains all utility classes e.g. Collection classes, Scanner and other utility. java.io package contains Java classes related to Input and Output functionality. java.util.concurrent also called sub package of java.util contains Concurrent utility classes like CountDownLatch, CyclicBarrier, Semaphore etc
Why use package in Java
Encapsulation in Java program. default access modifier for any variable or class is package-private i.e. they are only visible into package, on which they are declared. By using package you Encapsulate whole functionality which allows you to change the functionality, include new functionality or just change the implementation without breaking whole application. Though package is not the highest degree of Encapsulation in Java which is achieved using private keyword, it still second best option and must to encapsulate whole functionality rather than just a class.
How to use package in Java
Using package in Java is very simple. just use package keyword along with name of package at top of your Java source file to declare package in Java. package declaration must be first line in Java source file even before import statement. here is an example of how to use package in java
That's all on What is package in Java, Why should we use package in Java and how to use package in Java. Simple example of package as shown in above Java program is enough to make you going and explores how Java source file and class file organize into package.
Other Java tutorials from java67 blog
Difference between TreeSet and HashSet in Java