0% found this document useful (0 votes)
13 views

Library Classes and Packages

..

Uploaded by

anshj9797
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Library Classes and Packages

..

Uploaded by

anshj9797
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

JAVA PACKAGE

▪ A java package is a group of similar types of


classes, interfaces and sub-packages.
▪ Package in java can be categorized in two
form, built-in package and user-defined
package.
▪ There are many built-in packages such as
java, lang, awt, javax, swing, net, io, util, sql
etc.
Built-in Packages

▪ The Java API is a library of prewritten classes, that


are free to use, included in the Java Development
Environment.
▪ The library contains components for managing
input, database programming, and much much
more. The complete list can be found at Oracles
website: https://docs.oracle.com/javase/8/docs/api/.
▪ The library is divided into packages and classes.
Meaning you can either import a single class (along
with its methods and attributes), or a whole package
that contain all the classes that belong to the
specified package.
▪ Built-in Packages
These packages consist of a large number of classes which are a
part of Java API.Some of the commonly used built-in packages
are:
1) java.lang: Contains language support classes(e.g classed
which defines primitive data types, math operations). This
package is automatically imported.
2) java.io: Contains classed for supporting input / output
operations.
3) java.util: Contains utility classes which implement data
structures like Linked List, Dictionary and support ; for Date /
Time operations.
4) java.applet: Contains classes for creating Applets.
5) java.awt: Contain classes for implementing the components
for graphical user interfaces (like button , ;menus etc).
6) java.net: Contain classes for supporting networking
operations.
▪ Advantage of Java Package
▪ 1) Java package is used to categorize the
classes and interfaces so that they can be
easily maintained.
▪ 2) Java package provides access protection.
▪ 3) Java package removes naming collision.
▪ To use a class or a package from the library,
you need to use the import keyword:
▪ Syntax
▪ import package.name.Class;
▪ // Import a single class import
package.name.*;
▪ // Import the whole package
Import a Class

▪ If you find a class you want to use, for


example, the Scanner class, which is used to
get user input, write the following 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.
▪ To use the Scanner class, create an object of
the class and use any of the available
methods found in the Scanner class
documentation. 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 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
▪ 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 following
example will import ALL the classes in
the java.util package:
▪ Example
▪ import java.util.*;
User-defined Packages

▪ To create your own package, you need to


understand that Java uses a file system
directory to store them. Just like folders on
your computer:
▪ To create a package, use
the package keyword:
▪ MyPackageClass.java
▪ package mypack; class MyPackageClass {
public static void main(String[] args) {
System.out.println("This is my package!"); } }
▪ Then compile the package:
▪ C:\Users\Your Name>javac -d . MyPackageClass.java
▪ This forces the compiler to create the "mypack"
package.
▪ The -d keyword specifies the destination for where
to save the class file. You can use any directory
name, like c:/user (windows), or, if you want to keep
the package within the same directory, you can use
the dot sign ".", like in the example above.
▪ Note: The package name should be written in lower
case to avoid conflict with class names.
▪ When we compiled the package in the
example above, a new folder was created,
called "mypack".
▪ To run the MyPackageClass.java file, write
the following:
▪ C:\Users\Your Name>java
mypack.MyPackageClass
▪ Important points:
▪ Every class is part of some package.
▪ If no package is specified, the classes in the file goes into a
special unnamed package (the same unnamed package for
all files).
▪ All classes/interfaces in a file are part of the same package.
Multiple files can specify the same package name.
▪ If package name is specified, the file must be in a
subdirectory called name (i.e., the directory name must
match the package name).
▪ We can access public classes in another (named) package
using: package-name.class-name
JAR

▪ java library is deployed in a jar file format.


▪ JAR or Java Archive is like a zipped file which
contains many java classes and its associated
resources such as images, text etc. into one
file to distribute as libraries
▪ “ java.lang“ Package is automatically
imported by Java.
▪ Name the package that contains –
▪ 1) BufferedReader 2) Scanner 3) Math?

▪ Ans. 1) BufferedReader: java.io


2) Scanner: java.util
3) Math: java.lang
Java Math Class :
The java.lang.Math class contains various methods
for performing basic numeric operations such as the
logarithm, cube root, and trigonometric functions
etc. The various java math methods are as follows:
▪ Math.abs()It will return the Absolute value of the given value.
▪ Math.max()It returns the Largest of two values.
▪ Math.min()It is used to return the Smallest of two values.
▪ Math.round()It is used to round of the decimal numbers to the
nearest value.
▪ Math.sqrt()It is used to return the square root of a number.
▪ Math.cbrt()It is used to return the cube root of a number.
▪ Math.pow()It returns the value of first argument raised to the
power to second argument.
▪ Math.ceil()It is used to find the smallest integer value that is greater
than or equal to the argument or mathematical integer.
▪ Math.floor()It is used to find the largest integer value which is less
than or equal to the argument and is equal to the mathematical
integer of a double value.
▪ Math.random()It returns a double value with a positive sign, greater
than or equal to 0.0 and less than 1.0.
▪ Math.rint()It returns the double value that is closest to the given
argument and equal to mathematical integer.
▪ Math.log()It returns the natural logarithm of a double value.
▪ Math.exp()It returns E raised to the power of a double value, where E
is Euler’s number and it is approximately equal to 2.71828.
▪ Math.sin()It is used to return the trigonometric Sine value of a Given
double value.
▪ Math.cos()It is used to return the trigonometric Cosine value of a
Given double value.Math.tan()It is used to return the trigonometric
Tangent value of a Given double value.
JCL

▪ vJava Class Library (JCL) are dynamically


loadable libraries that Java applications can
call at run time. Some examples of JCL are
java.lang, java.util, java.math, java.awt. etc
Wrapper classes in Java

▪ The wrapper class in Java provides the


mechanism to convert primitive into object and
object into primitive.
▪ Use of Wrapper classes in Java
▪ Java is an object-oriented programming language, so we
need to deal with objects many times like in Collections,
Serialization, Synchronization, etc. Let us see the
different scenarios, where we need to use the wrapper
classes.
▪ Change the value in Method: Java supports only call by
value. So, if we pass a primitive value, it will not change
the original value. But, if we convert the primitive value
in an object, it will change the original value.
▪ Serialization: We need to convert the objects into
streams to perform the serialization. If we have a
primitive value, we can convert it in objects through the
wrapper classes.
▪ Synchronization: Java synchronization works with
objects in Multithreading.
▪ java.util package: The java.util package provides the
utility classes to deal with objects.
▪ Byte: The Byte class wraps a value of
primitive type byte in an object.
▪ Character: The Character class wraps a value
of the primitive type char in an object.
▪ Double: The Double class wraps a value of the
primitive type double in an object.
▪ Float: The Float class wraps a value of
primitive type float in an object.
▪ Integer: The Integer class wraps a value of the
primitive type int in an object.
▪ Long: The Long class wraps a value of the
primitive type long in an object.

You might also like