Curriculum
Course: Java Basic
Login

Curriculum

Java Basic

Java Home

0/1

Java Introduction

0/1

Java Get Started

0/1

Java Syntax

0/1

Java Comments

0/1

Java Type Casting

0/1

Java Operators

0/1

Java Booleans

0/1

Java Switch

0/1

Java Break / Continue

0/1

Java Errors and Exception

0/1
Text lesson

Java Packages/ API

Java Packages & API

In Java, packages serve to group related classes, akin to folders in a file directory. They are utilized to prevent name conflicts and enhance code maintainability. Packages are categorized into two types:

  • Built-in Packages: These are packages provided by the Java API.
  • User-defined Packages: These are packages created by the programmer.

Built-in Packages

      The Java API comprises a library of prewritten classes included in the Java Development Environment, available for unrestricted use.

The library is structured into packages and classes, offering the flexibility to import either individual classes with their methods and attributes or entire packages containing all classes associated with the specified package.

To utilize a class or a package from the library, you must employ the import keyword.

Syntax

import package.name.Class;   // Import a single class
import package.name.*;   // Import the whole package

Import a Class

When you identify a class you wish to use, such as the Scanner class for obtaining user input, you would write the following code:

Example

import java.util.Scanner;

In the provided example, java.util represents a package, whereas Scanner is a class within the java.util package.

To utilize the Scanner class, instantiate an object of the class and employ any of the methods detailed in the Scanner class documentation. In this instance, we’ll utilize the nextLine() method, designed to read an entire line of input:

Example

Utilizing the Scanner class for obtaining user input:

import java.util.Scanner;

class
MyClass {
public static void main(String[] args) {
Scanner myObj = new Scanner(System.in);
System.out.println("Enter username");
   String userName = myObj.nextLine(); 
System.out.println("Username is: " + userName);
  }
}

Import a Package

Numerous packages are available for selection. In the preceding example, we employed the Scanner class from the java.util package. This package also encompasses date and time functionalities, a random-number generator, and various other utility classes.

To import an entire package, conclude the statement with an asterisk sign ( * ). The subsequent example will import ALL the classes in the java.util package:

Example

import java.util.*;

User-defined Packages

To establish your own package, it’s essential to comprehend that Java utilizes a file system directory to store them, akin to folders on your computer.

Example

└── root
  └── mypack
    └── MyPackageClass.java

Use the package keyword to create a package :

MyPackageClass.java

package mypack;
class MyPackageClass {
public static void main(String[] args) {
System.out.println("This is my package!");
  }
}

Save the file as MyPackageClass.java and proceed with compiling it.

C:\Users\Your Name>javac MyPackageClass.java

Next, compile the package.

C:\Users\Your Name>javac -d . MyPackageClass.java

This compels the compiler to create the “mypack” package.

The -d option designates the directory where the class file will be saved. You can choose any directory name, such as c:/user for Windows. If you prefer to keep the package in the same directory, you can use a dot (.), as shown in the example above.

Note: To prevent conflicts with class names, the package name should be written in lowercase.

After compiling the package in the aforementioned example, a new folder titled “mypack” was generated.To execute the

MyPackageClass.java file, use the following command:

C:\Users\Your Name>java mypack.MyPackageClass

Upon execution, the output will be:

This is my package!