×

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

JDBC vs ODBC

Difference Between JDBC and ODBC ODBC: ODBC (Open Database Connectivity) is the accepted method for accessing databases among organisations and programmers. A database is linked to other programmes, such as word processors, spreadsheets,...

4 minutes read.

Sort Elements by Frequency in Java

To sort the elements in Java by using frequency, we need an input array. We should create a function that sorts the elements in an array by using their frequencies...

3 minutes read.

GCD of Different SubSequences in Java

The positive numbers are provided in an array called inArr. The aim is to determine the number of distinct GCDs (Greatest Common Divisors) in each subsequence present in the input...

4 minutes read.

How to get Day Name from Date in Java

We'll write a Java application to extract the day's name from the Date in this section. When dealing with Date and time in Java, the following classes are used. Class for Calendars:...

6 minutes read.

Practical Number in Java

In this tutorial, we will understand what is meant by practical numbers. We will understand it throughthe aid of examples and implementation in a java programming language. The practical numbers...

5 minutes read.

How to Convert Date to String in Java

How to Convert Date to String in Java We need to convert Date to String in Java may for displaying purpose. We can convert Date to String in Java using the format() method of java.text.DateFormat class. There...

2 minutes read.

Java String toUpperCase() methods

Java String toUpperCase() method is used to convert all the characters of the String into upper case. Syntax public String toUpperCase() public String toUpperCase(Locale locale) Returns It returns upper case String. Java String toUpperCase() Example 1: public...

1 minute read.

Java Integer class

The Integer class wraps a primitive int type value in an object. Its object contains only a single field whose type is int. Methods: The java.lang.Integer class provides several different methods for...

4 minutes read.

Memory Areas in Java

Let’s have a look at how memory management in Java works. We will be going to discuss how the objects get destroyed, the working of a garbage collector, and things...

5 minutes read.

JDK | Java Development Kit

Java Development Kit (JDK) The Java Development Kit is a software development environment used to create Java applications. The JDK includes JRE (Java Runtime Environment), an interpreter (java), a compiler (javac),...

3 minutes read.

Untouchable Number in Java

If a number N cannot be divided properly by any positive number, it is said to be an untouchable number. Additionally known as nonaliquot numbers. The sequence is A005114 from...

3 minutes read.

How to enable java in chrome

The Java module is huge for the Java Runtime Environment (JRE). It permits a program to work with the Java stage to run Java applets. Essentially, each of the undertakings connect...

3 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.

Rectangular Numbers in Java

In this tutorial, we will understand the meaning of a rectangular number in Java with the aid of examples, illustrations, and implementations. It is one of the popular coding interview...

3 minutes read.

How to Print array in Java?

A Java array is a data structure that allows us to hold components of the same data type. An array's items are kept in a single memory region. As a...

6 minutes read.

The Maximum Rectangular Area in a Histogram in Java

Continuous bars should be used to form the largest possible rectangle. We'll assume in the interest of convenience that each bar's width is 1. Naive Approach In this method, each bar will be...

6 minutes read.

Java String hashCode() method

Java String hashCode() method returns hash code for current String. hash code for string object is computed as s[0]*31^(n - 1) + s[1]*31^(n - 2) + ... + s[n - 1] Using int...

2 minutes read.

Permutation Coefficient in Java

In this tutorial, we will get familiar with the permutation coefficient in Java.  We will understand it through examples and see different approaches to solving the problem. A permutation is a...

5 minutes read.

Java time local date

Java: Java is one of the programming language which is object oriented. It consists of many features such as robust, simple, architecture neutral, dynamic, distributed, multi threaded, portable etc. The main feature...

3 minutes read.

Isomorphic String in Java

In this tutorial, we will understand what is meant by isomorphic String in java. We will also see a Java program to find out if the string is isomorphic or...

4 minutes read.