Java package and library classes

Let’s get started with what are the packages and library classes available in Java.

Let us understand with a practical example.

Have you ever thought that the sentences you are writing or speaking – be it in English, Hindi or whichever language you use, on what basis you know you are writing or speaking correctly?

The answer undoubtedly will be: there are some predefined texts or a set of rules that have been prescribed and referred to for this task. All the vocabulary, sentence formations and other such rules of grammar are part of it.

Similarly in the world of computers, when you are writing any program: long or short, all the tokens used in it like (public, static, void, float, double) are part of certain predefined rules. These rules are called predefined classes.

For example, if you are writing a program to print the first ten natural numbers using Java, you will first declare and create a class by writing “ class <class_name>…….”.

The class you’ve created here is called user-defined class because it is created by you.

Similarly, there are some classes predefined in the Java System which provides support to programmers to build their code. These classes are called Library classes.

Some of the library classes are:

Library class name Function
java.io Input-Output functions
java.lang Character and String operations
java.awt Abstract Window Tool, Windows Interface functions
java.util Develops Utility Programs
java.applet Helps in developing Applets
java.net Network Communication functions
java.math Mathematical Functions

Packages:

Suppose you are writing a large program, for e.g.: A Scientific Calculator. The program is large enough to be written at a stretch and it is difficult to keep track of the different sections of the program. What you can do is dividing the sections and grouping them. Such groups are called modules. These modules are grouped together and is termed as Package.

In a specific sense:

Packages are the group of classes which can be imported into a program so that the user may implement it whenever it is wanted.

Library classes are also known as packages.

The Syntax of using a package is:

import <package_name>< . >< * ><;>

Example:

import java.io.*;

import java.util.*;

Wrapper Classes:

The primitive data values are defined in a class called Wrapper Class. The main purposes of a wrapper class are:

  • The objects can hold primitive values.
  • To convert data from one primitive type to another.

Wrapper classes come under Java’s library:  java.lang

Fact: When you enter any data from the keyboard, be it an integer or any data type, it is accepted as a character or a string.

Thus this data needs to be converted to a primitive data type to perform any mathematical calculation. You can do this using wrapper class.

Wrapper Class Data Types
Character char
Byte byte
Integer int
Long long
Float float
Double double

Remember:  The name of a primitive data type always starts with a lower case character whereas its wrapper class starts with an uppercase letter.

Converting a String to Primitive Types and Vice-Versa:

The syntax of Converting String to Primitive Data Type:

String <variable_name 1> = ”<Value> ” ;

<primitive_data_type> <variable_name 2> = <Equivalent_WrapperClass>.parse< primitive_data_type> (<variable_name 1>);

Looks confusing isn’t?

Now Assume

String x=” 12345”;

int n=Integer.parseInt(x);

double v=Double.parseDouble(x);

Now when you’ll print the value of n and v, 12345  and 12345.0 will be displayed respectively, thus converted into their primitive data types.

The syntax of Converting Primitive Data Type to String:

<Primitive_DataType> <variable_name1> = <value>

String <variable_name2> =<Wrapper Class>.toString(<variable_name1>;

Again assume:

int n= 421;

String x=Integer.toString(n);

Now when you’ll print the value of x, “421” will be displayed.

Now this value within double inverted quotes because the value of a String is always within quotes, be it an alphabetical value like “knowminni” or an alphanumeric value like “Axce123gh@”.

How to create a user defined package? 

If you’ve read the lesson, you might remember a reference to the user-defined package was made. Let us understand how to create a user-defined package.

It will be created as the following syntax:

Package <Package_Name>;

class <Class_name1>

{

// block of statements;

}

class <Class_name2>

{

// block of statements;

}

.

.

.

class <Class name n>

{

//Block of statements;

}

Java package and library classes