Java Programming Unit Iii PDF
Java Programming Unit Iii PDF
• 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.
• 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
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:
3) java.util: Contains utility classes which implement data structures like Linked
List, Dictionary and support ; for Date / Time operations.
5) java.awt: Contain classes for implementing the components for graphical user
interfaces (like button , menus etc).
obj.getNames(name);
}
}
Note : MyClass.java must be saved inside the myPackage
directory since it is a part of the package.
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
Using user defined package Arith which contains arithmetic operations
package myPackage;
public class Arith {
public int Add(int a,int b)
{
return(a+b);
}
public int Sub(int a,int b)
{
return(a-b);
}
}
import myPackage.Arith;
import java.util.*;
public class ArithTest {
public static void main(String args[])
{
System.out.println("Enter two numbers");
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int b=sc.nextInt();
Arith a1 = new Arith();
System.out.println("The sum is"+a1.Add(a,b));
System.out.println(a1.Sub(a,b));
}
}
Advantages of using packages in Java
• Since the package creates a new namespace there won't be any name
conflicts with names in other packages.
• Suppose you have developed a very large application that includes many
modules. As the number of modules grows, it becomes difficult to keep track
of them all if they are dumped into one location. This is particularly so if they
have similar names or functionality. You might wish for a means of grouping
and organizing them.
THIS Keyword in Java
Keyword THIS is a reference variable in Java that refers to the current object.
The various usages of 'THIS' keyword in Java are as follows:
•It can be used to refer instance variable of current class
•It can be used to invoke or initiate current class constructor
•It can be passed as an argument in the method call
•It can be passed as argument in the constructor call
•It can be used to return the current class instance
public class Account {
int a;
int b;
void setdata(int a ,int b)
{
a=a;
b=b;
}
void showdata()
{
System.out.println("The value of a is" + a);
System.out.println("The value of b is"+b);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Account acc=new Account();
acc.setdata(5, 10);;
acc.showdata();
}
}
What is Garbage Collection in Java?
• Garbage Collection in Java is a process by which the programs perform memory
management automatically.
• The Garbage Collector(GC) finds the unused objects and deletes them to reclaim the
memory.
• In Java, dynamic memory allocation of objects is achieved using the new operator that
uses some memory and the memory remains allocated until there are references for
the use of the object.
• The technique that accomplishes this is known as Garbage Collection. Programs that
do not de-allocate memory can eventually crash when there is no memory left in the
system to allocate. These programs are said to have memory leaks.
Inner classes are of three types depending on how and where you define them. They are −
• Inner Class
• Method-local Inner Class
• Anonymous Inner Class
Inner Class
Creating an inner class is quite simple. You just need to write a class within a class. Unlike
a class, an inner class can be private and once you declare an inner class private, it
cannot be accessed from an object outside the class.
Following is the program to create an inner class and access it. In the given example, we
make the inner class private and access the class through a method.
class Outer_Demo {
int num;
// inner class
private class Inner_Demo {
public void print() {
System.out.println("This is an inner class");
}
}
// Accessing he inner class from the method within
void display_Inner() {
Inner_Demo inner = new Inner_Demo();
inner.print();
}
}
public class My_class {
A method-local inner class can be instantiated only within the method where the inner
class is defined. The following program shows how to use a method-local inner class.
public class Outerclass {
// instance method of the outer class
void my_Method() {
int num = 23;
nested.my_method();
}
}
Java Wrapper Classes
Wrapper classes provide a way to use primitive data types (int, boolean, etc..) as
objects.
The table below shows the primitive type and the equivalent wrapper class:
Output:
5
5.99
A
Methods Of wrapper classes contd-----
Another useful method is the toString() method, which is used to convert wrapper
objects to strings.
In the following example, we convert an Integer to a String, and use the length()
method of the String class to output the length of the "string":
public class MyClass {
public static void main(String[] args) {
Integer myInt = 100;
String myString = myInt.toString();
System.out.println(myString.length());
}
}
Output:
3