0% found this document useful (0 votes)
14 views12 pages

Java Unit I

The document provides an overview of Java basics, including its features, editions, applications, and architecture. It discusses Java's event handling, threading concepts, and networking features, emphasizing its object-oriented nature, platform independence, and security. Key components such as JVM, JRE, and JDK are explained, along with the process of creating and managing threads in Java.

Uploaded by

mei
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views12 pages

Java Unit I

The document provides an overview of Java basics, including its features, editions, applications, and architecture. It discusses Java's event handling, threading concepts, and networking features, emphasizing its object-oriented nature, platform independence, and security. Key components such as JVM, JRE, and JDK are explained, along with the process of creating and managing threads in Java.

Uploaded by

mei
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

CLASS: I M.

Sc (CS) SUBJECT: ADVANCED JAVA PROGRAMMING UNIT: I

BASICS OF JAVA
JAVA BASICS REVIEW
1. DISCUSS ABOUT JAVA BASICS. (PART-C)
2. EXPLAIN JAVA FEATURES. (PART-B)
 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 Microsystems changed the name to Java. In 2009, Sun Microsystems 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.
TYPES OF JAVA APPLICATIONS:
 There are four types of Java applications that can be created using Java programming:
o Standalone Applications: Java standalone applications use GUI components such as AWT,
Swing, and Java. These components contain buttons, list, menu, scroll panel, etc. It is also
known as desktop applcations.
o Enterprise Applications: An application which is distributed in nature is called
enterprise applications.
o Web Applications: An application that runs on the server is called web applications. We use
JSP, Servlet, spring, and Hibernate technologies for creating web applications.
o Mobile Applications: Java ME is a cross-platform to develop mobile applications which run
across smartphones. Java is a platform for App Development in Android.
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.
 Any hardware or software environment in which a program runs, is known as a platform. Since Java
has a runtime environment (JRE) and API, it is called a platform.
FEATURES OF JAVA:
 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.
 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.
 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.
 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.
 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.

1
 Portable: Java Byte code can be carried to any platform. No implementation-dependent features.
Everything related to storage is predefined, for example, the size of primitive data types.
 High Performance: Java is an interpreted language. Java enables high performance with the use of
the Just-In-Time compiler.
 Distributed: Java also has networking facilities. It is designed for the distributed environment of the
internet because it supports TCP/IP protocol. It can run over the internet. EJB and RMI are used to
create a distributed system.
 Multi-threaded: Java also supports multi-threading. It means to handle more than one job a time.
COMPONENTS AND EVENT HANDLING
3. DISCUSS ABOUT JAVA EVENT HANDLING. (PART-B)
EVENT AND LISTENER (JAVA EVENT HANDLING)
 Changing the state of an object is known as an event. For example, for click on button,
dragging mouse etc. The java.awt.event package provides many event classes and Listener interfaces
for event handling.
JAVA EVENT CLASSES AND LISTENER INTERFACES:
Event Classes Listener Interfaces
ActionEvent ActionListener
MouseEvent MouseListener and MouseMotionListener
MouseWheelEvent MouseWheelListener
KeyEvent KeyListener
ItemEvent ItemListener
TextEvent TextListener
AdjustmentEvent AdjustmentListener
WindowEvent WindowListener
ComponentEvent ComponentListener
ContainerEvent ContainerListener
FocusEvent FocusListener
STEPS TO PERFORM EVENT HANDLING:
 Following steps are required to perform event handling:
1. Register the component with the Listener
REGISTRATION METHODS:
 For registering the component with the Listener, many classes provide the registration
methods. For example:

2
Java Event Handling Code:
 We can put the event handling code into one of the following places:
o Within class
o Other class
o Anonymous class
Java event handling by implementing Action Listener:
1. import java.awt.*;
2. import java.awt.event.*;
3. class AEvent extends Frame implements ActionListener{
4. TextField tf;
5. AEvent(){
6.
7. //create components
8. tf=new TextField();
9. tf.setBounds(60,50,170,20);
10. Button b=new Button("click me");
11. b.setBounds(100,120,80,30);
12.
13. //register listener
14. b.addActionListener(this);//passing current instance
15.
16. //add components and set size, layout and visibility
17. add(b);add(tf);
18. setSize(300,300);
19. setLayout(null);
20. setVisible(true);
21.}
22. public void actionPerformed(ActionEvent e){
23. tf.setText("Welcome");
24.}
25. public static void main(String args[]){
26. new AEvent();
27.}
 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.

4. WRITE A BRIEF NOTE ON JAVA ARCHITECTURE. (PART-B)


 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:
o There is a 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.
3
Components of Java Architecture:
 The Java architecture includes the three main components:
o Java Virtual Machine (JVM)
o Java Runtime Environment (JRE)
o Java Development Kit (JDK)
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.
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.

4
 Execution Engine: It is the central part of the JVM. Its main task is to execute the byte code 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 evens 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.
Java Native Interface:
 Java Native Interface works as a mediator between Java method calls and native libraries.
Java Runtime Environment:
 It provides an environment in which Java programs are executed. JRE takes our Java code, integrates
it with the required libraries, and then starts the JVM to execute it.
Java Development Kit:
 It is a software development environment used in the development of Java applications and applets.
Java Development Kit holds JRE, a compiler, an interpreter or loader, and several
development tools in it.
THREADING CONCEPTS
5. EXPLAIN THREAD CONCEPT IN JAVA. (PART-C)
 Before introducing the thread concept, 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.
 In order to perform complicated tasks in the background, we used the Thread concept in Java.
All the tasks are executed without affecting the main program. In a 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.
Thread Model:
 Just like a process, a thread exists in several states. These states are as follows:

5
 New (Ready to run): A thread is in New when it gets CPU time.
 Running: A thread is in a Running state when it is under execution.
 Suspended: A thread is in the Suspended state when it is temporarily inactive or under
execution.
 Blocked: A thread is in the Blocked state when it is waiting for resources.
 Terminated: A thread comes in this state when at any given time, it halts its execution
immediately.
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.
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)
Runnable Interface (run() method):
 The Runnable interface is required to be implemented by that class whose instances are intended to
be executed by a thread. The runnable interface gives us the run() method to perform an action for
the thread.
start() method:
 The method is used for starting a thread that we have newly created. It starts a new thread with a new
callstack. After executing the start() method, the thread changes the state from New to Runnable.
It executes the run() method when the thread gets the correct time to execute it.
 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;

6
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
15. ThreadExample1 t1 = new ThreadExample1();
16. //calling start method to execute the run() method of the Thread class
17. t1.start();
18. }
19.}
Output:

Creating thread by implementing the runnable interface:


 In Java, we can also create a thread by implementing the runnable interface. The runnable interface
provides us both the run() method and the start() method.
ThreadExample2.java
1. class NewThread implements Runnable {
2. String name;
3. Thread thread;
4. NewThread (String name){
5. this.name = name;
6. thread = new Thread(this, name);
7. System.out.println( "A New thread: " + thread+ "is created\n" );
8. thread.start();
9. }
10. public void run() {
11. try {
12. for(int j = 5; j > 0; j--) {
13. System.out.println(name + ": " + j);
14. Thread.sleep(1000);
15. }
16. }catch (InterruptedException e) {
17. System.out.println(name + " thread Interrupted");
18. }
19. System.out.println(name + " thread exiting.");
20. }
21.}
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);
7
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. }
34.}
Output:

NETWORKING FEATURES
6. EXPLAIN NETWORKING FEATURES. (PART-C)
 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,
o TCP: Transmission Control Protocol provides reliable communication between the sender
and receiver. TCP is used along with the Internet Protocol referred as TCP/IP.
o UDP: User Datagram Protocol provides a connection-less protocol service by allowing
packet of data to be transferred along two or more nodes
Java Networking Terminology:
 The widely used Java networking terminologies are given below:
o IP Address
o Protocol
o Port Number
o MAC Address
o Connection-oriented and connection-less protocol
o Socket

8
IP Address:
 IP address is a unique number assigned to a node of a network e.g. 192.168.0.1.
It is composed of octets that range from 0 to 255. It is a logical address that can be changed.
Protocol:
 A protocol is a set of rules basically that is followed for communication. For example:
o TCP
o FTP
o Telnet
o SMTP
o POP etc.
Port Number:
 The port number is used to uniquely identify different applications. It acts as a communication
endpoint between applications. The port number is associated with the IP address for
communication between two applications.
MAC Address:
 MAC (Media Access Control) address is a unique identifier of NIC (Network Interface
Controller). A network node can have multiple NIC but each with unique MAC address.
For example, an Ethernet card may have a MAC address of 00:0d:83::b1:c0:8e.
Connection-oriented and connection-less protocol:
 In connection-oriented protocol, acknowledgement is sent by the receiver. So it is reliable but slow.
The example of connection-oriented protocol is TCP. But, in connection-less protocol,
acknowledgement is not sent by the receiver. So it is not reliable but fast. The example of
connection-less protocol is UDP.
Socket:
 A socket is an endpoint between two way communications.
 Java.net package: The java.net package can be divided into two sections:
o 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.
o 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.
 The java.net package provides many classes to deal with networking applications in Java. A list of
these classes is given below.

9
 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.
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.
Datagram Socket and Datagram Packet classes:
 These classes are used for UDP-based networking in Java. The java.net.DatagramSocket class
represents a connectionless socket for sending and receiving datagram packets.
 The java.net.DatagramPacket class encapsulates the data to be sent or received, along with the
destination or source address.
Multicast Socket class:
 The java.net.MulticastSocket class is used to implement multicast networking in Java. It allows
sending and receiving IP multicast packets, enabling communication between multiple hosts on a
network. These are just a few of the networking features provided by Java. By utilizing these
features, developers can create robust and efficient networked applications in Java.
MEDIA TECHNIQUES
7. EXPLAIN MEDIA TECHNIQUES. (PART-B)
8. EXPLAIN COMPONENTS OF MULTIMEDIA. (PART-B)
 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 communication tool. Education,
training, reference materials, corporate presentations, marketing, and documentary are a few
industries that heavily utilize multimedia.

10
 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:
o 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.
o 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:
 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.
 Vector Graphics: Computers can draw vector graphics because they just need a little
amount of memory. These images can be changed.
o 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.
o Audio - Speech, music, and sound effects could all be necessary for a multimedia
application. 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.
o 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.
 There are several media techniques that can be used in Java programming:
 Image Manipulation: 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.

11
 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 Media Player 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.
 Web Content Integration: JavaFX provides WebView class that enables developers to embed web
content in Java applications. This allows you to integrate media elements like images, audio,
and video from web sources into your Java application. These are just a few examples of media
techniques that can be used in Java programming.
PART – A [1 Mark]
1. Which of the following is not a Java features?
a) Dynamic b) Architecture Neutral c) Use of pointers d) Object-oriented
2. Which one of the following is used to find and fix bugs in the Java programs?
a) JVM b) JRE c) JDK d) JDB
3. Which of these packages contains all the classes and methods required for even handling in Java?
a) java.applet b) java.awt c) java.event d) java.awt.event
4. Which of these methods are used to register a keyboard event listener?
a) KeyListener() b) AddKistener() c) AddKeyListener() d) EventKeyboardListener()
5. Event class is defined in which of these libraries?
a) java.io b) java.lang c) java.net d) java.util
6. Thread priority in Java is?
a) integer b) float c) double d) long
7. Which of these methods of Thread class is used to suspend a thread for a period of time?
a) sleep() b) terminate() c) suspend() d) stop()
8. Which of these package contains classes and interfaces for networking?
a) java.io b) java.util c) java.net d) java.network
9. Which of these class is used to encapsulate IP address and DNS?
a) DatagramPacket b) URL c) InetAddress d) ContentHandler
10. Multimedia is also used for ---- communications and presentations.
a) Corporate b) Company c) Communal d) Commercial
PART – B [5 Marks]
1. Explain Java features.
2. Discuss about Java event handling.
3. Write a brief note on Java architecture.
4. Explain media techniques.
PART – C [10 Marks]
1. Discuss about Java basics.
2. Explain thread concept in Java.
3. Explain networking features.
4. Explain components of multimedia.

UNIT-I COMPLETED
12

You might also like