Java 6: Java Packages and API in programming

JAVA

Java Packages & API

In Java, a package is group related classes or a folder in a path (file directory).  Generally, packages are used to avoid name conflicts and to write a better supportable code.

Packages are divided into two categories:

  1. User-defined Packages (create your own packages)
  2. Built-in Packages (packages from the Java API)

Built-in Packages

The Java API is a library of classes free to use in the Java Development Environment. The library contains components for input, database, programming and so on.

For more details, application programming interface (API) is a list of all classes. These classes are part of the Java development kit (JDK). It includes all Java packages, classes, and interfaces, with their methods, fields, and constructors. These are pre-written classes provide a great amount of functionality to a programmer. A programmer should be aware of these classes and should know how to use them.

Some examples of Java API can be found at Oracle’s website: http://docs.oracle.com/javase/7/docs/api/.

In order to use a class from Java API, one needs to include an import statement at the start of the program.

The API library is divided into packages and classes either import a single or a whole package. When import single package contain class along with its methods and attributes. Also, for a whole package import, contain all the classes that belong to the definite package. To use a class or a package from the library, you need to use the import keyword such as Syntax

import package.name.Class;   //for  import a single class

import package.name.*;   //for import the whole package

Import a Class

When want to use a class, for sample, Scanner class, which is used to get user input, example code:
Example

import java.util.Scanner;

In the example above, java.util is a package, while Scanner is a class of the java.util package. In order to use the Scanner class, create an object of the class and use any of the available methods found in the Scanner class documentation.

What is util?

In Java Package java.util Contains the collections framework, legacy of the collection of various classes, event model, date and time facilities, internationalization, and miscellaneous utility classes,  for example, a string tokenizer, a random-number generator, and a bit array.

In our example, we will use the nextLine() method, which is used to read a complete line:

Example

Using the Scanner class to get user input:

import java.util.Scanner;

class Sclass {

  public static void main(String[] args) {

    Scanner sObj = new Scanner(System.in);

    System.out.println("Enter User name");

    String userName = sObj.nextLine();

    System.out.println("Username is: " + userName);

  }

}

OUTPUT

Enter username

hey

Username is: hey

Java Application Programming Interface (API)

For example, in order to use the Scanner class, which allows a program to accept input from the keyboard, one must include the following import statement:

import java.util.Scanner;

The above import statement allows the programmer to use any method listed in the

Scanner class. Another choice for including the import statement is the wildcard option

shown below:

import java.util.*;

This version of the import statement imports all the classes in the API’s java.util package and makes them available to the programmer. If check the API and look at the classes written in the java.util package, you will observe that it includes some of the classes that are used often, such as Arrays, ArrayList, Formatter, Random, and many others. Another Java package that has several commonly used classes is the java.lang package. This package includes classes that are fundamental to the design of Java language. The java.lang package is automatically imported in a Java program and does not need an explicit import statement.

Import a Package

There are many packages to choose from. In the previous example, we used the Scanner class from the java.util package. This package also contains date and time facilities, random-number generator and other utility classes. To import a whole package, end the sentence with an asterisk sign (*). The below example will import all the classes in the java.util package:

Example

import java.util.*;

User-defined Packages

To create own package, Java uses a file system directory to store them. Just like folders on a computer:

Example

└── root

└── mypack

└── MyPackClass.java

To create a package, use the package keyword:

MyPackClass.java

package mypack;

class MyPackClass {

  public static void main(String[] args) {

    System.out.println("Oh! This is my package !");

  }

}

Save the file as MyPackageClass.java, and compile it:

C:\Users\Pc Name>javac MyPackClass.java

Then compile the package:

C:\Users\ Pc Name>javac -d. MyPackClass.java

From this path create notepad to practice your java code until expert.

0 Comments

You may find interest following article

Chapter 4 Relational Algebra

Relational Algebra The part of mathematics in which letters and other general symbols are used to represent numbers and quantities in formula and equations. Ex: (x + y) · z = (x · z) + (y · z). The main application of relational algebra is providing a theoretical foundation for relational databases, particularly query languages for such databases. Relational algebra...

Chapter 3 Components of the Database System Environment

Components of the Database System Environment There are five major components in the database system environment and their interrelationships are. Hardware Software Data Users Procedures Hardware:  The hardware is the actual computer system used for keeping and accessing the database. Conventional DBMS hardware consists of secondary storage devices, usually...

Chapter 2: Database Languages and their information

Database Languages A DBMS must provide appropriate languages and interfaces for each category of users to express database queries and updates. Database Languages are used to create and maintain database on computer. There are large numbers of database languages like Oracle, MySQL, MS Access, dBase, FoxPro etc. Database Languages: Refers to the languages used to...