×

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

Process and Thread in Java

Process It is a program that is running on your computer. The process is a heavy-weight, which takes memory separately to other processes. The process may be a small background task like spell-checker; it...

11 minutes read.

Java Iterator

When iterating through, traversing, or retrieving the individual elements of a Collection or Flow object, a Java Cursor is leveraged as an iterator. In Java, there exist three types of...

4 minutes read.

Difference between = = and equals ( ) in java

Java : Java is a pure object oriented language. It was introduced by James Gosling in the year 1995. The first public implementation of java was done by sun micro systems...

6 minutes read.

Java Externalization

What is Externalization in Java? Externalization is a concept that is advanced to serialization. Serialization is a concept where we can transfer an object from our JVM ( Java virtual machine...

3 minutes read.

Multiple Inheritance Programs in Java

A component of the object-oriented notion known as multiple inheritances allows a class to inherit properties from multiple parent classes. When methods that have the same signature are present in...

4 minutes read.

Zigzag Traversal of Binary Tree in Java

In this article, you will be acknowledged about the zigzag traversal of binary tree in java and the approaches or ways in which the zigzag traversal can be done. Zigzag Traversal A...

10 minutes read.

Static class in Java

In Java, a class is a file containing the Java byte code. It can essentially specifically be executed on the JVM (Java Virtual Machine), fairly significant. The JVM is mostly...

7 minutes read.

How to Read CSV Files in Java?

Comma-Separated Values is the acronym for this format. It is a straightforward file format storing textual tabular data in a database or spreadsheet. The CSV files can be imported into...

3 minutes read.

Java Integer numberOfTrailingZeros() method

The numberOfTrailingZeros()  method of Java Integer class returns the total number of zero bits following the lowest-order one-bit in the 2’s complement binary representation of the specified int value. Syntax public static...

1 minute read.

Java Math incrementExact() Method

The incrementExact() method of Math class returns the argument incremented by one, throwing an exception if the result overflows an int or a long. Syntax: public static int incrementExact (int a)public static...

1 minute read.

Java Math nextUp() Method

The nextUp() method of Math class returns the floating-point number adjacent to the argument in direction of the positive infinity. Syntax: public static double nextUp (double d)public static float nextUp (float f) Parameters: The...

2 minutes read.

Java protected vs private

Java : Java is a pure object oriented language. It was introduced by James Gosling in the year 1995. The first public implementation of java was done by sun micro systems...

3 minutes read.

Tetranacci Number in Java

This article mainly describes tetranacci number identification and the Java Program for Tetranacci numbers. Tetranacci number Tetranacci numbers and Fibonacci numbers are related. The key contrast is that a Tetranacci number depends...

3 minutes read.

Java Program to Print Permutations of String

A string is given and you need to print all the possible ways for that string. Permutation is arranging the characters of a string to get outputs from the given...

3 minutes read.

Java Boolean booleanValue() method

The booleanValue() method of Java Boolean class returns a Boolean value for the specified Boolean argument. Syntax public Boolean booleanValue() Parameters NA Return Value This method returns the primitive value of specified Boolean object. Example 1 public class...

2 minutes read.

Why main method is static in Java

The method serves as the entry point for Java programmes or simply the point from which the programme begins to run. As a result, it is one of the most...

4 minutes read.

Console Errors in Java

An unlawful motion taken through the person that reasons this system to act abnormally is amistake until this system is compiled or run; maximum programming mistakes pass unnoticed.The software is...

3 minutes read.

Difference between String and Char Array in Java

We are heading to examine some significant differences between String and Character arrays. Both char arrays and String hold the series of characters and are utilised as a cluster of...

3 minutes read.

Java String charAt() method

It returns the char value present in the string at the specified index. Here, index value can not be greater than length() -1. Syntax: public char charAt (int index) Parmeters It accepts only...

3 minutes read.

Java String indexOf() method

Java String indexOf() method returns index of a given character or substring present in a String. Methods Description int indexOf(int ch)     It returns index position for the given char value.int indexOf(int ch,...

2 minutes read.