0% found this document useful (0 votes)
26 views76 pages

Java PTU

Uploaded by

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

Java PTU

Uploaded by

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

Dr. N.

SIVAKUMAR
M.Tech, MHRM, MBA, Ph.D

Professor, CSE
Puducherry Technological University
1
2
JAVA Features
• Simple • Robust
• Secured • Dynamic
• Object-Oriented • Class – Based
• Platform Independent
• Architectural – Neutral
• Portable
• High Performance
• Multithreaded
• Interpreted
• Distributed
3
JAVA Features
• Simple • Robust
• Secured • Dynamic
• Object-Oriented • Class – Based
• Platform Independent • Architectural – Neutral
• Portable • High Performance
• Multithreaded • Interpreted
• Distributed 4
JAVA Features
• Simple • Robust
• Secured • Dynamic
• Object-Oriented • Class – Based
• Platform Independent • Architectural – Neutral
• Portable • High Performance

• Multithreaded Interpreted

• Distributed 5
6
Operators in JAVA

7
Operators in JAVA

8
Control Structures in JAVA

9
Object Oriented Features
• Objects • Message Passing
• Class • Data Binding
• Abstraction • Overloading
• Encapsulation • Constructor
• Inheritance • Polymorphism

10
Class
• The basic building block of an object-oriented language
• A class is a blueprint from which individual objects are created.
• It is a template that describes the data and behavior associated with
instances of that class

11
Class

12
Object
• An entity that has state and behavior is known as an object
e.g. chair, bike, marker, pen, table, car etc.
• Object is an instance of a class.

13
Object Creation

14
3 Ways to initialize object
• There are 3 ways to initialize object in java.
• By reference variable
• By method
• By constructor

15
16
17
Encapsulation

18
Constructor
• Constructor is a block of codes similar to method
• It is a special type of method which is used to initialize the object.
• It is called when an instance of object is created and memory is allocated
for the object.
• Java compiler creates a default constructor

19
Rules for creating java constructor

• Constructor name must be same as its class name


• Constructor must have no explicit return type

20
Types of Java Constructors

21
Default Constructor
• A constructor is called "Default Constructor" when it doesn't have any
parameter.
• If there is no constructor in a class, compiler automatically creates a
default constructor.

22
Default Constructor
class Bike1
{
Output:
Bike1( )
{

System.out.println("Bike is created"); Bike is created


}

public static void main(String args[ ])


{
Bike1 b=new Bike1( );
}
}
23
Parameterized Constructor
• A constructor which has a specific number of parameters is called
parameterized constructor.
• Parameterized constructor is used to provide different values to the
distinct objects.

24
Output:

111 Karan
Parameterize 222 Aryan
d Constructor

25
Constructor Overloading in Java
• A constructor is just like a method but without return type. It can also be
overloaded like Java methods.
• Constructor overloading in Java is a technique of having more than one
constructor with different parameter lists.
• They are arranged in a way that each constructor performs a different task.
• They are differentiated by the compiler by the number of parameters in
the list and their types.

26
Output:

111 Karan 0
Constructor 222 Aryan 25
Overloading in
Java

27
Java Copy Constructor
• There is no copy constructor in java.
• But, we can copy the values of one object to another like copy constructor
in C++

28
Output:

111 Karan
Java Copy 111 Karan
Constructor

29
Java Package
• A java package is a group of similar types of classes, interfaces and sub-
packages.
• Package in java can be categorized in two form,
• built-in package (java, lang, awt, javax, swing, net, io, util,
sql etc.)
• user-defined package.

30
Package - Example

31
How to access package from another
package?
• There are three ways to access the package from outside the package.
• import package.*;
• import package.classname;
• fully qualified name.

32
33
34
Internationalization
• Internationalization is a List of culturally dependent
mechanism to create data:
such an application that • Messages
can be adapted to • Dates
different languages and • Times
regions. • Numbers
• Currencies
• Measurements
• Phone Numbers
• Postal Addresses 35

• Labels on GUI component


Inheritance
• It is a mechanism in which one object acquires all the properties and
behaviors of parent object.

36
37
Types of Inheritance

38
39
Method Overloading
• If a class has multiple methods having same name but different in
parameters, it is known as Method Overloading.
• There are two ways to overload the method in java
1. By changing number of arguments
2. By changing the data type

40
1) Method Overloading: changing no. of
arguments

Output:

22 33

41
2) Method Overloading: changing data type
of arguments

42
// Java Program to Demonstrate Nested class
class Main {
class Outer
{ public static void main(String[] args)
{
class Inner Outer.Inner in = new Outer().new
{ Inner();
in.show();
public void show() }
{ }
System.out.println("In a nested class
method");
}
}
}

43
Exception Handling
• The exception handling in java is one
of the powerful mechanism to handle
the runtime errors so that normal flow
of the application can be maintained.
• Examples for runtime errors:
ClassNotFound, IO, SQL, Remote etc.

44
45
Common scenarios where exceptions may
occur

46
Common scenarios where exceptions may
occur

47
Exception Handling Keywords
• There are 5 keywords used in java exception handling.
1. try
2. catch
3. finally
4. throw
5. throws

48
Example: Exception Handling

49
throw keyword
• The Java throw keyword is used to explicitly throw an exception.

50
throw keyword example

51
finally block
• Java finally block is a block that
is used to execute important
code such as closing connection,
stream etc.
• Java finally block is always
executed whether exception is
handled or not.
• Java finally block follows try or
catch block.
52
Multithreading
• Multithreading in java is a process of executing multiple threads
simultaneously.
• Thread is basically a lightweight sub-process, a smallest unit of processing.
Multiprocessing and multithreading, both are used to achieve multitasking.
• But we use multithreading than multiprocessing because threads share a
common memory area.
• They don't allocate separate memory area so saves memory, and context-
switching between the threads takes less time than process.
• Java Multithreading is mostly used in games, animation etc. 53
Thread
• A thread is a lightweight sub
process, a smallest unit of
processing. It is a separate path of
execution.
• Threads are independent, if there
occurs exception in one thread, it
doesn't affect other threads.
• It shares a common memory area.
54
Life cycle of a Thread (Thread States)
• The life cycle of the thread in java is
controlled by JVM. The java thread
states are as follows:
1. New
2. Runnable
3. Running
4. Non-Runnable (Blocked)
5. Terminated
55
Files and Streams
• A stream is a sequence of data.
• In Java a stream is composed of bytes. It's called a stream because it is like a
stream of water that continues to flow.
• In java, 3 streams are created for us automatically. All these streams are
attached with console.
1) System.out: standard output stream
2) System.in: standard input stream
3) System.err: standard error stream 56
OutputStream vs InputStream

57
Java Applet
• Applet is a special type of program that is embedded in the webpage to generate the
dynamic content.
• It runs inside the browser and works at client side.
• Advantage of Applet
• It works at client side so less response time.
• Secured
• It can be executed by browsers running under many plateforms, including Linux, Windows, Mac Os etc.
• Drawback of Applet
• Plugin is required at client browser to execute applet. 58
59
Simple example of Applet by html file

60
Displaying The Image Object On The Java Applet

import java.awt.*; <HTML>


import java.applet.*; <HEAD>
public class DisplayImageInApplet extends Applet <TITLE>Display Image In Applet</TITLE>
{ </HEAD>
Image img; <BODY>
public void init() <APPLET Code="DisplayImageInApplet.class"
{ Width=400 Height=300>
img = </APPLET>
getImage(getCodeBase(),"Images/Juggler.gif"); </BODY>
} </HTML>
public void paint(Graphics g)
{
g.drawImage(img,10,20,this);
}
} 61
Output

62
Java AWT
• Java AWT (Abstract Window Toolkit) is an API to develop GUI or
window-based applications in java.
• Java AWT components are platform-dependent i.e. components are
displayed according to the view of operating system.
• The java.awt package provides classes for AWT api such as TextField,
Label, TextArea, RadioButton, CheckBox, Choice, List etc.

63
Java AWT Hierarchy

64
Definitions
Container
• The Container is a component in AWT that can contain another components like buttons, textfields, labels etc. The
classes that extends Container class are known as container such as Frame, Dialog and Panel.
Window
• The window is the container that have no borders and menu bars. You must use frame, dialog or another window for
creating a window.
Panel
• The Panel is the container that doesn't contain title bar and menu bars. It can have other components like button,
textfield etc.
Frame
• The Frame is the container that contain title bar and can have menu bars. It can have other components like button,
textfield etc. 65
AWT - Example

66
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
• sharing resources
• centralize software management
67
Java Networking Terminology
• The widely used java networking terminologies are given below:
• IP Address
• Protocol
• Port Number
• MAC Address
• Connection-oriented and connection-less protocol
• Socket
68
Java Networking Terminology
1) 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.
2) Protocol
• A protocol is a set of rules basically that is followed for communication. For
example:
• TCP, FTP, Telnet, SMTP, POP etc.
69
Java Networking Terminology
3) 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.
4) 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.
70
Java Networking Terminology
5) 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.
6) Socket
• A socket is an endpoint between two way communication.
71
72
73
74
Output

75
76

You might also like