Skip to main content

Introduction to Java Programming

  What is Java? Java is a programming language and a platform. Java is a high-level, robust, object-oriented and secure programming language. Java was developed by Sun Microsystems (which is now the subsidiary of Oracle) in the year 1995. James Gosling is known as the father of Java. Firstly, it was called  Greentalk  and the file extension was  .gt . After that, it was called  Oak . Initially Java was designed for small, embedded systems in electronic appliances like set-top boxes. Platform:  Any hardware or software environment in which a program runs, is known as a platform. Since Java has a runtime environment (JRE) and API(Application Programming Interface), it is called a platform. The principles for creating Java programming were "Simple, Robust, Portable, Platform-independent, Secured, High performance, Multithreaded, Architecture neutral, Object-oriented, Interpreted, and Dynamic". Currently, Java is used in internet programming, mobile devices, g...

21. Packages in Java

 

Packages in Java

  • Packages in Java is a mechanism to encapsulate a group of classes, sub packages and interfaces.
  • We can reuse existing classes from the packages as many time as we need it in our program.
  • All we need to do is put related classes into packages.
  • After that, we can simply write an import class from existing packages and use it in our program.
  • Packages names and directory structure are closely related. For example, if a package name is DevBhoomi.BTech.CSE.Deepak, then there are three directories, DevBhoomi, BTech and CSE such that CSE is present in BTech, BTech is present in DevBhoomi and Deepak is the class name.

There are two types of packages in Java:

  1. In-Built packages
  2. User-defined packages

In-Built packages

These packages consists of a large number of classes which are a part of Java API (Application Programming Interface). Some of the commonly used built-in packages are:

  1. java.lang: Provides classes that are fundamentals to the design of the Java Programming Language. This package is automatically imported.
  2. java.io: Provides for system input and output through data streams, serialization and the file system.
  3. java.util: Contains the collections framework, a service loader, properties, random number generation, string parsing and scanning classes.
  4. java.applet: Provides the classes necessary to create an applet and the classes an applet uses to communicate with its applet context.
  5. java.awt: Contains all the classes for creating user interfaces and for painting graphics and images.
  6. java.net: Provides the classes for implementing networking applications.

User-defined packages

These are the packages that are defined by the user. To create a package, write package keyword at the top of program followed by the directory name, and then compile the program.

Creating a Java Package

package MyPackage; // Package name

public class Calculator {
    public void add(double... arr) {
        double sum = 0;
        for (double element : arr) {
            sum += element;
        }
        System.out.println("The sum of numbers is " + sum);
    }

    public double sub(double a, double b) {
        double diff = a - b;
        return diff;
    }

    public double mul(double... arr) {
        double pro = 0;
        for (double element : arr) {
            pro *= element;
        }
        return pro;
    }

    public void div(double a, double b) {
        System.out.println("The division of 10 by 3 is " + a / b);
    }
}

To Compile :

javac -d. Calculator.java

This will create a package of name MyPackage that will contain the class file of that program. Now, we can move the program to that folder.

Using a Java Package

import keyword is used to import packages in the java program. There are three ways to import a class in Java, let's try these ways for a Scanner class

  • Writing at top: import java.util.Scanner; This will import the Scanner class only.
  • Writing at top: import java.util.*; This will import all classes inside the util directory.
  • Importing while creating object or without using import keyword: java.util.Scanner object_name = new java.util.Scanner; This will also import the Scanner class.
import MyPackage.Calculator; // Importing the Calculator class inside MyPackage package

class Main {
    public static void main(String[] arr) {
        System.out.println("Performing some basic mathematical operations...");
        Calculator obj = new Calculator();
        obj.add(10, 20, 30, 40, 50);
        System.out.println("The difference of 20 and 10 is " + obj.sub(20, 10));
        System.out.println("The product of numbers is " + obj.mul(10, 2, 3, 4, 5));
        obj.div(10, 3);
    }
}

Output :

Performing some basic mathematical operations...
The sum of numbers is 150.0
The difference of 20 and 10 is 10.0
The product of numbers is 0.0
The division of 10 by 3 is 3.3333333333333335

Previous

Next

Comments