4.
Class Library Of Java
There are three ways to represent values in a program
1. Initialize variables or assign values to variables within the program
2. Receive values through parameters.
3. Input values from user during execution.
Initialize variables or assign values to variables.
Example program 1
class Sum1
{
//function to assign values and display sum
void displaySum()
{
int a,b;
a=10;
b=5;
System.out.println("Sum="+(a+b));
}
}
The drawback of this method is that the program will always work with the same set of values.
Receive values through parameters
Example Program 2
class Sum2
{
//function to accept values through parameters and to display sum
void displaySum(int a, int b)
{
System.out.println("Sum="+(a+b));
}
}
The drawback of this method is that you need BlueJ IDE to execute this program.
Input values from user during execution (using methods of class Scanner)
Example Program 3
YNotTheBest.in
import java.util.*;
class Sum3
{
//function to input two numbers and to display sum
void displaySum()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the first number");
int n1=sc.nextInt();
System.out.println("Enter the second number");
int n2=sc.nextInt();
System.out.println("sum="+(n1+n2));
}
}
import java.util.*;
import is a keyword in java.
java.util is one of the packages in java’s class library.
The group of pre-defined classes that are part of java programming language is called the
class library of java .
class library of java is also called java API /JCL
API- Application Programming interface
package in java
A java package is a group of similar types of classes, interfaces and sub-packages.
Packages are divided into two categories:
- Built-in Packages (packages from Java API)
- User-defined Packages (created by the programmer)
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 removes naming collision.
YNotTheBest.in
Built-in Packages (packages from the Java API)
Package Name Classes belong to the package
java.util Scanner
java.io InputStreamReader, BufferedReader
java.lang Math, System, String
Byte
Short
Wrapper Classes
Integer
Long
Float
Double
Boolean
Character
There are three ways to access classes from packages other than java.lang
i) import package.*;
ii) import package.classname;
ii) fully qualified name.
The import keyword is used to make the classes and interface of another package accessible to
the current class.
i) Accessing classes from other packages using import package.*;
If you use package.* then all the classes and interfaces of this package will be accessible.
* is a wild-card character that represents all the classes of a package.
Example of using package.*
Example Program 4
import java.util.*;
class Pgm4
{
//function to input two numbers and to display sum
void displaySum()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the first number");
int n1=sc.nextInt();
System.out.println("Enter the second number");
int n2=sc.nextInt();
System.out.println("sum="+(n1+n2));
}
}
ii) Accessing classes from other packages using import package.classname;
If you import package.classname then only declared class of this package will be accessible.
Example of using import package.classname
YNotTheBest.in
Example Program 5
import java.util.Scanner;
class Pgm5
{
//function to input two numbers and to display sum
void displaySum()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the first number");
int n1=sc.nextInt();
System.out.println("Enter the second number");
int n2=sc.nextInt();
System.out.println("sum="+(n1+n2));
}
}
iii)Accessing classes from other packages using fully qualified name.
packagename.classname is called the fully qualified name or the complete name of a class.
For example the fully qualified name of class String is java.lang.String
If you use fully qualified name then the declared class of this package will be accessible. Now
there is no need to import. But you need to use fully qualified name every time when you are
accessing the class or interface.
It is generally used when two packages have same class name e.g. java.util and java.sql packages
contain Date class.
Example of using classes of other packages by using the fully qualified name
Example Program 6
class Pgm6
{
//function to input two numbers and to display sum
void displaySum()
{
java.util.Scanner sc=new java.util.Scanner(System.in);
System.out.println("Enter the first number");
int n1=sc.nextInt();
System.out.println("Enter the second number");
int n2=sc.nextInt();
System.out.println("sum="+(n1+n2));
}
Scanner sc=new Scanner(System.in);
In this statement an object of class Scanner named sc is created and this object is linked to the
predefined input stream called System.in.
YNotTheBest.in