Java Notes
Java Notes
History of Java
Before learning about Java, first let us know the history of Java. Before 1990,
Sun Microsystems use to maintain two departments, the first department
was known as Sunsoft which was used to deal only with the development of
new software and the other department use to deal with development of
hardware components like processors. The processor developed by Sun is
known as Spark processor. This processor is based on an architechture
known as RISC architechture.
In the year 1990, Sun Microsystems received a software
project by name Green Project (some books say as Stealth project)
from some client. The purpose of this project was to develop software for
consumer electronic devices like televisions, refrigerators, toasters,
microwave ovens, washing machines etc. As we already know that
developing software consists of many stages or phases i.e. Analysis,
Design, Coding, Testing and Maintenance. The job of coding the software
was given to James Gosling. At that time, the languages used to develop
softwares was C and C++. But James Gosling knew that C, C++ comes under
system dependent languages category i.e. he has to develop different
softwares for different electronic devices. In other words, for refrigerators
there was be separate software, for washing machine there was be separate
software, for ovens there was be separate software i.e. for different
electronic devices there must exists different softwares. Then he thought
that there is a need to develop a new language that can solve the problem of
system dependency.
So, he started to develop a new language, which was initially
known as Oak (It is the name of the tallest tree in America. It is said that
there was an Oak tree, in the cottage where James started developing a new
language). Later the name changed to Java. This language could
successfully solve the problem of system dependency. Here we can conclude
that Java was introduced to develop software that can run on any electronic
device. It is said that Java was introduced in the year 1991 by James Gosling,
but was announced to public in the year 1995 in a conference. In the
year 1996, the first version of Java i.e. JDK 1.0 was released.
During 1990s, Internet and World Wide Web became popular
because they can be accessible from any computer. Then they started
thinking what will be the effect of Java on Internet. By keeping this in mind,
These notes are dedicated to my father Mr. Mir Farooq Ali, Professor and
Ex-Head of the Department of Mathematics (Retired), MJCET and my mom
Mrs. Arifa, Vice Principal and Junior Lecturer in Botany (Retired) Govt.
Falaknuma Junior College. If you have any doubts in this document, you
can call me on 9010149047 or mail me at [email protected],
your queries will be replied within 24 hours.
PROGRAMMING Prepared by Mir Ahmed Ali (Ali Sir),
IN JAVA UNIT I Assistant Professor of CSE, MJCET
2
they were successful in making impact of Java on Internet. The various java
programs that can work on Internet were applets and servlets.
Creation of Java:
These notes are dedicated to my father Mr. Mir Farooq Ali, Professor and
Ex-Head of the Department of Mathematics (Retired), MJCET and my mom
Mrs. Arifa, Vice Principal and Junior Lecturer in Botany (Retired) Govt.
Falaknuma Junior College. If you have any doubts in this document, you
can call me on 9010149047 or mail me at [email protected],
your queries will be replied within 24 hours.
PROGRAMMING Prepared by Mir Ahmed Ali (Ali Sir),
IN JAVA UNIT I Assistant Professor of CSE, MJCET
3
operating system is changed then the above code never gets executed.
These programs are known as system dependent programs or platform
dependent programs. Hence C and C++ are known as system dependent
languages or platform dependent languages.
These notes are dedicated to my father Mr. Mir Farooq Ali, Professor and
Ex-Head of the Department of Mathematics (Retired), MJCET and my mom
Mrs. Arifa, Vice Principal and Junior Lecturer in Botany (Retired) Govt.
Falaknuma Junior College. If you have any doubts in this document, you
can call me on 9010149047 or mail me at [email protected],
your queries will be replied within 24 hours.
PROGRAMMING Prepared by Mir Ahmed Ali (Ali Sir),
IN JAVA UNIT I Assistant Professor of CSE, MJCET
4
system. In other words, the JVM working for Windows operating system is
different from the JVM working for Linux operating system. As we know that
whenever user compiles a java program, it generates a code known as byte
code. This code is system independent or platform independent. A byte code
that is generated on a computer that is having windows operating system
and Pentium processor can run on a computer that is having Sun Solaris
operating system and Spark processor. In other words, a byte code
generated on one computer having X operating system and Y hardware can
run on a computer that is having Y operating system and X hardware. To be
more specific, a byte code generated on a computer that is having any
operating system and any processor can run on a computer that is having
any operating system and any hardware. This concept made Java to be
known as Platform Independent Language.
If we consider an Internet, that contains millions
of computers having different operating system and different processors and
if we want to run the java byte code, then user can expect same result on all
the systems. If user wants to develop a software using Java programming
language, and wants to send this software to some other person in some
other part of world, then the user can just send .class file containing of byte
code and that person can download this file( .class file) from internet and can
execute this file without any problem. Hence we can conclude that java is
suitable for developing software on the internet.
Applets:
Servlets:
These notes are dedicated to my father Mr. Mir Farooq Ali, Professor and
Ex-Head of the Department of Mathematics (Retired), MJCET and my mom
Mrs. Arifa, Vice Principal and Junior Lecturer in Botany (Retired) Govt.
Falaknuma Junior College. If you have any doubts in this document, you
can call me on 9010149047 or mail me at [email protected],
your queries will be replied within 24 hours.
PROGRAMMING Prepared by Mir Ahmed Ali (Ali Sir),
IN JAVA UNIT I Assistant Professor of CSE, MJCET
5
Servlets are defined as a java program that gets executed on the server side
of the Web Connection.
Byte Code:
A byte code is defined as a code that gets generated when a java program
gets compiled. In other words, whenever user wants to compile a program
with the help of a system program known as Java Compiler, then its job is to
convert the java source code into byte code. This code is named as byte
code, because the size of each instruction is 1 byte that is equal to 8 bits.
Practically, this byte code is present in a file known as .class file. This code
that is being generated neither depends on the operating system nor on the
processor that is present in the computer, on which the program was
compiled. This byte code can be executed on any computer that is having
different operating system and different processor. JVM software for Windows
operating system is different from the JVM software for Linux operating
system that is different from the JVM software for Sun Solaris operating
system. This new concept of byte code made Java famous to be known as a
Platform Independent Language. The below diagram shows how a byte
code gets generated.
1. Simple
2. Object Oriented
3. Secure
4. Portable
These notes are dedicated to my father Mr. Mir Farooq Ali, Professor and
Ex-Head of the Department of Mathematics (Retired), MJCET and my mom
Mrs. Arifa, Vice Principal and Junior Lecturer in Botany (Retired) Govt.
Falaknuma Junior College. If you have any doubts in this document, you
can call me on 9010149047 or mail me at [email protected],
your queries will be replied within 24 hours.
PROGRAMMING Prepared by Mir Ahmed Ali (Ali Sir),
IN JAVA UNIT I Assistant Professor of CSE, MJCET
6
5. Interpreted
6. Architechture Neutral
7. Multithreaded
8. Distributed
9. Dynamic
10. High Performance
The above features that made Java as a popular language are explained
below:
Java is Simple
For a learner or a programmer the most difficult concepts in C and C++ are
pointers, structures, unions. Pointers are useful in some applications, but if
they are not used properly they result in unpredictable and undetectable
results. The most confusing concepts in C++ is operator overloading. Hence
Java has removed all complex issues making it more simple. Moreover Java
was designed to be easy for the professional programmers so that they can
learn and can use it efficiently. If a person is very good at C++, then learning
Java becomes very easy i.e. at that time when Java was introduced, many
programmers were working with C++.
Java is Interpreted
Java is said to be Interpreted i.e. java programs are compiled into a code
known as byte code for an imaginary machine known as virtual machine. Any
machine that supports java programs provides a simulator known as
interpreter that could read byte code and convert it into machine code and
then execute them to give output. This interpreter is known as Java Virtual
Machine. So the job of JVM is to convert byte code to machine code and then
to execute this machine code to give output. So, by doing this the
performance of Java gets degraded i.e. it leads to slower execution.
These notes are dedicated to my father Mr. Mir Farooq Ali, Professor and
Ex-Head of the Department of Mathematics (Retired), MJCET and my mom
Mrs. Arifa, Vice Principal and Junior Lecturer in Botany (Retired) Govt.
Falaknuma Junior College. If you have any doubts in this document, you
can call me on 9010149047 or mail me at [email protected],
your queries will be replied within 24 hours.
PROGRAMMING Prepared by Mir Ahmed Ali (Ali Sir),
IN JAVA UNIT I Assistant Professor of CSE, MJCET
7
Java is Robust
Java is said to be Robust, because java programs never crash if there are any
software and hardware errors. The reason is that Java follows exception
handling. A java program has some statements and if these statements lead
to some error like division by zero, file operation that can attempt to read
data from non-existent source, then it will generate an exception instead of
performing an erroneous operation.
Another reason why Java is said to be robust, is automatic
memory management. In case of C and C++, it is the responsibility of the
programmer to write the functions in their programs that are used to release
the memory that was used by the variables and object of the program. But in
case of Java, the programmer need not feel burden of releasing the memory
but it is the responsibility of a special program known as Garbage Collector
is to detect and release the memory that is no longer used by the currently
running program.
The above two features Exceptional Handling and use of
Garbage Collector simplifies the programmer’s task and makes programs
more reliable.
As we know, that the byte code generated by the Java Compiler, is neither
dependent on the computer architechture of the system (where it was
generated) nor the operating system of the computer. Computer
Architechture means the various components of the computer are connected
to each other. In some books, architechture is nothing but the processor and
the other components of the computer connected to each other to work with
each other to perform a simple task. The processors can be Intel, AMD or
Spark.
With this feature, there is a famous
saying on Java: “write once, run anywhere, any time, forever”.
These notes are dedicated to my father Mr. Mir Farooq Ali, Professor and
Ex-Head of the Department of Mathematics (Retired), MJCET and my mom
Mrs. Arifa, Vice Principal and Junior Lecturer in Botany (Retired) Govt.
Falaknuma Junior College. If you have any doubts in this document, you
can call me on 9010149047 or mail me at [email protected],
your queries will be replied within 24 hours.
PROGRAMMING Prepared by Mir Ahmed Ali (Ali Sir),
IN JAVA UNIT I Assistant Professor of CSE, MJCET
8
Java is Multithreaded
Java was the first programming language that introduced the concept of
multithreaded i.e. they can be multiple threads in a single program so that
they can perform many things simultaneously. Creating multiple threads, is
known as multithreading.
Java is Distributed
Java is Portable
If a Java program gives same results on any machine regardless of the type
of operating system and the type of computer architechture, then it is said to
be portable.
Java is Dynamic
Before Java, people used to write web pages with the help of HTML. But
viewing, data with the help of HTML gives an impression of static data
getting displayed on a web browser. But with the help of applets,
dynamically interacting programs with animations were made, on internet.
The problem with previous JVM was slow execution. This made java programs
low performance. To increase their performance, a system program by name
JIT Compiler was introduced in JVM that increased the performance of Java
and that made JIT Compiler and JVM to work together.
OOP Principles:
These notes are dedicated to my father Mr. Mir Farooq Ali, Professor and
Ex-Head of the Department of Mathematics (Retired), MJCET and my mom
Mrs. Arifa, Vice Principal and Junior Lecturer in Botany (Retired) Govt.
Falaknuma Junior College. If you have any doubts in this document, you
can call me on 9010149047 or mail me at [email protected],
your queries will be replied within 24 hours.
PROGRAMMING Prepared by Mir Ahmed Ali (Ali Sir),
IN JAVA UNIT I Assistant Professor of CSE, MJCET
9
1. Encapsulation
2. Inheritance
3. Polymorphism
Encapsulation:
It is defined as a mechanism that associates the code and the data into a
single unit to and keeps them safe from external interference and misuse. In
other words, it is a wrapper that wraps code and data into one unit.
Practically, this is implemented with the help of a
construct known as a class. A class is defined as a template that has a
structure and behaviour that is shared by a group of objects. Each object of a
class contains structure and behaviour of a class i.e. why sometimes objects
are known as instance of a class. A class contains variables and methods.
These are known as members of the class. Variables of a class explains the
structure of a class, where as methods of a class explains the behaviour of a
class. In Java, they are known as methods where as in C and C++ they are
known as functions.
With the help of encapsulation we can protect the data present in
instance variables and method body. But this can be implemented with the
help of access specifiers. They are three access specifiers as shown below:
public
private
protected
Inheritance:
These notes are dedicated to my father Mr. Mir Farooq Ali, Professor and
Ex-Head of the Department of Mathematics (Retired), MJCET and my mom
Mrs. Arifa, Vice Principal and Junior Lecturer in Botany (Retired) Govt.
Falaknuma Junior College. If you have any doubts in this document, you
can call me on 9010149047 or mail me at [email protected],
your queries will be replied within 24 hours.
PROGRAMMING Prepared by Mir Ahmed Ali (Ali Sir),
IN JAVA UNIT I Assistant Professor of CSE, MJCET
10
the features of a son are much similar of that his father. This concept is
known as Inheritance.
In Programming, the basic definition of
inheritance is Reusability. The concept of inheritance was first introduced by
a programming language known as Smalltalk. If Java is using inheritance
concept, then they are two types of classes i.e. sub class and super class. A
sub class is a more specialized versions of a class, which inherit attributes
and behaviors from their parent class. In other words, this sub class contains
attributes and behaviours of parent class and it has capability to define on its
own.
With in a class hierarchy, it is also possible for
subclasses to override any methods that they have inherited, and they can
create unique implementations for these methods. Moreover, if user wants to
change the body of a method in a sub class that is inherited from the super
class, then it is possible with the help of a concept known as “Method
Overriding”.
Polymorphism
A variable is defined as a storage area that is used to store data. Each and
every variable has a scope and a lifetime. Let us consider a small example. If
user is interested to write a program i.e. addition of two numbers, say 2 and
3. Then there is a necessity to store the above values. Here the variable
comes into being. We use variable to store this values i.e. 2 is stored in one
variable and 3 is stored in another variable.
If user wants to use a variable, while writing a java program, then it is
mandatory to declare that variable before it is used.
Declaring a Variable:
These notes are dedicated to my father Mr. Mir Farooq Ali, Professor and
Ex-Head of the Department of Mathematics (Retired), MJCET and my mom
Mrs. Arifa, Vice Principal and Junior Lecturer in Botany (Retired) Govt.
Falaknuma Junior College. If you have any doubts in this document, you
can call me on 9010149047 or mail me at [email protected],
your queries will be replied within 24 hours.
PROGRAMMING Prepared by Mir Ahmed Ali (Ali Sir),
IN JAVA UNIT I Assistant Professor of CSE, MJCET
11
Being a java programmer, if you want to say the operating system to reserve
some space in the main memory in order to store some data, then it is
possible with the help of a statement known as declaring a variable .The
syntax to declare a variable is
The datatype explains the type of data that can be stored in that memory
location. The identifier is defined as the rules that are used to name a
variable. To be more specific, identifier is the name that we use with in the
program that refers to that memory location where the data is stored.
Whenever user refers this identifier in the program, the operating system will
look up the memory address that corresponds the identifier and accesses the
memory address. This concept is known as resolving. Now the last part is
nothing but the right hand side of the assignment operator can be the value
or an expression.
Identifier
Identifier is nothing but some rules that define the name of a variable or a
class or an interface or a method or an array or an object. The name that the
programmer wants to select must represent the nature of data that is stored
in the corresponding memory location. The below are some restrictions that
must be applied while choosing an identifier:
While using an identifier, it must be noted that it must be unique with in the
block where it is declared i.e. programmer cannot declare two variables with
same name within the same block otherwise it will lead to a compile time
error. However the programmer can declare same identifier in different
blocks.
HelloWorld.java
These notes are dedicated to my father Mr. Mir Farooq Ali, Professor and
Ex-Head of the Department of Mathematics (Retired), MJCET and my mom
Mrs. Arifa, Vice Principal and Junior Lecturer in Botany (Retired) Govt.
Falaknuma Junior College. If you have any doubts in this document, you
can call me on 9010149047 or mail me at [email protected],
your queries will be replied within 24 hours.
PROGRAMMING Prepared by Mir Ahmed Ali (Ali Sir),
IN JAVA UNIT I Assistant Professor of CSE, MJCET
12
Let's look into our first program. Save the following text into the file called
HelloWorld.java
class Hello
System.out.println("Hello World");
We examine that there is a class called Hello. Since Hello is the only class in
the file, it is called the main class. In order to make your program runnable,
you should have a function called main which is defined as:
The public qualifier tells you that the function main is callable from outside.
Then the next qualifier is static. We will study about static in near future.
Then, void tells you that the function main returns nothing to the system.
Then String[] args denotes the argument passed into the program.
These notes are dedicated to my father Mr. Mir Farooq Ali, Professor and
Ex-Head of the Department of Mathematics (Retired), MJCET and my mom
Mrs. Arifa, Vice Principal and Junior Lecturer in Botany (Retired) Govt.
Falaknuma Junior College. If you have any doubts in this document, you
can call me on 9010149047 or mail me at [email protected],
your queries will be replied within 24 hours.
PROGRAMMING Prepared by Mir Ahmed Ali (Ali Sir),
IN JAVA UNIT I Assistant Professor of CSE, MJCET
13
Arguments are nothing but the options or switches you would mention when
invoking programs.
NOTE:
The main class name has to be the same as the program file name.
Otherwise, your Java program won't compile. In this example the class name
is Hello. The program file has to be Hello.java. You have to pay attention
that this naming convention is CASE SENSITIVE it means that
naming your file into helloworld.java WON'T work.
If user wants to compile a java program, then it is possible with the help of a
command known as javac. This command internally calls Java Compiler,
which will create a new file that contains the byte code version of the
source code and the name of the file will be same as source code file with an
extension of .class .Don't forget to change the directory to the directory
where your Java program resides. Also, make sure that your JDK installation
is correct.
javac Hello.java
When the above program gets executed, they are two expectations. The first
is it produces a file called Hello.class, it means that your compilation is
successful. The second is gives compile time errors, if they exists with in the
program, then no .class file gets created. It must be noted that a .class
file gets created, if and only if they are no compile time errors.
These notes are dedicated to my father Mr. Mir Farooq Ali, Professor and
Ex-Head of the Department of Mathematics (Retired), MJCET and my mom
Mrs. Arifa, Vice Principal and Junior Lecturer in Botany (Retired) Govt.
Falaknuma Junior College. If you have any doubts in this document, you
can call me on 9010149047 or mail me at [email protected],
your queries will be replied within 24 hours.
PROGRAMMING Prepared by Mir Ahmed Ali (Ali Sir),
IN JAVA UNIT I Assistant Professor of CSE, MJCET
14
java Hello
On writing the above line, it must be noted that there must be no .class
extension. When the above command gets executed, internally the java
command will call for JVM that will convert the byte code into suitable
machine instructions that can be understandable by the processor present in
the system on which the program is getting executed. After converting the
processor will act on that statements and then user can see output. Some
times if there is some error during execution or runtime, then instead of
displaying output, it displays user understandable messages known as
Exceptions.
NOTE:
It is said to be a good practice, if you write comments beside the source code
of the program. In real time industry, you cannot find software or program
without comments. Sometimes, it is useful to make some note about your
program. Not only making you clear about what's going on your program,
but also making others able to read your program based on the comments of
your program. The compiler will completely ignore your comment, however.
In Java, there are three ways of commenting:
Example:
// Put your comments here
After you put the double slash (//), everything else behind it is considered as
comment.
These notes are dedicated to my father Mr. Mir Farooq Ali, Professor and
Ex-Head of the Department of Mathematics (Retired), MJCET and my mom
Mrs. Arifa, Vice Principal and Junior Lecturer in Botany (Retired) Govt.
Falaknuma Junior College. If you have any doubts in this document, you
can call me on 9010149047 or mail me at [email protected],
your queries will be replied within 24 hours.
PROGRAMMING Prepared by Mir Ahmed Ali (Ali Sir),
IN JAVA UNIT I Assistant Professor of CSE, MJCET
15
Embracing comment.
Example:
/*
My comment is here
Neat huh?
*/
If your comment is pretty long, it is better to embrace it with /* and */.
Everything within it is considered as a comment.
These notes are dedicated to my father Mr. Mir Farooq Ali, Professor and
Ex-Head of the Department of Mathematics (Retired), MJCET and my mom
Mrs. Arifa, Vice Principal and Junior Lecturer in Botany (Retired) Govt.
Falaknuma Junior College. If you have any doubts in this document, you
can call me on 9010149047 or mail me at [email protected],
your queries will be replied within 24 hours.
PROGRAMMING Prepared by Mir Ahmed Ali (Ali Sir),
IN JAVA UNIT I Assistant Professor of CSE, MJCET
16
The primitive datatypes are categorized into two categories as higher types
and the second is lower types. The higher types includes datatypes that
contains a large number and their size is also large. The lower type include
datatypes that contains a small number and their size is also small. The size
of the datatype means amount of memory allocated to the variable
belonging to that datatype. For example, in case of character datatype, the
memory allocated will be 16-bits where as in case of long datatype, the
memory allocated will be 64-bits. In typecasting, we can convert a higher
type to lower type and viceversa. But we cannot convert boolean datatype
into either higher type or lower type.
byteshortcharintlongfloatdouble
Widening:
These notes are dedicated to my father Mr. Mir Farooq Ali, Professor and
Ex-Head of the Department of Mathematics (Retired), MJCET and my mom
Mrs. Arifa, Vice Principal and Junior Lecturer in Botany (Retired) Govt.
Falaknuma Junior College. If you have any doubts in this document, you
can call me on 9010149047 or mail me at [email protected],
your queries will be replied within 24 hours.
PROGRAMMING Prepared by Mir Ahmed Ali (Ali Sir),
IN JAVA UNIT I Assistant Professor of CSE, MJCET
17
Narrowing:
These notes are dedicated to my father Mr. Mir Farooq Ali, Professor and
Ex-Head of the Department of Mathematics (Retired), MJCET and my mom
Mrs. Arifa, Vice Principal and Junior Lecturer in Botany (Retired) Govt.
Falaknuma Junior College. If you have any doubts in this document, you
can call me on 9010149047 or mail me at [email protected],
your queries will be replied within 24 hours.
PROGRAMMING Prepared by Mir Ahmed Ali (Ali Sir),
IN JAVA UNIT I Assistant Professor of CSE, MJCET
18
circular brace and closing of circular brace and in the middle it contains data
type that user is interested to cast. In case of narrowing, if the programmer
is not using any cast operator, then there is a compile time error. In other
words, Java Compiler does not take any responsibility to perform
typecasting. In other words, the typecasting responsibility must be taken by
the programmer itself. That is why, it is also known as Explicit
typecasting.
Practice Codes:
These notes are dedicated to my father Mr. Mir Farooq Ali, Professor and
Ex-Head of the Department of Mathematics (Retired), MJCET and my mom
Mrs. Arifa, Vice Principal and Junior Lecturer in Botany (Retired) Govt.
Falaknuma Junior College. If you have any doubts in this document, you
can call me on 9010149047 or mail me at [email protected],
your queries will be replied within 24 hours.
PROGRAMMING Prepared by Mir Ahmed Ali (Ali Sir),
IN JAVA UNIT I Assistant Professor of CSE, MJCET
19
char ch='A';
int num;
num = ch;
System.out.println(num);
}
}
Class
class ClassName {
When a class gets loaded into JVM, there exists some priority to execute the
above mentioned members. The priority is listed in ascending order as
shown below:
These notes are dedicated to my father Mr. Mir Farooq Ali, Professor and
Ex-Head of the Department of Mathematics (Retired), MJCET and my mom
Mrs. Arifa, Vice Principal and Junior Lecturer in Botany (Retired) Govt.
Falaknuma Junior College. If you have any doubts in this document, you
can call me on 9010149047 or mail me at [email protected],
your queries will be replied within 24 hours.
PROGRAMMING Prepared by Mir Ahmed Ali (Ali Sir),
IN JAVA UNIT I Assistant Professor of CSE, MJCET
20
1. Static Variables
2. Static Methods
3. Static Block
4. public static void main(String a[]) method
5. Instance Variables
6. Constructors
7. Instance Methods
8. Local Variables
In the above class, we can see two members . The first is instance variables
i.e. we have two instance variables and the second is instance method. The
two instance variables are x and y and the instance method is
display method. So, we can conclude that any variable present in a class is
known as instance variables and any method present in a class is known as
instance method. The access specifier can be public, protected and
default. If the programmer has not mention the access-specifier while writing
the program, then the access specifier is default access specifier. The only
access specifier that cannot be used is private. If private access specifier is
used then we will get a compile time error as Illegal access specifier. The
keyword field can contain keywords like final and abstract. Depending on
the type of environment, the keyword can be either final or abstract. The
ClassName must reflect the type of code that is written inside the class.
Method
As we know, that the job of a method is to represent functionality or
behaviour of a class. In Java, a method consists of 6 parts as shown below:
1. Access Specifier
2. keyword
These notes are dedicated to my father Mr. Mir Farooq Ali, Professor and
Ex-Head of the Department of Mathematics (Retired), MJCET and my mom
Mrs. Arifa, Vice Principal and Junior Lecturer in Botany (Retired) Govt.
Falaknuma Junior College. If you have any doubts in this document, you
can call me on 9010149047 or mail me at [email protected],
your queries will be replied within 24 hours.
PROGRAMMING Prepared by Mir Ahmed Ali (Ali Sir),
IN JAVA UNIT I Assistant Professor of CSE, MJCET
21
3. Return Datatype
4. Method Name
5. Parameters
6. Method Body
Access Specifier:
The first field is the access specifier. The access specifier defines or controls
the access of the method, i.e. it explains how it can allow other objects to
access this method. They are various access specifiers in Java. Let us
consider only two in order to understand the concept. If we consider access
specifier as public, then any object can access this method. But if we
consider access specifier as private, then only the object that is present in
the class where the method body is present can access i.e. objects from
other class cannot access this method.
Keyword:
The second field is optional. They are many types of keywords available i.e.
depending on the type of environment keywords gets varied. The keywords
are static, final, abstract and so on. If user wants to make a method as static,
then the developer has to write this keyword static before the name of the
method. If the method is static, then it has its own importance and own
meaning. But if the method is final, then it has its own meaning. In other
words, depending on the type of requirement, different type of keyword is
used.
Return Datatype:
The third field is return datatype. As we know that a method contains some
code and if this code gets executed, it performs some task. Each and every
These notes are dedicated to my father Mr. Mir Farooq Ali, Professor and
Ex-Head of the Department of Mathematics (Retired), MJCET and my mom
Mrs. Arifa, Vice Principal and Junior Lecturer in Botany (Retired) Govt.
Falaknuma Junior College. If you have any doubts in this document, you
can call me on 9010149047 or mail me at [email protected],
your queries will be replied within 24 hours.
PROGRAMMING Prepared by Mir Ahmed Ali (Ali Sir),
IN JAVA UNIT I Assistant Professor of CSE, MJCET
22
Method Name:
The fourth field is method name. The Method Name is used to identify a
method. The name of the method must reflect the type of code developer is
writing with in the body of the method.
Parameters
Method Body
Constructor
These notes are dedicated to my father Mr. Mir Farooq Ali, Professor and
Ex-Head of the Department of Mathematics (Retired), MJCET and my mom
Mrs. Arifa, Vice Principal and Junior Lecturer in Botany (Retired) Govt.
Falaknuma Junior College. If you have any doubts in this document, you
can call me on 9010149047 or mail me at [email protected],
your queries will be replied within 24 hours.
PROGRAMMING Prepared by Mir Ahmed Ali (Ali Sir),
IN JAVA UNIT I Assistant Professor of CSE, MJCET
23
1. The constructor name must be same as the name of the class in which
it is present.
2. A constructor must not have any return datatype i.e. a constructor will
never return a value, nor even void. If user writes a return datatype,
then it may lead to compile time error.
3. A constructor may or may not have parameters.
4. A constructor is called and executed only once per object. In other
words, if user creates an object, then constructor is called. If user
creates 10 objects, then the constructor is called 10 times.
As soon as the new operator gets executed, memory is created on heap. The
size of memory that gets created on heap depends on number of instance
variables and instance methods present in a class. After execution of new
operator, JVM will call for the constructor. After finding the constructor, JVM
will execute the constructor, and the values present in the objects gets
initialized.
In short , a constructor is defined as a method
that is automatically invoked when an instance of a class is created.
1. Default Constructor
2. Zero Parameter Constructor
3. Parameterized Constructor
4. Copy Constructor
Default Constructor:
If the developer doesn’t define a constructor, then the JVM will provide a
default constructor or do-nothing constructor. The job of this constructor is to
assign null values to instance variables present in the object. If the variables
are of type integer, then it assign value as 0, and if the variables are of type
float or double, then it assign value as 0.0f and 0.0 and if the value are of
type String, then it assign value as null.
These notes are dedicated to my father Mr. Mir Farooq Ali, Professor and
Ex-Head of the Department of Mathematics (Retired), MJCET and my mom
Mrs. Arifa, Vice Principal and Junior Lecturer in Botany (Retired) Govt.
Falaknuma Junior College. If you have any doubts in this document, you
can call me on 9010149047 or mail me at [email protected],
your queries will be replied within 24 hours.
PROGRAMMING Prepared by Mir Ahmed Ali (Ali Sir),
IN JAVA UNIT I Assistant Professor of CSE, MJCET
24
In the above example, the developer has define a class known as Default
Constructor. This class is having two instance variables, one instance method
and one main method i.e. public static void main(String args[]). When the
first statements in main method, gets executed a reference variable by
name dc is created on the stack, and then the new operator creates a
memory on the heap. After creating the memory, the JVM will search for the
constructor. But in the program, the developer has not written the body of
zero parameter constructor. In this case, the JVM will not give any run-time
error, but internally calls for a constructor known as Default Constructor. It
means that default constructor is called , if the developer has not provided
the body of zero parameter constructor. But if we assume, that the user has
provided the body of zero parameter constructor then the JVM will not call
Default Constructor.
Zero Parameter Constructor:
These notes are dedicated to my father Mr. Mir Farooq Ali, Professor and
Ex-Head of the Department of Mathematics (Retired), MJCET and my mom
Mrs. Arifa, Vice Principal and Junior Lecturer in Botany (Retired) Govt.
Falaknuma Junior College. If you have any doubts in this document, you
can call me on 9010149047 or mail me at [email protected],
your queries will be replied within 24 hours.
PROGRAMMING Prepared by Mir Ahmed Ali (Ali Sir),
IN JAVA UNIT I Assistant Professor of CSE, MJCET
25
x=6;
y=9;
}
public static void main(String[] args) {
ZeroPC zpc = new ZeroPC();
System.out.println(zpc.x);
System.out.println(zpc.y);
}
}
When the above program gets executed, the JVM will search for the Zero
Parameter Constructor. In this program, the developer has provided a zero
parameter constructor, which is executed and the values that are present
with in the zero parameter constructor gets initialized to instance variables
that are present on the heap. But if user has not provided the zero
parameter constructor, then the JVM will call default constructor.
Copy Constructor:
A copy constructor is one which takes only one argument i.e. a reference to
an object of same class to which the constructor belongs.
Parameterized Constructor:
class Person{
int age;
String name;
}
These notes are dedicated to my father Mr. Mir Farooq Ali, Professor and
Ex-Head of the Department of Mathematics (Retired), MJCET and my mom
Mrs. Arifa, Vice Principal and Junior Lecturer in Botany (Retired) Govt.
Falaknuma Junior College. If you have any doubts in this document, you
can call me on 9010149047 or mail me at [email protected],
your queries will be replied within 24 hours.
PROGRAMMING Prepared by Mir Ahmed Ali (Ali Sir),
IN JAVA UNIT I Assistant Professor of CSE, MJCET
26
}
}
When the above program gets executed, the JVM will see whether the
programmer has provided the body of the parameterized constructor. In this
program, we using two parameterized constructor. As the programmer has
supplied the body of the parameterized constructor, the JVM during
execution of the program will initialize the values of instance variables with
the values that are provided by the programmer. But if the programmer does
not provide the body of parameterized constructor, then there will be
compile time error stating the body of parameterized constructor is not
defined. It must be noted that, the JVM will not call any default constructor.
In other words, a default constructor is called by JVM , if the programmer
does not supply the body of zero parameter constructor only.
Constructor Overloading
These notes are dedicated to my father Mr. Mir Farooq Ali, Professor and
Ex-Head of the Department of Mathematics (Retired), MJCET and my mom
Mrs. Arifa, Vice Principal and Junior Lecturer in Botany (Retired) Govt.
Falaknuma Junior College. If you have any doubts in this document, you
can call me on 9010149047 or mail me at [email protected],
your queries will be replied within 24 hours.
PROGRAMMING Prepared by Mir Ahmed Ali (Ali Sir),
IN JAVA UNIT I Assistant Professor of CSE, MJCET
27
y=12;
}
ConstructorOverlaoding(int a){
x=a;
}
ConstructorOverlaoding(int a,int b){
x=a;
y=b;
}
public static void main(String[] args) {
The word polymorphism was introduced by the people of Greek. The word
poly means many and morphism means forms i.e. many forms. This term is
used by the developers to describe a method to have multiple definitions.
Java implements this theoretical concept, by enabling
programmers to overload a method. A programmer overloads a method by
defining two or more methods using the same name, but differs in their
signatures either in type of parameters or in the number of parameters. The
reason of using method overloading is it make easier to use methods for a
programmer because the programmer doesn’t have to remember a lot of
method names. The only thing the programmer must remember is the
method name and number of parameters it holds.
These notes are dedicated to my father Mr. Mir Farooq Ali, Professor and
Ex-Head of the Department of Mathematics (Retired), MJCET and my mom
Mrs. Arifa, Vice Principal and Junior Lecturer in Botany (Retired) Govt.
Falaknuma Junior College. If you have any doubts in this document, you
can call me on 9010149047 or mail me at [email protected],
your queries will be replied within 24 hours.
PROGRAMMING Prepared by Mir Ahmed Ali (Ali Sir),
IN JAVA UNIT I Assistant Professor of CSE, MJCET
28
this keyword:
But if user has used same name for both local and
instance variable and if user wants to access the instance variable, then it is
possible with the help of this keyword.
By doing this, we can inform the JVM to access the value
of current instance variable. The below program is used to illustrate this
keyword.
These notes are dedicated to my father Mr. Mir Farooq Ali, Professor and
Ex-Head of the Department of Mathematics (Retired), MJCET and my mom
Mrs. Arifa, Vice Principal and Junior Lecturer in Botany (Retired) Govt.
Falaknuma Junior College. If you have any doubts in this document, you
can call me on 9010149047 or mail me at [email protected],
your queries will be replied within 24 hours.
PROGRAMMING Prepared by Mir Ahmed Ali (Ali Sir),
IN JAVA UNIT I Assistant Professor of CSE, MJCET
29
Use of final with data:If a variable is declared as final, then the value
of the variable can never be altered. That is , with the help of final keyword,
we can make the value of a variable as constant. If anyone wants to change
the value, then it leads to compile time error. The syntax to declare a
variable as final is:
The below program illustrate the concept of final with variables that contains
data:
Recursion:
These notes are dedicated to my father Mr. Mir Farooq Ali, Professor and
Ex-Head of the Department of Mathematics (Retired), MJCET and my mom
Mrs. Arifa, Vice Principal and Junior Lecturer in Botany (Retired) Govt.
Falaknuma Junior College. If you have any doubts in this document, you
can call me on 9010149047 or mail me at [email protected],
your queries will be replied within 24 hours.
PROGRAMMING Prepared by Mir Ahmed Ali (Ali Sir),
IN JAVA UNIT I Assistant Professor of CSE, MJCET
30
Garbage Collection:
The JVM's heap stores all objects created by an executing Java program.
Objects are created by Java's "new" operator, and memory for new objects is
allocated on the heap at run time. Garbage collection is the process of
automatically freeing objects that are no longer referenced by the program.
This frees the programmer from having to keep track of when to free
allocated memory, thereby preventing many potential bugs and headaches.
The name "garbage collection" implies that objects that are no longer
needed by the program are "garbage" and can be thrown away. A more
accurate and up-to-date metaphor might be "memory recycling." When an
object is no longer referenced by the program, the heap space it occupies
must be recycled so that the space is available for subsequent new objects.
The garbage collector must somehow determine which objects are no longer
referenced by the program and make available the heap space occupied by
such unreferenced objects.
These notes are dedicated to my father Mr. Mir Farooq Ali, Professor and
Ex-Head of the Department of Mathematics (Retired), MJCET and my mom
Mrs. Arifa, Vice Principal and Junior Lecturer in Botany (Retired) Govt.
Falaknuma Junior College. If you have any doubts in this document, you
can call me on 9010149047 or mail me at [email protected],
your queries will be replied within 24 hours.
PROGRAMMING Prepared by Mir Ahmed Ali (Ali Sir),
IN JAVA UNIT I Assistant Professor of CSE, MJCET
31
Any garbage collection algorithm must do two basic things. First, it must
detect garbage objects. Second, it must reclaim the heap space used by the
garbage objects and make it available to the program. Garbage detection is
ordinarily accomplished by defining a set of roots and determining
reachability from the roots. An object is reachable if there is some path of
references from the roots by which the executing program can access the
object. The roots are always accessible to the program. Any objects that are
reachable from the roots are considered live. Objects that are not reachable
These notes are dedicated to my father Mr. Mir Farooq Ali, Professor and
Ex-Head of the Department of Mathematics (Retired), MJCET and my mom
Mrs. Arifa, Vice Principal and Junior Lecturer in Botany (Retired) Govt.
Falaknuma Junior College. If you have any doubts in this document, you
can call me on 9010149047 or mail me at [email protected],
your queries will be replied within 24 hours.
PROGRAMMING Prepared by Mir Ahmed Ali (Ali Sir),
IN JAVA UNIT I Assistant Professor of CSE, MJCET
32
are considered garbage, because they can no longer affect the future course
of program execution.
In a JVM the root set is implementation dependent but would always include
any object references in the local variables. In the JVM, all objects reside on
the heap. The local variables reside on the Java stack, and each thread of
execution has its own stack. Each local variable is either an object reference
or a primitive type, such as int, char, or float. Therefore the roots of any JVM
garbage-collected heap will include every object reference on every thread's
stack. Another source of roots are any object references, such as strings, in
the constant pool of loaded classes. The constant pool of a loaded class may
refer to strings stored on the heap, such as the class name, superclass
name, superinterface names, field names, field signatures, method names,
and method signatures.
The JVM can be implemented such that the garbage collector knows the
difference between a genuine object reference and a primitive type (for
example, an int) that appears to be a valid object reference. However, some
garbage collectors may choose not to distinguish between genuine object
references and look-alikes. Such garbage collectors are called conservative
because they may not always free every unreferenced object. Sometimes a
garbage object will be wrongly considered to be live by a conservative
collector, because an object reference look-alike referred to it. Conservative
collectors trade off an increase in garbage collection speed for occasionally
not freeing some actual garbage.
These notes are dedicated to my father Mr. Mir Farooq Ali, Professor and
Ex-Head of the Department of Mathematics (Retired), MJCET and my mom
Mrs. Arifa, Vice Principal and Junior Lecturer in Botany (Retired) Govt.
Falaknuma Junior College. If you have any doubts in this document, you
can call me on 9010149047 or mail me at [email protected],
your queries will be replied within 24 hours.
PROGRAMMING Prepared by Mir Ahmed Ali (Ali Sir),
IN JAVA UNIT I Assistant Professor of CSE, MJCET
33
encountered during the trace are marked in some way. After the trace is
complete, unmarked objects are known to be unreachable and can be
garbage collected.
What Is Stack?
Each Java virtual machine thread has a private Java virtual machine stack,
created at the same time as the thread. A Java virtual machine stack or stack
stores frames. It holds local variables and partial results, and plays a part in
method invocation and return. Because the Java virtual machine stack is
never manipulated directly except to push and pop frames, frames may be
heap allocated.
The Java virtual machine specification permits Java virtual machine stacks
either to be of a fixed size or to dynamically expand and contract as required
by the computation. If the Java virtual machine stacks are of a fixed size, the
size of each Java virtual machine stack may be chosen independently when
that stack is created. A Java virtual machine implementation may provide the
programmer or the user control over the initial size of Java virtual machine
stacks, as well as, in the case of dynamically expanding or contracting Java
virtual machine stacks, control over the maximum and minimum sizes.
The following exceptional conditions are associated with Java virtual machine
stacks:
These notes are dedicated to my father Mr. Mir Farooq Ali, Professor and
Ex-Head of the Department of Mathematics (Retired), MJCET and my mom
Mrs. Arifa, Vice Principal and Junior Lecturer in Botany (Retired) Govt.
Falaknuma Junior College. If you have any doubts in this document, you
can call me on 9010149047 or mail me at [email protected],
your queries will be replied within 24 hours.
PROGRAMMING Prepared by Mir Ahmed Ali (Ali Sir),
IN JAVA UNIT I Assistant Professor of CSE, MJCET
34
To avoid above errors, internally JVM will shift the local variable from the
stack to method area, that is one of the blocks maintained by JVM.
What Is Heap?
In case of stack, each and every thread will have its own stack. But this
option is not with heap. The heap that is maintained by the JVM is shared by
all the threads. It means, that only one heap is present in the JVM. The heap
is the runtime data area from which memory for all class instances.
The heap is created when a Java Virtual Machine starts up. Heap storage for
objects is reclaimed by an automatic storage management system known as
a garbage collector. Objects are never explicitly deallocated. The heap may
be of a fixed size or may be expanded as required by the computation and
may be contracted if a larger heap becomes unnecessary. The memory for
the heap does not need to be contiguous. In other words, it totally depends
on the type of algorithm that is followed by JVM. Some algorithms, allocate
memory in terms of continuous fashion and some in indexed fashion.
The JVM maintains the following Data Area (Memory). In other words, the
memory in JVM is divided into 5 areas as shown below:
1. Method Area
2. Heap Area
3. Java Stacks
4. PC Registers
5. Native Method Stacks
Out of the above five memory blocks that are present in JVM, we have
discussed only two which will be required to us.
These notes are dedicated to my father Mr. Mir Farooq Ali, Professor and
Ex-Head of the Department of Mathematics (Retired), MJCET and my mom
Mrs. Arifa, Vice Principal and Junior Lecturer in Botany (Retired) Govt.
Falaknuma Junior College. If you have any doubts in this document, you
can call me on 9010149047 or mail me at [email protected],
your queries will be replied within 24 hours.
PROGRAMMING Prepared by Mir Ahmed Ali (Ali Sir),
IN JAVA UNIT I Assistant Professor of CSE, MJCET
35
class SuperClassName{
--------------------------------
--------------------------------
--------------------------------
--------------------------------
--------------------------------
--------------------------------
}
In the above program, the sub class will get the variables and methods of the
super class also. You can understand this line, after understanding the basic
program on inheritance.
These notes are dedicated to my father Mr. Mir Farooq Ali, Professor and
Ex-Head of the Department of Mathematics (Retired), MJCET and my mom
Mrs. Arifa, Vice Principal and Junior Lecturer in Botany (Retired) Govt.
Falaknuma Junior College. If you have any doubts in this document, you
can call me on 9010149047 or mail me at [email protected],
your queries will be replied within 24 hours.
PROGRAMMING Prepared by Mir Ahmed Ali (Ali Sir),
IN JAVA UNIT I Assistant Professor of CSE, MJCET
36
class A{
int x=12;
int y=13;
void Add(){
System.out.println("The result of Add method is "+(x+y));
}
}
class B extends A{
int z=16;
void Addition(){
System.out.println("The result of Addition is "+(x+y+z));
}
}
public class Inheritance {
public static void main(String[] args) {
A a1 = new A();
a1.Add();
B b1 = new B();
b1.Addition();
b1.Add();
}
}
In the above program we can notice that, there are three classes. The first is
class A, the second in class B and the third is class C. The class A is known as
super class, the class B is known as sub class the class C is known as
independent class. In class C, we are creating an object of class A as well as
we are creating an object of class B. Now let us consider the following
statements as shown below:
A a1 = new A();
These notes are dedicated to my father Mr. Mir Farooq Ali, Professor and
Ex-Head of the Department of Mathematics (Retired), MJCET and my mom
Mrs. Arifa, Vice Principal and Junior Lecturer in Botany (Retired) Govt.
Falaknuma Junior College. If you have any doubts in this document, you
can call me on 9010149047 or mail me at [email protected],
your queries will be replied within 24 hours.
PROGRAMMING Prepared by Mir Ahmed Ali (Ali Sir),
IN JAVA UNIT I Assistant Professor of CSE, MJCET
37
gets allocated on the heap. The amount of memory that gets allocated
depends on number of instance variables and instance methods that are
present with in a class. In class A, we have two instance variables x and y
and one instance method known as Add. It must be noted that a1 is a
reference variable of super class A.
B b1 = new B();
As we know that the sub class object can access the variables and methods
of a super class. But the condition is the access specifiers for the variable
and methods present in the super class must be either public, protected or
default. But, there must be not private. If the variables and methods of a
super class are private, then the sub class object cannot access the variables
and methods of a super class because the variables and methods that are
declared as private, cannot be accessed outside the class in which they are
defined. Let us understand the concept of private access specifier with the
help of a program as shown below:
class A1{
private int x=10;
void display(){
System.out.println(x);
}
}
public class privateprogram extends A1 {
These notes are dedicated to my father Mr. Mir Farooq Ali, Professor and
Ex-Head of the Department of Mathematics (Retired), MJCET and my mom
Mrs. Arifa, Vice Principal and Junior Lecturer in Botany (Retired) Govt.
Falaknuma Junior College. If you have any doubts in this document, you
can call me on 9010149047 or mail me at [email protected],
your queries will be replied within 24 hours.
PROGRAMMING Prepared by Mir Ahmed Ali (Ali Sir),
IN JAVA UNIT I Assistant Professor of CSE, MJCET
38
In the above program there exists two classes, the first is super class A1 and
the second is sub class privateprogram. The members that are present in
class A1 are instance variable x and instance method display(). The access
specifier of the instance variable is private and the access specifier for
display() method is default. The sub class is privateprogram that contains a
method known as main method. In this method we are creating an object of
class A1. The reference variable to class A1 is a2. With the help of a2 , now I
want to change the value of x, which is an instance variable in class A1. If we
want to change the value, we will get compile time error because we are
accessing the variable in another class, which leads to error as we cannot
access the variable outside the class A1.
Forms of Inheritance
class A{
int x=12;
These notes are dedicated to my father Mr. Mir Farooq Ali, Professor and
Ex-Head of the Department of Mathematics (Retired), MJCET and my mom
Mrs. Arifa, Vice Principal and Junior Lecturer in Botany (Retired) Govt.
Falaknuma Junior College. If you have any doubts in this document, you
can call me on 9010149047 or mail me at [email protected],
your queries will be replied within 24 hours.
PROGRAMMING Prepared by Mir Ahmed Ali (Ali Sir),
IN JAVA UNIT I Assistant Professor of CSE, MJCET
39
int y=13;
void Add(){
System.out.println("The result of Add method is "+(x+y));
}
}
class B extends A{
int z=16;
void Addition(){
System.out.println("The result of Addition is "+(x+y+z));
}
}
public class Inheritance {
public static void main(String[] args) {
A a1 = new A();
a1.Add();
B b1 = new B();
b1.Addition();
b1.Add();
}
}
In the above example, we can notice that there exists only one super class
and one sub class.
class A{
int x=12;
int y=13;
These notes are dedicated to my father Mr. Mir Farooq Ali, Professor and
Ex-Head of the Department of Mathematics (Retired), MJCET and my mom
Mrs. Arifa, Vice Principal and Junior Lecturer in Botany (Retired) Govt.
Falaknuma Junior College. If you have any doubts in this document, you
can call me on 9010149047 or mail me at [email protected],
your queries will be replied within 24 hours.
PROGRAMMING Prepared by Mir Ahmed Ali (Ali Sir),
IN JAVA UNIT I Assistant Professor of CSE, MJCET
40
void Add(){
System.out.println("The result of Add method is "+(x+y));
}
}
class B extends A{
int z=16;
void Addition(){
System.out.println("The result of Addition is "+(x+y+z));
}
}
class C extends B{
int w=19;
void Addition1(){
System.out.println("The result of Addition1 is"+(x+y+z+w));
}
}
public class Inheritance {
public static void main(String[] args) {
A a1 = new A();
a1.Add();
B b1 = new B();
b1.Addition();
b1.Add();
C c1 = new C();
c1.Addition();
c1.Add();
c1.Addition1();
}
}
In the above program, we can notice that there exists more than one sub
class and more than one super class, depending on the type of combinations.
Multiple Inheritance
Multiple Inheritance is a type of inheritance that makes a sub class to access
variables and methods from more than one super class at a time. Normally,
in case of C++, multiple inheritance is possible with the help of extends
keyword but where as in Java, multiple inheritance with the help of extends
These notes are dedicated to my father Mr. Mir Farooq Ali, Professor and
Ex-Head of the Department of Mathematics (Retired), MJCET and my mom
Mrs. Arifa, Vice Principal and Junior Lecturer in Botany (Retired) Govt.
Falaknuma Junior College. If you have any doubts in this document, you
can call me on 9010149047 or mail me at [email protected],
your queries will be replied within 24 hours.
PROGRAMMING Prepared by Mir Ahmed Ali (Ali Sir),
IN JAVA UNIT I Assistant Professor of CSE, MJCET
41
interface A{
void m1();
}
interface B{
void m2();
}
class C implements A,B{
public void m1(){
System.out.println("Method of Interface A");
}
public void m2(){
System.out.println("Method of Interface B");
}
}
In the above program we can notice that class C is getting features from
more than one interface at a time. This concept is known as Multiple
Inheritance, which is solved with the help of interfaces concept.
These notes are dedicated to my father Mr. Mir Farooq Ali, Professor and
Ex-Head of the Department of Mathematics (Retired), MJCET and my mom
Mrs. Arifa, Vice Principal and Junior Lecturer in Botany (Retired) Govt.
Falaknuma Junior College. If you have any doubts in this document, you
can call me on 9010149047 or mail me at [email protected],
your queries will be replied within 24 hours.
PROGRAMMING Prepared by Mir Ahmed Ali (Ali Sir),
IN JAVA UNIT I Assistant Professor of CSE, MJCET
42
If we use final access specifier for a class, and if the program that contains
classes is using inheritance, then we get compile time error. In other words,
if we write final access specifier for a super class , and if we try to create sub
classes then we get a compile time error saying that classes cannot be
inherited. It means that if we declare super class as final, then we cannot
create a sub class for that class. if we do so, then a compile time error gets
generated.
If we use final access specifier for a method, and if the program is following
inheritance concept and if user wants to over ride the method then we will
get a compile time error. In other words, if we write final access specifier for
a method that is present in the super class and if we try to over ride the
same method with in a sub class, then we get a compile time error as final
methods cannot be overridden i.e. we cannot write methods having same
signature as that of a methods in super class with in a sub class.
These notes are dedicated to my father Mr. Mir Farooq Ali, Professor and
Ex-Head of the Department of Mathematics (Retired), MJCET and my mom
Mrs. Arifa, Vice Principal and Junior Lecturer in Botany (Retired) Govt.
Falaknuma Junior College. If you have any doubts in this document, you
can call me on 9010149047 or mail me at [email protected],
your queries will be replied within 24 hours.
PROGRAMMING Prepared by Mir Ahmed Ali (Ali Sir),
IN JAVA UNIT I Assistant Professor of CSE, MJCET
43
class A{
void display(){
System.out.println("Method display of class A is called");
}
}
class B extends A{
void display(){
System.out.println("Method display of class B is called");
}
}
These notes are dedicated to my father Mr. Mir Farooq Ali, Professor and
Ex-Head of the Department of Mathematics (Retired), MJCET and my mom
Mrs. Arifa, Vice Principal and Junior Lecturer in Botany (Retired) Govt.
Falaknuma Junior College. If you have any doubts in this document, you
can call me on 9010149047 or mail me at [email protected],
your queries will be replied within 24 hours.
PROGRAMMING Prepared by Mir Ahmed Ali (Ali Sir),
IN JAVA UNIT I Assistant Professor of CSE, MJCET
44
In the above example , we can notice that they are three classes. The first is
class A , which is a super class . The second is class B, which is a sub class
and the third is MethodOverriding. We can notice that there is a method by
name display() that is present in super class as well as sub class has same
method name, same return datatype and same number of parameters. But if
we create an object of sub class, and if we call display() method, then
display() method of class B is called, but if I want to call display() method of
class A, which is a super class here, then we cannot call the display() method
which is present in super class with the help of object of class B. Here we are
getting this problem because the name of the method is same in both super
class and sub class. So, if I want to call display() method of super class, then
I have to use a keyword known as super which calls a method or variable
present in the super class.
class A{
void display(){
System.out.println("Method display of class A is called");
}
}
class B extends A{
void display(){
System.out.println("Method display of class B is called");
super.display();
These notes are dedicated to my father Mr. Mir Farooq Ali, Professor and
Ex-Head of the Department of Mathematics (Retired), MJCET and my mom
Mrs. Arifa, Vice Principal and Junior Lecturer in Botany (Retired) Govt.
Falaknuma Junior College. If you have any doubts in this document, you
can call me on 9010149047 or mail me at [email protected],
your queries will be replied within 24 hours.
PROGRAMMING Prepared by Mir Ahmed Ali (Ali Sir),
IN JAVA UNIT I Assistant Professor of CSE, MJCET
45
}
}
class A{
int x=5;
}
class B extends A{
int x=13;
void dis(){
System.out.println(x);
System.out.println(super.x);
}
}
class C{
public static void main(String args[]){
B b1 = new B();
b1.dis();
}
}
In the above program, we can notice that the program is using a concept
known as inheritance. In this program, there exists three classes. Among
three classes, we have one class A, which is known as super class, we have
These notes are dedicated to my father Mr. Mir Farooq Ali, Professor and
Ex-Head of the Department of Mathematics (Retired), MJCET and my mom
Mrs. Arifa, Vice Principal and Junior Lecturer in Botany (Retired) Govt.
Falaknuma Junior College. If you have any doubts in this document, you
can call me on 9010149047 or mail me at [email protected],
your queries will be replied within 24 hours.
PROGRAMMING Prepared by Mir Ahmed Ali (Ali Sir),
IN JAVA UNIT I Assistant Professor of CSE, MJCET
46
one more class B, which is known as sub class and we have one more class
C, which is neither a sub class nor super class, but is said to be an
independent class. We can also notice, that there is a variable by name x in
super class A and another variable by name x in sub class B. In class C, we
created an object of class B , and with the help of this object if we access the
variable x, then the x present in class B only gets executed. In other words,
with the help of sub class object , we cannot access the variable x that is
present in super class. In order to access the variable x present in super
class, we have to use a keyword super with the name of the variable. Here
we write
When the above statement gets executed, the variable present in super
class is called.
Packages:
Types of Packages
They are two types of packages that exists in Java programming language.
They are:
1. Predefined Package
2. User Defined Package
Predefined Package:
These notes are dedicated to my father Mr. Mir Farooq Ali, Professor and
Ex-Head of the Department of Mathematics (Retired), MJCET and my mom
Mrs. Arifa, Vice Principal and Junior Lecturer in Botany (Retired) Govt.
Falaknuma Junior College. If you have any doubts in this document, you
can call me on 9010149047 or mail me at [email protected],
your queries will be replied within 24 hours.
PROGRAMMING Prepared by Mir Ahmed Ali (Ali Sir),
IN JAVA UNIT I Assistant Professor of CSE, MJCET
47
1. java.lang
2. java.awt
3. java.net
4. java.sql
5. java.util
6. javax.swing
Creation of Packages
These notes are dedicated to my father Mr. Mir Farooq Ali, Professor and
Ex-Head of the Department of Mathematics (Retired), MJCET and my mom
Mrs. Arifa, Vice Principal and Junior Lecturer in Botany (Retired) Govt.
Falaknuma Junior College. If you have any doubts in this document, you
can call me on 9010149047 or mail me at [email protected],
your queries will be replied within 24 hours.
PROGRAMMING Prepared by Mir Ahmed Ali (Ali Sir),
IN JAVA UNIT I Assistant Professor of CSE, MJCET
48
package packagename;
package packagename;
class classname{
---------------------
---------------------
---------------------
---------------------
}
The above code can be written either in a notepad or java editor or any
editor. It must be noted that the name of the notepad or file, must be same
as the name of the class that is present with in the file. This class must be a
single class i.e. if we write more than one class with in a same file, then a
compile time error gets generated. But, if user wants to include one more
class with in a package, then the user has to open new notepad or java
editor, write the first statement as package creation statement , followed by
the name of the class and save the name of the file as same as the name of
the class.
Write a java program that is used to create a package that is having only two
classes.
package student;
public class Addition{
int i,j;
public Addition(int a,int b){
These notes are dedicated to my father Mr. Mir Farooq Ali, Professor and
Ex-Head of the Department of Mathematics (Retired), MJCET and my mom
Mrs. Arifa, Vice Principal and Junior Lecturer in Botany (Retired) Govt.
Falaknuma Junior College. If you have any doubts in this document, you
can call me on 9010149047 or mail me at [email protected],
your queries will be replied within 24 hours.
PROGRAMMING Prepared by Mir Ahmed Ali (Ali Sir),
IN JAVA UNIT I Assistant Professor of CSE, MJCET
49
i=a;
j=b;
}
public void display(){
System.out.println("The added value is:"+(i+j));
}
}
package student;
public class Substraction{
int i,j;
public Substraction(int a,int b){
i=a;
j=b;
}
public void display(){
System.out.println("The added value is:"+(i-j));
}
}
The above step1 must be written in separate notepad and the step2 must
also written in separate notepad. In the above step1 and step2 we can notice
that when user is writing a class with in a package, then it is compulsory that
the class must be public, the methods with in the class must be public, the
constructors with in the class must be public.
Compilation of Packages
These notes are dedicated to my father Mr. Mir Farooq Ali, Professor and
Ex-Head of the Department of Mathematics (Retired), MJCET and my mom
Mrs. Arifa, Vice Principal and Junior Lecturer in Botany (Retired) Govt.
Falaknuma Junior College. If you have any doubts in this document, you
can call me on 9010149047 or mail me at [email protected],
your queries will be replied within 24 hours.
PROGRAMMING Prepared by Mir Ahmed Ali (Ali Sir),
IN JAVA UNIT I Assistant Professor of CSE, MJCET
50
When user press enter key, then a directory with the name that is present in
the package gets created and this directory is stored in a location that has
been specified in the directory path and the .class file is stored in this
directory.
Accessing a Package:
If user wants to access a package, then the user has to write a java program
that can access the methods that are present in the class that are present in
the package. This java program, must contain a statement that is used to
access a package. The statement is nothing but import statement. With the
help of import statement, the java program can access the methods that are
present with in the class and these classes are present in the package.
Write a java program that is used to access a package that is having only
two classes.
import student.Addition;
import student.Substraction;
class Pack{
public static void main(String sun[]){
Addition a1 = new Addition(2,3);
a1.display();
Substraction s1 = new Substraction(10,4);
s1.display();
}
}
The above java program is written in a notepad, whose name is same as the
name of the class. Here the class name is Pack i.e. the name of the notepad
is Pack.java . When the above program gets compiled with the help of javac
command, a .class file gets generated and when the above program gets
executed with the help of java command, then we can see the output.
These notes are dedicated to my father Mr. Mir Farooq Ali, Professor and
Ex-Head of the Department of Mathematics (Retired), MJCET and my mom
Mrs. Arifa, Vice Principal and Junior Lecturer in Botany (Retired) Govt.
Falaknuma Junior College. If you have any doubts in this document, you
can call me on 9010149047 or mail me at [email protected],
your queries will be replied within 24 hours.
PROGRAMMING Prepared by Mir Ahmed Ali (Ali Sir),
IN JAVA UNIT I Assistant Professor of CSE, MJCET
51
Importing a Package:
import packagename.*;
import packagename.subpackagename.*;
import java.awt.*;
These notes are dedicated to my father Mr. Mir Farooq Ali, Professor and
Ex-Head of the Department of Mathematics (Retired), MJCET and my mom
Mrs. Arifa, Vice Principal and Junior Lecturer in Botany (Retired) Govt.
Falaknuma Junior College. If you have any doubts in this document, you
can call me on 9010149047 or mail me at [email protected],
your queries will be replied within 24 hours.
PROGRAMMING Prepared by Mir Ahmed Ali (Ali Sir),
IN JAVA UNIT I Assistant Professor of CSE, MJCET
52
When the above statement gets executed, all classes and interfaces of
package awt are loaded into the main memory, though the user is
interested only in some methods or some classes.
import java.awt.event.*;
When the above statement gets executed, all classes and interfaces present
in the sub-package event are loaded in the main memory, though the user is
interested only in some methods or some classes.
As in the above cases, we can notice that the classes and interfaces that the
user is not interested are also getting loaded into main memory. (It must be
noted that the word interested reflects to the methods that are
present in the classes and interfaces that are not required to the
program or application that user is writing).
So, if the programmer is interested only in loading of the methods
(present in the classes and interfaces), that are present in the application,
and if the programmer wants no delay during the execution of the program,
then it is advisable to go for explicit import or general import.
These notes are dedicated to my father Mr. Mir Farooq Ali, Professor and
Ex-Head of the Department of Mathematics (Retired), MJCET and my mom
Mrs. Arifa, Vice Principal and Junior Lecturer in Botany (Retired) Govt.
Falaknuma Junior College. If you have any doubts in this document, you
can call me on 9010149047 or mail me at [email protected],
your queries will be replied within 24 hours.
PROGRAMMING Prepared by Mir Ahmed Ali (Ali Sir),
IN JAVA UNIT I Assistant Professor of CSE, MJCET
53
import java.awt.event.ActionEvent;
When the above statement gets executed, only the methods that are present
in the class ActionEvent are loaded on to the main memory. So in this way
we can save the time i.e. the execution time and this increases the efficiency
of the application or the program that is developed by the user. .all classes
and interfaces present in the sub-package event are loaded in the main
memory, though the user is interested only in some methods or some
classes.
Define Interface
As we have studied a concept known as Encapsulation in Object Oriented
Paradigm. It states that wrapping of variables and methods into one unit.
This is done just to provide security to data that is present in variables and
security to information that is present in the method. Technically, the
concept of encapsulation is implemented with the help of classes and
interfaces. As we know that a class is defined as a collection of variables and
methods. The methods that are present in a class is known as concrete
methods.
An interface is defined as collection of
abstract methods and interface variables. If an interface contains concrete
methods, then java compiler generates a compile time error. An abstract
method is defined as a method that does not contain body. In other words an
interface consists of only method prototypes. Since, these methods are
incomplete methods, that’s the reason we cannot create objects for
interfaces. The methods present in the interface are public and abstract. In
other words, the access specifier that is used for the methods present in an
interface is public , so that these interfaces can be accessed from anywhere.
The keyword abstract, says java compiler that these methods don’t have
body. These abstract methods present in the interfaces are terminated by
semicolon. If semicolon is missing , then we get a compile time error.
interface interface_name{
These notes are dedicated to my father Mr. Mir Farooq Ali, Professor and
Ex-Head of the Department of Mathematics (Retired), MJCET and my mom
Mrs. Arifa, Vice Principal and Junior Lecturer in Botany (Retired) Govt.
Falaknuma Junior College. If you have any doubts in this document, you
can call me on 9010149047 or mail me at [email protected],
your queries will be replied within 24 hours.
PROGRAMMING Prepared by Mir Ahmed Ali (Ali Sir),
IN JAVA UNIT I Assistant Professor of CSE, MJCET
54
Their can exist only one abstract method or any number of abstract
methods. Number of abstract methods in an interface depends on the type
of program, a programmer is writing.
Implementation Class
The class that take the responsibility of defining the body of the abstract
methods in an interface is known as implementation class. Moreover, this
class can define their methods of their own. The syntax of implementation
class is
Interface Reference
These notes are dedicated to my father Mr. Mir Farooq Ali, Professor and
Ex-Head of the Department of Mathematics (Retired), MJCET and my mom
Mrs. Arifa, Vice Principal and Junior Lecturer in Botany (Retired) Govt.
Falaknuma Junior College. If you have any doubts in this document, you
can call me on 9010149047 or mail me at [email protected],
your queries will be replied within 24 hours.
PROGRAMMING Prepared by Mir Ahmed Ali (Ali Sir),
IN JAVA UNIT I Assistant Professor of CSE, MJCET
55
interface Inter {
void addTwo(int a,int b);
void addThree(int a,int b,int c);
}
class Arith implements Inter{
public void addTwo(int a,int b){
System.out.println("The result of two values "+(a+b));
}
public void addThree(int a,int b,int c){
System.out.println("The result of three values "+(a+b+c));
}
}
class Arithmetic{
public static void main(String ag[]){
Inter i = new Arith();
i.addTwo(2, 3);
i.addThree(6, 8, 9);
}
}
These notes are dedicated to my father Mr. Mir Farooq Ali, Professor and
Ex-Head of the Department of Mathematics (Retired), MJCET and my mom
Mrs. Arifa, Vice Principal and Junior Lecturer in Botany (Retired) Govt.
Falaknuma Junior College. If you have any doubts in this document, you
can call me on 9010149047 or mail me at [email protected],
your queries will be replied within 24 hours.
PROGRAMMING Prepared by Mir Ahmed Ali (Ali Sir),
IN JAVA UNIT I Assistant Professor of CSE, MJCET
56
Interface Variables:
interface InterfaceVariables{
int x=10;
}
class Interf1 implements InterfaceVariables{
public static void main(String args[]){
Interf1 in = new Interf1();
System.out.println(in.x);
//in.x=112;
}
}
When the above program gets executed, we get output as 10. But, if we
remove the single comment, then if we save and then compile the program,
we get compile time error as saying that we cannot assign a value
to variable x.
These notes are dedicated to my father Mr. Mir Farooq Ali, Professor and
Ex-Head of the Department of Mathematics (Retired), MJCET and my mom
Mrs. Arifa, Vice Principal and Junior Lecturer in Botany (Retired) Govt.
Falaknuma Junior College. If you have any doubts in this document, you
can call me on 9010149047 or mail me at [email protected],
your queries will be replied within 24 hours.
PROGRAMMING Prepared by Mir Ahmed Ali (Ali Sir),
IN JAVA UNIT I Assistant Professor of CSE, MJCET
57
As we have seen in Unit III that a class can inherit another class with the help
of extends keyword. Likewise, we can inherit an interface from another
interface. The syntax holds good as same as the class. When a class
implements an interface, that inherits another interface, it becomes
mandatory for the implementation class to implement the methods that are
present in the interface and the methods that have been inherited from
other interface. With this concept, we can implement Multiple Inheritance in
the indirect way.
interface A{
void d1();
}
interface B extends A{
void d2();
}
class C implements B{
public void d1(){
System.out.println("Method of Interface A is called");
}
class InterfEx{
public static void main(String args[]){
C c1 = new C();
c1.d1();
c1.d2();
}
}
These notes are dedicated to my father Mr. Mir Farooq Ali, Professor and
Ex-Head of the Department of Mathematics (Retired), MJCET and my mom
Mrs. Arifa, Vice Principal and Junior Lecturer in Botany (Retired) Govt.
Falaknuma Junior College. If you have any doubts in this document, you
can call me on 9010149047 or mail me at [email protected],
your queries will be replied within 24 hours.
PROGRAMMING Prepared by Mir Ahmed Ali (Ali Sir),
IN JAVA UNIT I Assistant Professor of CSE, MJCET
58
In the above program, we can notice that, there are two interfaces namely A
and B. We can notice that interface A contains only one method. There is
another interface by name B, which has two methods one of its own and the
another method is inherited from the interface A as we are using a keyword
known as extends. After writing the interface, we are creating a class
known as implementation class, whose responsibility is write the body of the
abstract methods that are declared with in the interface. It is mandatory that
this implementation class must contain the bodies of the abstract methods
that are present in the interface B. If any one of the method body is missing,
then a compile time error gets generated. We are creating one more class
that contains main method. In this method, we are creating objects of
implementation class so that we can call the methods that are present in the
implementation class.
Introduction
Exception is a run-time error which arises during the execution of java
program. The term exception in java stands for an exceptional event. It
can be defined as abnormal event that arises during the execution and
normal flow of program.
Types of Errors in Java :
They are two types of errors in java. They are discussed below:
1.Compile-time Error-Compile -time Error are those error which occurs
when you don't follow the syntax of the code or do wrong in writing a code
of the Programming language. The Compiler detect the syntax -error in the
code of the Programming language during its execution. For example in
normal java coding ,you need your every statement to be terminated with
semicolon(;).if you don't follow the rule, this will give you Compile-time
error.
2.Run -time Error-Run-time error are the error which arises during the
execution of a program. For example. The program runs out of the
memory, result in Run-time error.
Exceptional Hierarchy
These notes are dedicated to my father Mr. Mir Farooq Ali, Professor and
Ex-Head of the Department of Mathematics (Retired), MJCET and my mom
Mrs. Arifa, Vice Principal and Junior Lecturer in Botany (Retired) Govt.
Falaknuma Junior College. If you have any doubts in this document, you
can call me on 9010149047 or mail me at [email protected],
your queries will be replied within 24 hours.
PROGRAMMING Prepared by Mir Ahmed Ali (Ali Sir),
IN JAVA UNIT I Assistant Professor of CSE, MJCET
59
Object Class is the base class of the exception hierarchy. Object class is the
super class of Throwable class.Throwable class is the super class of all the
exceptional class and error class. It must be noted that methods like
printStackTrace() is defined in this class. Moreover the Throwable class is
present in a package known as java.lang.
Types of Exceptions-
Exceptions in java are categorized into two. The first is known as Checked
Exception and the second is known as Un-Checked Exception as discussed
below:
These notes are dedicated to my father Mr. Mir Farooq Ali, Professor and
Ex-Head of the Department of Mathematics (Retired), MJCET and my mom
Mrs. Arifa, Vice Principal and Junior Lecturer in Botany (Retired) Govt.
Falaknuma Junior College. If you have any doubts in this document, you
can call me on 9010149047 or mail me at [email protected],
your queries will be replied within 24 hours.
PROGRAMMING Prepared by Mir Ahmed Ali (Ali Sir),
IN JAVA UNIT I Assistant Professor of CSE, MJCET
60
These notes are dedicated to my father Mr. Mir Farooq Ali, Professor and
Ex-Head of the Department of Mathematics (Retired), MJCET and my mom
Mrs. Arifa, Vice Principal and Junior Lecturer in Botany (Retired) Govt.
Falaknuma Junior College. If you have any doubts in this document, you
can call me on 9010149047 or mail me at [email protected],
your queries will be replied within 24 hours.
PROGRAMMING Prepared by Mir Ahmed Ali (Ali Sir),
IN JAVA UNIT I Assistant Professor of CSE, MJCET
61
c=a/b;
}
}
C:\New Folder>javac Excep.java
When the above program gets compiled, we don’t get any compile time
error. After compiling the program, if we don’t have any compile time error,
then we have to execute the java program as shown below:
C:\New Folder>java Excep
Exception in thread "main" java.lang.ArithmeticException: / by zero
at Excep.main(Excep.java:7)
As soon as we execute the program, we see that a message is displayed.
This message is the result of execution of default exceptional handler by the
JVM. The job of the default exception handler is to display the details of the
exception to the user.
If we clearly observe the message, then we can notice that till line 6, the
program execution was normal, i.e. the program was under normal
execution. On encountering statement 7, JVM is not able to resolve the
problem, and it gets terminated from line 7 to end of the program.
But, if I want to handle this exception i.e. if I want to avoid the
abnormal termination of the program(due to raise of exception), then I have
to incorporate or include a mechanism known as Exceptional Handling
mechanism in to my program. On doing so, if there is an exception in my
program in future, then my program will not get terminated abnormally.
Thread
These notes are dedicated to my father Mr. Mir Farooq Ali, Professor and
Ex-Head of the Department of Mathematics (Retired), MJCET and my mom
Mrs. Arifa, Vice Principal and Junior Lecturer in Botany (Retired) Govt.
Falaknuma Junior College. If you have any doubts in this document, you
can call me on 9010149047 or mail me at [email protected],
your queries will be replied within 24 hours.
PROGRAMMING Prepared by Mir Ahmed Ali (Ali Sir),
IN JAVA UNIT I Assistant Professor of CSE, MJCET
62
When we compile the above program, we don’t get any compile time error.
But when we execute the above program , we get the following output as
shown below:
The first is name of the thread, the second is the priority and the third is the
name of the group.
If we compile the above program, we don’t get any compile time error. But
when we execute the java program, we get the following output as shown
below:
Program Explanation:
These notes are dedicated to my father Mr. Mir Farooq Ali, Professor and
Ex-Head of the Department of Mathematics (Retired), MJCET and my mom
Mrs. Arifa, Vice Principal and Junior Lecturer in Botany (Retired) Govt.
Falaknuma Junior College. If you have any doubts in this document, you
can call me on 9010149047 or mail me at [email protected],
your queries will be replied within 24 hours.
PROGRAMMING Prepared by Mir Ahmed Ali (Ali Sir),
IN JAVA UNIT I Assistant Professor of CSE, MJCET
63
value is 5. The third is the name of the group to which the newly
created thread belongs.
If user wants to see the detail information about a thread i.e. name of the
thread, the priority of the thread that is assigned by JVM and the name
of the group to which thread belongs, then it is possible with the help
of a method known as currentThread. This thread is a static method
which is present in a class known as Thread class. So, if we want to see
this details of newly created thread object then it is possible with the
help of the following statement as shown below:
If user wants to see the name of the thread only, then it is possible with
the help of a method known as getName() , which is a method present
in a class known as Thread Class. So, if we want to see only the name
of the thread, then it is possible with the help of the following
statement as shown below:
Creating a Thread:
If user wants to create and manage a thread, then it is possible with help of
two ways:
a. First, with the help of a class known as Thread class. i.e. we have to
create a sub class, which has to extend Thread class.
b. Secondly, with the help of an interface known as Runnable i.e. we
have to create a class that must implement this interface.
Both Thread class and Runnable interface are present in a package known as
java.lang package, which is a predefined package in Java.
These notes are dedicated to my father Mr. Mir Farooq Ali, Professor and
Ex-Head of the Department of Mathematics (Retired), MJCET and my mom
Mrs. Arifa, Vice Principal and Junior Lecturer in Botany (Retired) Govt.
Falaknuma Junior College. If you have any doubts in this document, you
can call me on 9010149047 or mail me at [email protected],
your queries will be replied within 24 hours.
PROGRAMMING Prepared by Mir Ahmed Ali (Ali Sir),
IN JAVA UNIT I Assistant Professor of CSE, MJCET
64
The first way to create a thread is to create a class, which is derived from a
class known as Thread class. The steps that are involved to create a thread
by extending Thread class are:
These notes are dedicated to my father Mr. Mir Farooq Ali, Professor and
Ex-Head of the Department of Mathematics (Retired), MJCET and my mom
Mrs. Arifa, Vice Principal and Junior Lecturer in Botany (Retired) Govt.
Falaknuma Junior College. If you have any doubts in this document, you
can call me on 9010149047 or mail me at [email protected],
your queries will be replied within 24 hours.
PROGRAMMING Prepared by Mir Ahmed Ali (Ali Sir),
IN JAVA UNIT I Assistant Professor of CSE, MJCET
65
As I said you that, threads are created by the programmer for internal use.
The second way to create a thread is to implement an interface known as
Runnable. It must be noted that the Runnable interface is present in a
package known as java.lang package, which is considered to be default
package in Java. Runnable is the name of the interface, that consists of only
one method known as run() method. So, it is responsibility of the user is to
write the body of run() method in a class known as Implementation class. In
other words, if user wants to create a thread then, the user has to create an
implementation class for Runnable interface.
}
}
In the above program, we can notice that the Runnable interface consists of
only one method known as run() method. But as I said you that, if you want a
thread to be started it is mandatory that it gets started if and only if start()
method is invoked. Since, Runnable interface is not having start() method,
we are creating an object of user define thread class, and passing this object
These notes are dedicated to my father Mr. Mir Farooq Ali, Professor and
Ex-Head of the Department of Mathematics (Retired), MJCET and my mom
Mrs. Arifa, Vice Principal and Junior Lecturer in Botany (Retired) Govt.
Falaknuma Junior College. If you have any doubts in this document, you
can call me on 9010149047 or mail me at [email protected],
your queries will be replied within 24 hours.
PROGRAMMING Prepared by Mir Ahmed Ali (Ali Sir),
IN JAVA UNIT I Assistant Professor of CSE, MJCET
66
As it is discussed in previous topics that each and every program in Java has
a default thread known as main thread. In the above program, we have a two
threads, one is user define thread and the second is the main thread. As we
know that the priority of the main thread is 5, and the priority of the user
define thread if also 5. But during execution, the JVM will give more priority
to main thread than the user define thread i.e. first the statements present in
main thread gets executed and then the statements in run() method of user
define class gets executed. Normally, this is the output that is always
expected, but also some times it totally depends it may varied i.e. it totally
depends on the type of scheduling algorithm that is used by a system
program known as Thread Scheduler. But if you want to change the priority
of the user define thread then it is possible with the help of a method known
as setPriority() method which is present in a class known as Thread class.
Normally the priority of a thread lies between 1 to 10. 1 is said to be
minimum priority and 10 is said to be maximum priority. If user want to set
some priority to some thread , then user can write this method in the
program as shown below. It must be noted, that instead of giving the values
between 1 to 10, but if user is giving value more than 10, then an exception
by name IllegalArgumentException gets generated.
These notes are dedicated to my father Mr. Mir Farooq Ali, Professor and
Ex-Head of the Department of Mathematics (Retired), MJCET and my mom
Mrs. Arifa, Vice Principal and Junior Lecturer in Botany (Retired) Govt.
Falaknuma Junior College. If you have any doubts in this document, you
can call me on 9010149047 or mail me at [email protected],
your queries will be replied within 24 hours.
PROGRAMMING Prepared by Mir Ahmed Ali (Ali Sir),
IN JAVA UNIT I Assistant Professor of CSE, MJCET
67
for(int i=0;i<10;i++){
System.out.println("Main Thread");
}
}
}
In the above example, you could see they are two threads, the first is the
main thread and the second is the user defined thread. So iam setting a
value of 10, to the user define thread with the help of a method known as
setPriority () method.
These notes are dedicated to my father Mr. Mir Farooq Ali, Professor and
Ex-Head of the Department of Mathematics (Retired), MJCET and my mom
Mrs. Arifa, Vice Principal and Junior Lecturer in Botany (Retired) Govt.
Falaknuma Junior College. If you have any doubts in this document, you
can call me on 9010149047 or mail me at [email protected],
your queries will be replied within 24 hours.
PROGRAMMING Prepared by Mir Ahmed Ali (Ali Sir),
IN JAVA UNIT I Assistant Professor of CSE, MJCET
68
1) New or Born
2) Ready or Runnable
3) Running
4) Waiting
5) Terminated or Dead
New State:
After being in new state, the thread moves to Ready state or Runnable state.
Practically, when thread object invokes start() method, then thread enters
into Ready state. In this state, the thread request for Thread Scheduler,
which in turn execute thread scheduling algorithms, and the result of this
algorithm will make the thread to be allocated to CPU, for execution.
Running State:
After waiting for CPU, the thread scheduler will allocate the CPU to the
thread. This state is known as Running state. Practically, when thread object
invokes run() method, then it is said to be in Running state.
Waiting State:
These notes are dedicated to my father Mr. Mir Farooq Ali, Professor and
Ex-Head of the Department of Mathematics (Retired), MJCET and my mom
Mrs. Arifa, Vice Principal and Junior Lecturer in Botany (Retired) Govt.
Falaknuma Junior College. If you have any doubts in this document, you
can call me on 9010149047 or mail me at [email protected],
your queries will be replied within 24 hours.
PROGRAMMING Prepared by Mir Ahmed Ali (Ali Sir),
IN JAVA UNIT I Assistant Professor of CSE, MJCET
69
Thread Groups:
where
tg: ThreadGroupObject
These notes are dedicated to my father Mr. Mir Farooq Ali, Professor and
Ex-Head of the Department of Mathematics (Retired), MJCET and my mom
Mrs. Arifa, Vice Principal and Junior Lecturer in Botany (Retired) Govt.
Falaknuma Junior College. If you have any doubts in this document, you
can call me on 9010149047 or mail me at [email protected],
your queries will be replied within 24 hours.
PROGRAMMING Prepared by Mir Ahmed Ali (Ali Sir),
IN JAVA UNIT I Assistant Professor of CSE, MJCET
70
A a1 = new A();
B b1 = new B();
/* The below statement when executed creates
a thread group by name TG
*/
ThreadGroup tg = new ThreadGroup("TG");
Thread t1 = new Thread(tg,a1,"TG");
Thread t2 = new Thread(tg,b1,"TG");
Daemon Threads:
The threads that get executed at the back ground, in order to support the
main thread are known as Daemon threads. In other words, the job of
daemon thread is to provide support for non-daemon thread. The best
example of a non-daemon thread is main thread. The various examples of
the daemon threads are Garbage Collector, Thread Scheduler etc.
These notes are dedicated to my father Mr. Mir Farooq Ali, Professor and
Ex-Head of the Department of Mathematics (Retired), MJCET and my mom
Mrs. Arifa, Vice Principal and Junior Lecturer in Botany (Retired) Govt.
Falaknuma Junior College. If you have any doubts in this document, you
can call me on 9010149047 or mail me at [email protected],
your queries will be replied within 24 hours.
PROGRAMMING Prepared by Mir Ahmed Ali (Ali Sir),
IN JAVA UNIT I Assistant Professor of CSE, MJCET
71
It must be noted that the priority of a daemon thread is very low i.e. 1. But, if
we want to increase the priority of a daemon thread then it is possible with
the help of a method known as setDaemon () method. For example, during
execution of the program, the main thread priority is 5 and the priority of the
garbage collector is by default 1. If JVM wants to increase the priority of
garbage collector, then it will use a method known as setDaemon () method.
But if programmer, wants to change the priority of a user define thread, then
also it is possible with the help of a method known as setDaemon() method.
The below program shows how to change the value of a user define thread:
These notes are dedicated to my father Mr. Mir Farooq Ali, Professor and
Ex-Head of the Department of Mathematics (Retired), MJCET and my mom
Mrs. Arifa, Vice Principal and Junior Lecturer in Botany (Retired) Govt.
Falaknuma Junior College. If you have any doubts in this document, you
can call me on 9010149047 or mail me at [email protected],
your queries will be replied within 24 hours.
PROGRAMMING Prepared by Mir Ahmed Ali (Ali Sir),
IN JAVA UNIT I Assistant Professor of CSE, MJCET
72
When the above program gets compiled, we don’t get any compile time
error. But if the program gets executed, we get output as shown below:
In the above program, we are creating a user define thread which is a thread
that is sharing the features of main thread. Since, main thread is a non-
daemon thread, the user define thread is also a non-daemon thread. And if
we try to change the value of a non-daemon thread, then it gives an
exception as shown above.
These notes are dedicated to my father Mr. Mir Farooq Ali, Professor and
Ex-Head of the Department of Mathematics (Retired), MJCET and my mom
Mrs. Arifa, Vice Principal and Junior Lecturer in Botany (Retired) Govt.
Falaknuma Junior College. If you have any doubts in this document, you
can call me on 9010149047 or mail me at [email protected],
your queries will be replied within 24 hours.