×

Packages Program in java

Let us know what package is in the Java programming language, and later we will learn about types of packages and how to define a package. How to import Java packages?

Package: A package can be defined in Java programming language as a container in which sub-packages, interfaces, also classes are contained. Packages are also can be called a folder in a file directory.

The main purpose of these packages in this object-oriented programming language, Java, is to avoid naming conflicts. These packages are also used for organizations. For organizing sub-classes, interfaces and the classes which are project-related packages are used in the Java programming language.

Simply, we can say a package is a group which contains sub-packages, interfaces, also classes.

The main advantages of these Java packages are to provide access protection, for removing or else we can also say for avoiding naming conflicts when we are writing a program and categorize sub-packages, interfaces, also classes.

Packages can also be defined as the mechanism. And this mechanism in Java is used for encapsulation the things like sub-packages, interfaces, also classes.

We can also consider packages in Java programming language as data hiding or else data encapsulation also. With the help of packages in Java, we can make a search or else we can locate the usage of classes, annotations and also enumerations faster and easier while working with the Java programming language. As mentioned, packages provide access protection, a protected member in the Java program can be accessed by the classes in the sub-classes or else the in the same package in which it is defined. If we do not provide any access specifier, then the member defined is called a default member, and these default members can be accessed by classes which are defined in the same package only. So, we can say packages provide default and also protected access specifiers in the Java programming language.

Packages Types in Java Programming Language

  1. Built-in Packages
  2. User-defined Packages

Built-in Packages: Built-in packages are the packages from the Java Application Programming Interface (API – It is a software intermediary which helps in the communication of two applications to talk to each other). And Java API is a library of pre-defined interfaces, classes and also of sub-packages in Java. We know that a large number of classes are a part of the Application Programming Interface of Java, and all these classes will be in built-in packages.

Java.io, Java.awt, Java.net, Java.util, Java.applet, Java.lang,swing, net, sql are some of the built-in packages in Java programming language.

User-defined Packages: As the name indicates, user-defined packages are the packages which are defined by the user or can be created by the user while writing a program. The programmer who is working on the packages can freely create packages freely and also on their own. Here, the user will first create a package and then create MyClass inside the directory.

Defining a Package in Java

For defining a package in the Java programming language, we use the keyword “package” before the “package name”.

Rules for Defining Packages

  • The first statement of the Java program which we are writing should be the package statement.
  • It must be a single word. It should contain multiple words.
  • It must and should follow camel case notation rules while defining the package names in the Java programming language.

The syntax for Defining a Package

Package packageName;

Let us understand the concept of packages in a Java programming language by seeing the below program.

package createdPackage;
public class void main(String[] args) {
      System.out.println(“The class which we defined it belongs the “createdPackage");
     }
}

For compiling the above code, use the below example syntax.

Javac -d. DefinedPackage.Java

So, the above command “Javac -d. DefinedPackage.Java” creates a directory with the package name “createdPackage”. Also creates “DefinedPackage.class”, which is saved into it.

Java createdPackage.DefinedPackage

The above command, "Java createdPackage.DefinedPackage” is the command which is used to run the program.

Importing the Packages

To import the Java packages, we use the keyword “import”. By using this keyword, we can import built-in packages and also user-defined packages.

syntax

import packageName.ClassName;

Below is the syntax for importing all classes at a time.

import packageName.*;

Let us take an example to understand the concept of importing the packages.

package packageCreated;
import Java.util.*;
public static void main(String[] args) {
scanner r = new Scanner(System.in);
int i = r.nextInt();
System.out.println(“We imported the package”);
}
}

Related Topics

Java Integer valueOf() method

The valueOf() method of Java Integer class returns an Integer object holding the specified int value. The second method returns an Integer object holding the specified String value. The third syntax returns...

2 minutes read.

Java InputStreamReader

What is InputStreamReader?An InputStreamReader is a converter between byte and character streams: It reads bytes and converts them to characters with the help of a charset. The charset it uses can...

4 minutes read.

Checked vs Unchecked Exceptions in Java

In this tutorial, we will discuss Java's checked and unchecked Exceptions. Exception An exception is an undesirable event that disrupts the normal flow of the program. An exception is thrown at runtime. It...

4 minutes read.

Java Math decrementExact() Method

The decrementExact() method of Math class returns the argument which is decremented by one, throwing an exception is the result overflows an int or long. Syntax: public static int decrementExact (int a) Parameters: The...

1 minute read.

Java String compareTo() Method

compareTo() method is used to compare the two specified Strings based on the alphabetical order(lexicographical order) of their characters.It returns positive number ,negative number or 0 Syntax: public int compareTo(String anotherString) Parameters: anotherString: the...

2 minutes read.

Java gc()

Garbage collection Java language provides different ways to perform the task of memory management. In Java, objects are declared and assigned references. Once lifeof an object is completed it is dereferenced...

4 minutes read.

Difference between Constructor and Method in Java

What is Constructor? In Constructor, we will discuss constructors and also will discuss default constructors and finally, we will discuss overloading constructors. A constructor is a method that is used to...

13 minutes read.

Java Console

If there is a character-based console device connected to the active Java virtual machine, it can be accessed using methods provided by the Java.io.Console class. JDK 6 adds the Console...

3 minutes read.

Java StringBuffer vs StringBuilder

Java StringBuffer vs StringBuilder StringBuffer The StringBuffer class is used to create mutable string. StringBuffer shows writable and growable character sequences. Java StringBuffer program FileName: StringBufferExample.java // A basic program that demonstrates the working...

2 minutes read.

Java Long Keyword

Long is a primitive data type in Java. To initilize variables, we implement the long keyword. It can also be applied to techniques. A 64-bit two's complement integer can put...

3 minutes read.

Round Robin Scheduling Program in Java

A CPU scheduling technique is known as Round Robin (RR). Additionally, network schedulers employ it. It was created specifically for a time-sharing system. The temporal slicing scheduling algorithm is another...

4 minutes read.

Print Matrix Diagonally in Java

The aim is to print the elements of a matrix of size n*n in some kind of a diagonal pattern. Input : mat[3][3] = {{1, 2, 3},                      {4, 5, 6},                      {7,...

3 minutes read.

Java Code Coverage Tools

Code coverage testing is a crucial metric that gauges how thoroughly the program's source code has been tested. The market is flooded with Code Coverage Tools, making it difficult to...

7 minutes read.

Java Viva Questions and Answers

Question: Explain Java programming language. Answer: It is a high-level programming language. This programming language is based on the principles of object-oriented programming. Large-scale applications can be developed by using this...

16 minutes read.

Morris Traversal for Inorder in Java

Through Morris’s traversal, a tree is traversed without the aid of recursion or stacks. Based on the threaded binary tree, the Morris traversal is used. We perform internal modification throughout...

4 minutes read.

Java Exception Propagation

Java Exception Propagation When an exception is being thrown from the peak of the stack and not getting caught, it runs down the stack to the previous method, which is sitting...

3 minutes read.

Java Socket Programming

Java Socket Programming Socket programming is used for the communication between the applications, i.e., client and server running on different JRE may be connection-oriented or connectionless. The client program can be designed using the...

8 minutes read.

Java String join() method

Java String join() method returns a joined String with given delimiter Syntax: public static String join(CharSequence delimeter,charSequence...elements) public static String join(CharSequence delimeter,Iterable<?extens charSequence>elements) Parameters: delimiter: char value to be added with each element elements: char value...

1 minute read.

Minimum Difference Between Groups of Size Two in Java

There is given an array with various integers in it. The goal is to divide the elements into distinct groups, each of which has just two, so that the difference...

3 minutes read.

Java Pop

The array, linked list, stack, queue, and other data structures are supported by Java programming. The insertion, deletion, and element searching operations are available for every data structure. And Java...

4 minutes read.