Unit-Iv Oopc Using Java
Unit-Iv Oopc Using Java
This package contains important classes like Socket, Proxy, Authenticator, URI,
URL, etc.
7. Javax Swing Package (javax.swing.*):
This package contains important classes like BoxLayout, ImageIcon, JButton, JCheckBox,
JComponent, JFrame, JLabel, JList, JPanel, etc.
8. java.applet: Contains classes for creating Applets.(AppletViwer)
9. java.awt: Contain classes for implementing the components for graphical user
interfaces (like button , ;menus etc)
User-defined packages
These are the packages that are defined by the user.
Creating User defined Package:
While creating a package, we should choose a name for the package and
include a package statement along with that name at the top of every source
file.
The package statement should be the first line in the source file.
Syntax:
package <package-name>;
Ex:
package cse;
In java, the accessibility of the members of a class or interface depends on its
access specifiers. The following table provides information about the visibility of
both data members and methods.
Compiling and executing the user defined
package java program
How to import Java Package
To import java package into a class, we need to use
java import keyword which is used to access package and its
classes into the java program.
Use import to access built-in and user-defined packages into
your java source file so that your class can refer to a class
that is in another package by directly using its name.
There are 3 different ways to refer to any class that is
present in a different package:
1. without import the package
2. import package with specified class
3. import package with all classes
without import the package(fully specified name)
CommandLineArguments in java
The command line argument is the argument that passed to a program during
runtime. It is the way to pass argument to the main method in Java.
These arguments store into the String type args parameter which is main method
parameter.
Java command-line argument is an argument i.e. passed at the time of running the
Java program.
In Java, the command line arguments passed from the console can be received in
the Java program and they can be used as input.
The users can pass the arguments during the execution bypassing the command-line
arguments inside the main() method.
So, it provides a convenient way to check the behavior of the program for the
different values. You can pass N (1,2,3 and so on) numbers of arguments from the
command prompt.
We need to pass the arguments as space-separated values. We can pass both strings
and primitive data types(int, double, float, char, etc) as command-line arguments.
These arguments convert into a string array and are provided to the main() function
as a string array argument.
we were unable to run more than one task in parallel. It was a
drawback, and to remove that drawback, Thread Concept was
introduced.
A Thread is a very light-weighted process, or we can say the
smallest part of the process that allows a program to operate
more efficiently by running multiple tasks simultaneously.
The java programming language allows us to create a program
that contains one or more parts that can run simultaneously at
the same time.
This type of program is known as a multithreading program.
Each part of this program is called a thread.
Every thread defines a separate path of execution in java.
All the tasks are executed without affecting the main
program.
Another benefit of using thread is that if a thread gets an exception or
an error at the time of its execution, it doesn't affect the execution of
the other threads.
Multitasking:
Multitasking is a process of performing multiple tasks simultaneously.
We can understand it by computer system that perform multiple tasks
like: writing data to a file, playing music, downloading file from remote
server at the same time.
Multitasking can be achieved either by using multiprocessing or
multithreading. Multitasking by using multiprocessing involves multiple
processes to execute multiple tasks simultaneously whereas
Multithreading involves multiple threads to executes multiple tasks.
Process is heavy weight, takes more memory and occupy CPU
for longer time that may lead to performance issue.
To overcome these issue process is broken into small unit of
independent sub-process.
These sub-process are called threads that can perform
independent task efficiently.
Process-based multitasking Thread-based multitasking
It allows the computer to run two or more It allows the computer to run two or more
programs concurrently threads concurrently
In this process is the smallest unit. In this thread is the smallest unit.
Process requires seperate address space for Threads share same address space.
each.
Process never gain access over idle time of Thread gain access over idle time of CPU.
CPU.
}
Exapmle on synchronized block
Java Synchronized Method
If you declare any method as synchronized, it is known as synchronized
method.
Synchronized method is used to lock an object for any shared resource.
When a thread invokes a synchronized method, it automatically acquires
the lock for that object and releases it when the thread completes its task.
Synchronized method It is a method that can be declared synchronized
using the keyword “synchronized” before the method name.
By writing this, it will make the code in a method thread-safe so that no
resources are shared when the method is executed.
Syntax:
}
If you make any static method as synchronized, the lock will be on the class
not on object.
we are using synchronized keyword on the static method to perform static
synchronization.
In case of synchronized method and synchronized block there cannot be
interference between t1 and t2 or t3 and t4 because t1 and t2 both refers to a
common object that have a single lock.
But there can be interference between t1 and t3 or t2 and t4 because t1
acquires another lock and t3 acquires another lock. So we need static
synchronization
Inter Thread Communication in java
Interthread communication is important when you develop an
application where two or more threads exchange some information.
There are three simple methods and a little trick which makes thread
communication possible. All the three methods are listed below −
These methods have been implemented as final methods in Object, so
they are available in all the classes. All three methods can be called
only from within a synchronized context.
Sr.No. Method & Description
public void wait()
Causes the current thread to wait
1
until another thread invokes the
notify().
public void notify()
2 Wakes up a single thread that is
waiting on this object's monitor.
public void notifyAll()
Wakes up all the threads that
3
called wait( ) on the same object.
Inter-thread communication is a process in which a thread is paused
running in its critical region and another thread is allowed to enter (or
lock) in the same critical region to be executed. i.e. synchronized threads
communicate with each other.
What is the difference between notify() and notifyAll()?
notify() wakes up only one thread and it follows thread priority.
notifyAll() wakes up multiple threads and it does not follow thread
priority.
Explain the reason why wait(), notify() and notifyAll() belong to the
object class instead of the thread class?
According to the Inter-thread communication rule, the owner must be
common to the multiple threads.
In Java, the owner can be any Java class object. To make these methods
available in any type of owner, Java defines those methods in object
class because it is a supreme class for all owners.
Vector in java
Vector is like the dynamic array which can grow or shrink its size.
Unlike array, we can store n-number of elements in it as there is no size limit.
It is found in the java.util package and implements the List interface, so we can use all the methods of List
interface here.
It is recommended to use the Vector class in the thread-safe implementation only.
It is similar to the ArrayList, but with two differences.
• Vector is synchronized.
• Java Vector contains many legacy methods that are not the part of a collections framework.
Java Vector Constructors
Vector class supports four types of constructors. These are given below:
SN Constructor Description
• Methods in ArrayList:
• boolean add(E element) - Appends given element to the ArrayList.
• boolean addAll(Collection c) - Appends given collection of elements to the
ArrayList.
• void add(int index, E element) - Inserts the given element at specified index.
• E get(int index) - Returns element at specified index from the ArrayList.
• int indexOf(E element) - Returns the index value of given element first occurence
in the ArrayList.
• int lastIndexOf(E element) - Returns the index value of given element last
occurence in the ArrayList.
E set(int index, E newElement) - Replace the element at specified index
with newElement in the invoking ArrayList.
• E remove(int index) - Removes the element at specified index in the
invoking ArrayList.
• boolean remove(Object element) - Removes the first occurence of the
given element from the invoking ArrayList.
void clear( ) - Removes all the elements from the ArrayList.
• int size( ) - Returns the total number of elements in the invoking
ArrayList.
• boolean isEmpty( ) - Returns true if the list is empty otherwise returns
false.
• void sort(Comparator c) - Sorts all the elements of invoking list based on
the given comparator.
• List[ ] subList(int startIndex, int endIndex) - Returns list of elements
starting from startIndex to endIndex-1.
void trimToSize( ) - Used to trim a ArrayList instance to the number of
elements it contains.