UNIT I Java
UNIT I Java
BASICS OF JAVA
Java
Java is a high-level, general-purpose, object-oriented, and secure programming
language developed by James Gosling at Sun Microsystems, Inc. in 1991. It is formally
known as OAK. In 1995, Sun Microsystem changed the name to Java. In 2009, Sun
Microsystem takeover by Oracle Corporation.
Java was developed by James Gosling at Sun Microsystems and it was released in
May 1995 as a core component of Sun Microsystems' Java platform.
EDITIONS OF JAVA
Each edition of Java has different capabilities. There are three editions of Java:
o Java Standard Editions (JSE): It is used to create programs for a desktop computer.
o Java Enterprise Edition (JEE): It is used to create large programs that run on the
server and manages heavy traffic and complex transactions.
o Java Micro Edition (JME): It is used to develop applications for small devices such
as set-top boxes, phone, and appliances.
JAVA PLATFORM
Java Platform is a collection of programs. It helps to develop and run a program
written in the Java programming language. Java Platform includes an execution engine, a
compiler and set of libraries. Java is a platform-independent language.
FEATURES OF JAVA
o Simple: Java is a simple language because its syntax is simple, clean, and easy to
understand. Complex and ambiguous concepts of C++ are either eliminated or re-
implemented in Java. For example, pointer and operator overloading are not used in
Java.
o Object-Oriented: In Java, everything is in the form of the object. It means it has
some data and behavior. A program must have at least one class and object.
1|Page
o Robust: Java makes an effort to check error at run time and compile time. It uses a
strong memory management system called garbage collector. Exception handling
and garbage collection features make it strong.
o Secure: Java is a secure programming language because it has no explicit pointer
and programs runs in the virtual machine. Java contains a security manager that
defines the access of Java classes.
o Platform-Independent: Java provides a guarantee that code writes once and run
anywhere. This byte code is platform-independent and can be run on any machine.
What is Java?
Java is a programming language and a platform. Java is a high level, robust,
object- oriented and secure programming language.
Java was developed by Sun Microsystems (which is now the subsidiary of Oracle) in
the year 1995. James Gosling is known as the father of Java. Before Java, its name was
Oak. Since Oak was already a registered company, so James Gosling and his team changed
the name from Oak to Java.
Java Example
2|Page
Let's have a quick look at Java programming example. A detailed description of
Hello Java example is available in next page.
Simple.java
1. class Simple{
2. public static void main(String args[]){
3. System.out.println("Hello Java");
4. }
5. }
Application
According to Sun, 3 billion devices run Java. There are many devices where Java is
currently used. Some of them are as follows:
1. Desktop Applications such as acrobat reader, media player, antivirus, etc.
2. Web Applications such as irctc.co.in, javatpoint.com, etc.
3. Enterprise Applications such as banking applications.
4. Mobile
5. Embedded System
6. Smart Card
7. Robotics
8. Games, etc.
3|Page
Stream, Networking, AWT, Swing, Reflection, Collection, etc.
Java EE (Java Enterprise Edition)
It is an enterprise platform that is mainly used to develop web and
enterprise applications. It is built on top of the Java SE platform. It includes topics
like Servlet, JSP, Web Services, EJB, JPA, etc.
Java ME (Java Micro Edition)
It is a micro platform that is dedicated to mobile applications.
JavaFX
It is used to develop rich internet applications. It uses a lightweight user interface API.
Java Architecture
Java Architecture is a collection of components, i.e., JVM, JRE, and JDK. It
integrates the process of interpretation and compilation. It defines all the processes involved
in creating a Java program. Java Architecture explains each and every step of how a
program is compiled and executed.
Java Architecture can be explained by using the following
steps:
There is process of compilation and interpretation in Java.
o Java compiler converts the Java code into byte code.
o After that, the JVM converts the byte code into machine code.
o The machine code is then executed by the machine.
The following figure represents the Java Architecture in which each step is elaborate
graphically.
Now let's dive deep to get more knowledge about Java Architecture. As we know
that the Java architecture is a collection of components, so we will discuss each and every
component into detail.
4|Page
•
Java Virtual Machine
The main feature of Java is WORA. WORA stands for Write Once Run Anywhere.
The feature states that we can write our code once and use it anywhere or on any operating
system. Our Java program can run any of the platforms only because of the Java Virtual
Machine. It is a Java platform component that gives us an environment to execute java
programs. JVM's main task is to convert byte code into machine code.
JVM, first of all, loads the code into memory and verifies it. After that, it executes
the code and provides a runtime environment. Java Virtual Machine (JVM) has its own
architecture, which is given below:
JVM Architecture
JVM is an abstract machine that provides the environment in which Java byte code is
executed. The falling figure represents the architecture of the JVM.
Class Loader: Class Loader is a subsystem used to load class files. Class Loader first loads
the Java code whenever we run it.
Class Method Area: In the memory, there is an area where the class data is stored during
the code's execution. Class method area holds the information of static variables, static
methods, static blocks, and instance methods.
Heap: The heap area is a part of the JVM memory and is created when the JVM starts up.
Its size cannot be static because it increase or decrease during the application runs.
Stack: It is also referred to as thread stack. It is created for a single execution thread. The
thread uses this area to store the elements like the partial result, local variable, data used for
calling method and returns etc.
Native Stack: It contains the information of all the native methods used in our application.
Execution Engine: It is the central part of the JVM. Its main task is to execute the byte code
5|Page
and execute the Java classes. The execution engine has three main components used for
executing Java classes.
o Interpreter: It converts the byte code into native code and executes. It sequentially
executes the code. The interpreter interprets continuously and even the same method
multiple times. This reduces the performance of the system, and to solve this, the JIT
compiler is introduced.
o JIT Compiler: JIT compiler is introduced to remove the drawback of the interpreter.
It increases the speed of execution and improves performance.
o Garbage Collector: The garbage collector is used to manage the memory, and it is a
program written in Java. It works in two phases, i.e., Mark and Sweep. Mark is an
area where the garbage collector identifies the used and unused chunks of memory.
The Sweep removes the identified object from the Mark
6|Page
AdjustmentEvent AdjustmentListener
WindowEvent WindowListener
ComponentEvent ComponentListener
ContainerEvent ContainerListener
FocusEvent FocusListener
7|Page
9. Button b=new Button("click me");
10. b.setBounds(100,120,80,30);
11. //register listener
12. b.addActionListener(this);//passing current instance
15. //add components and set size, layout and visibility
16. add(b);add(tf);
17. setSize(300,300);
18. setLayout(null);
19. setVisible(true);
20. }
21. public void actionPerformed(ActionEvent e){
22. tf.setText("Welcome");
23. }
24. public static void main(String args[]){
25. new AEvent();
26. }
public void set Bounds(int xaxis, int yaxis, int width, int height); have
been used in the above example that sets the position of the component it may be
button, text field.
8|Page
program or process, all the threads have their own separate path for execution, so each
thread of a process is independent.
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. All the threads share
a common memory and have their own stack, local variables and program counter. When
multiple threads are executed in parallel at the same time, this process is known
as Multithreading.
In a simple way, a Thread is a:
o Feature through which we can perform multiple activities within a single process.
o Lightweight process.
o Series of executed statements.
o Nested sequence of method calls.
Thread Model
Just like a process, a thread exists in several states. These states are as follows:
Creating Thread
A thread is created either by "creating or implementing" the Runnable Interface or
by extending the Thread class. These are the only two ways through which we can create a
thread.
9|Page
Let's dive into details of both these way of creating a thread:
Thread Class
A Thread class has several methods and constructors which allow us to perform various
operations on a thread. The Thread class extends the Object class. The Object class
implements the Runnable interface. The thread class has the following constructors that are
used to perform various operations.
o Thread()
o Thread(Runnable, String name)
o Thread(Runnable target)
o Thread(ThreadGroup group, Runnable target, String name)
o Thread(ThreadGroup group, Runnable target)
o Thread(ThreadGroup group, String name)
o Thread(ThreadGroup group, Runnable target, String name, long stackSize)
Let's take an example to understand how we can create a Java thread by extending the
Thread class:
ThreadExample1.java
1. // Implementing runnable interface by extending Thread class
2. public class ThreadExample1 extends Thread {
3. // run() method to perform action for thread.
4. public void run()
5. {
6. int a= 10;
7. int b=12;
8. int result = a+b;
9. System.out.println("Thread started running..");
10. System.out.println("Sum of two numbers is: "+ result);
11. }
12. public static void main( String args[] )
13. {
14. // Creating instance of the class extend Thread class
10 | P a g e
15. ThreadExample1 t1 = new ThreadExample1();
16. //calling start method to execute the run() method of the Thread class
17. t1.start();
18. }
19. }
Output:
11 | P a g e
18. }
19. System.out.println(name + " thread exiting.");
20. }}
22. class ThreadExample2 {
23. public static void main(String args[]) {
24. new NewThread("1st");
25. new NewThread("2nd");
26. new NewThread("3rd");
27. try {
28. Thread.sleep(8000);
29. } catch (InterruptedException excetion) {
30. System.out.println("Inturruption occurs in Main Thread");
31. }
32. System.out.println("We are exiting from Main Thread");
33. }}
Output:
Java Networking
Java Networking is a concept of connecting two or more computing devices
together so that we can share resources.
Java socket programming provides facility to share data between different
computing devices.
Advantage of Java Networking
1. Sharing resources
2. Centralize software management
The java.net package supports two protocols,
12 | P a g e
1. TCP: Transmission Control Protocol provides reliable communication between the
sender and receiver. TCP is used along with the Internet Protocol referred as TCP/IP.
2. UDP: User Datagram Protocol provides a connection-less protocol service by
allowing packet of data to be transferred along two or more nodes
Java.net package
The java.net package can be divided into two sections:
1. A Low-Level API: It deals with the abstractions of addresses i.e. networking
identifiers, Sockets i.e. bidirectional data communication mechanism and Interfaces
i.e. network interfaces.
2. A High Level API: It deals with the abstraction of URIs i.e. Universal Resource
Identifier, URLs i.e. Universal Resource Locator, and Connections i.e. connections
to the resource pointed by URLs.
13 | P a g e
The java.net package provides many classes to deal with networking
applications in Java. A list of these classes is given below:
Inet4Address Inet6Address
NETWORKING FEATURES
In Java, there are several networking features that allow developers to create networked
applications. Some of the most commonly used networking features in Java are:
Java.net package:
This package provides classes to implement network protocols, such as TCP/IP, UDP,
Sockets, and URLs. It allows developers to establish connections and communicate with other
computers over a network.
14 | P a g e
Socket programming:
Java's java.net.Socket class provides a low-level interface for networking. It allows
developers to create client-server applications using TCP/IP or UDP protocols. Sockets can be used
to send and receive data over a network.
URL handling:
Java's java.net.URL class provides methods to handle URLs. It allows developers to read and
write data from a specified URL, such as reading a webpage or downloading a file from the internet.
InetAddress class:
The java.net.InetAddress class is used to represent Internet Protocol (IP) addresses.
It provides methods to get the IP address of a host, resolve hostnames, and perform IP address
manipulation.
Network interfaces:
Java's java.net.NetworkInterface class provides methods to retrieve information about
network interfaces on the system. It allows developers to determine available network interfaces,
their IP addresses, and other related information.
Server socket class:
Java's java.net.ServerSocket class allows developers to create server applications that listen
for incoming connections on a specific port. This class is used to implement server-side socket
programming.
MULTIMEDIA
Multimedia is an engaging kind of media that offers a variety of effective ways to convey
information to users. Users can interact with digital information through it. It serves as a
15 | P a g e
communication tool. Education, training, reference materials, corporate presentations, marketing,
and documentary are a few industries that heavily utilize multimedia.
Multimedia, by definition, is the use of text, audio, video, graphics, and animation to convey
information in an engaging and dynamic way. In other terms, multimedia is a technological way of
presenting information that combines audio, video, images, and animations with textual data.
Examples include video conferencing, Yahoo Messenger, email, and the Multimedia Messaging
Service ( MMS Service (MMS).
As the name implies, multimedia is the combination of the words "multi" and "media,"
which refers to the various media (hardware/software) utilized for information
transmission.
COMPONENTS OF MULTIMEDIA
The following are typical multimedia elements:
1) Text - Text appears in all multi-media projects to some extent. To match the successful
presentation of the multimedia program, the text may be presented in a variety of font styles and sizes.
2) Graphics - The multimedia program is appealing because of its graphics. People
frequently find it difficult to read long passages of text on screens. As a result, visuals are
frequently utilized instead of writing to convey ideas, give context, etc. Graphics can be of
two different types:
o Bitmap - Bitmap images are authentic pictures that can be taken using tools like
digital cameras or scanners. Bitmap pictures are often not modifiable. Memory use
for bitmap pictures is high.
o Vector Graphics - Computers can draw vector graphics because they just need a
little amount of memory. These images can be changed.
3) Animation - A static picture can be animated to appear to be in motion. A continuous succession
of static images shown in order is all that makes up an animation. Effective attention-getting may be
achieved by the animation. Additionally, animation adds levity and appeal to a presentation. In
multimedia applications, the animation is fairly common.
Audio - Speech, music, and sound effects could all be necessary for a multimedia application.
4) They are referred to as the audio or sound component of multimedia. Speaking is a fantastic
educational tool. Analog and digital audio are both kinds. The initial sound signal is referred to as
analog audio or sound. Digital sound is saved on a computer. Digital audio is therefore utilized for
sound in multimedia applications.
5) Video - The term "video" describes a moving image that is supported by sound, such as a
television image. A multimedia application's video component conveys a lot of information quickly.
For displaying real-world items in multimedia applications, digital video is helpful. If uploaded to the
internet, the video really does have the highest performance requirements for computer memory and
bandwidth. The quality of digital video files may still be preserved while being saved on a computer,
similarly to other data. A computer network allows for the transport of digital video files. The digital
video snippets are simple to modify.
16 | P a g e
APPLICATIONS OF MULTIMEDIA
The typical areas where multimedia is applied are listed below.
1) For Entertainment purposes - Multimedia marketing may significantly improve the promotion
of new items. Both advertising and marketing staff had their doors opened by the economical
communication boost provided by multimedia. Flying banner presentations, video transitions,
animations, and audio effects are just a few of the components utilized to create a multimedia-based
advertisement that appeals to the customer in a brand-new way and encourages the purchase of the
goods.
2) For Education purposes - There are currently a lot of educational computer games accessible.
Take a look at an illustration of an educational app that plays children's rhymes. In addition to merely
repeating rhymes, the youngster may create drawings, scale items up or down, and more. There are
many more multimedia products on the market that provide children with a wealth of in-depth
knowledge and playing options.
3) For business purposes - There are several commercial uses for multimedia. Multimedia and
communication technologies have made it possible for information from international work groups.
Today's team members can work remotely and for a variety of businesses. A global workplace will
result from this. The following facilities should be supported by the multimedia network:
o Office needs
o Records management
o Employee training
o Electronic mail
o Voice mail
For marketing purposes - Multimedia marketing may significantly improve the promotion of new
items. Both advertising and promotion staff had their doors opened by the economical communication
boost provided by multimedia. Flying banner presentations, video transitions, animations, and audio.
4) effects are just a few of the components utilized to create a multimedia- based advertisement that
appeals to the customer in a brand-new way and encourages the purchase of the goods.
5) For banking purposes - Another public setting where multimedia is being used more and more
recently is banks. People visit banks to open savings and current accounts, make deposits and
withdrawals, learn about the bank's various financial plans, apply for loans, and other things. Each
bank wants to notify its consumers with a wealth of information. It can employ multimedia in a
variety of ways to do this. The bank also has a PC monitor in the clients' rest area that shows details
about its numerous programs. Online and internet banking have grown in popularity recently. These
heavily rely on multimedia. As a result, banks are using multimedia to better serve their clients and
inform them of their appealing financing options.
There are several media techniques that can be used in Java programming:
Image Manipulation:
17 | P a g e
Java provides various libraries and classes for loading, manipulating, and saving images. For
example, the javax.imageio package allows developers to read and write images in different formats,
while java.awt.image package offers classes for performing operations like scaling, cropping, and
filtering on images.
Audio Playback:
Java Sound API enables developers to play audio files of different formats. It provides classes
like Clip and AudioInputStream to load and manipulate audio data. JavaFX also provides media
classes for playing audio files, along with advanced features such as playback controls and volume
management.
Video Playback:
JavaFX has built-in media capabilities, which allow developers to play video files in different
formats. The javafx.scene.media package provides classes like MediaPlayer and MediaView for
handling video playback. By using these classes, you can control video playback, handle events, and
even apply visual effects to the video.
Animation:
Java provides several libraries and frameworks that support animation. For example, JavaFX
provides a powerful animation framework with classes like Animation, Timeline, and KeyFrame.
These classes allow you to create smooth animations by specifying time-based actions and transitions.
3D Graphics:
Java 3D API provides support for creating and manipulating 3D graphics in Java. It offers a
comprehensive set of classes and utilities for constructing and rendering 3D scenes. With Java 3D,
you can create complex 3D objects, apply textures and lighting effects, and even perform animations
in three-dimensional space.
18 | P a g e