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

Java UNIT-2

The document covers key concepts of object-oriented programming in Java, focusing on decision-making and branching structures such as selection statements (if and switch) and iteration statements (for, while, and do-while loops). It also discusses classes, objects, constructors, method overloading, static fields and methods, and nesting of methods. The content provides a foundational understanding of how to control program flow and structure in Java programming.

Uploaded by

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

Java UNIT-2

The document covers key concepts of object-oriented programming in Java, focusing on decision-making and branching structures such as selection statements (if and switch) and iteration statements (for, while, and do-while loops). It also discusses classes, objects, constructors, method overloading, static fields and methods, and nesting of methods. The content provides a foundational understanding of how to control program flow and structure in Java programming.

Uploaded by

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

OBJECT-ORIENTED PROGRAMMING USING JAVA

UNIT-II
DECISION MAKING AND BRANCHING:
In java program, control structure is can divide in three parts:
 Selection statement
 Iteration statement
 Jumps in statement
Selection Statement:
Selection statement is also called as Decision making statements because it
provides the decision making capabilities to the statements. In selection statement,
there are two types:
 if statement
 switch statement
These two statements are allows you to control the flow of a program with their
conditions.
if statements :
The ‘if – statement’ is a powerful decision making statement and is used to
control the flow of execution of statements. It is basically a two way decision statement
and is used in conjunction with an expression.
It takes the following form if (text expression)

It allows the computer to evaluate the expression first and then depending on
whether the value of the expression is true or false. It transfers the control to a
particular statements.

true
If test
exp

false

The ‘if statement’ may be implemented in different forms depending on the


complexity of conditions to be tested. The different forms are
1. simple-if statement
2. if-else statement
3. nested if-else statement
4. else-if ladder
1. simple-if statement: The general form of a simple-if statement is
if (test expression)
{
True block statements;
}
Statement X;

P CHITRALINGAPPA 1
OBJECT-ORIENTED PROGRAMMING USING JAVA

The statement block may be a single statement or a group of statements. If the


test expression is true, the statement block will be executed. Otherwise, the statement
jump to the statement X. note that when the condition is true and the statement X are
executed in sequence.
true
If test
exp

True block statement


false

Statement-x

Next statement

2. if – else statement :
The “if – else statement” is an extension of the simple-if statement. The
general form is
if (test expression)
{
True block statements;
}
else
{
False block statements;
}
Statement X;
If the test expression is true,
then the true block statement
immediately following the if
statements are executed. Otherwise
the false block statements are
executed. In either case, either true block or block will be executed, not both. In both the
cases, the control is transferred subsequently to the statement-X.

If test
true exp false

True block statement True block statement

Statement-x

P CHITRALINGAPPA 2
Next statement
OBJECT-ORIENTED PROGRAMMING USING JAVA

3. nested if-else statement : When a series of decisions are involved , we may have to
use more than one if-else statement in nested form. The general form is
if (condition-1)
{
if (condition-2)
{
Statement-1;
}
else
{
Statement-2;
}
}
else
{
Statement-3;
}
Statement-X;
If the condition-1 is false, the statement-3 will be executed. Otherwise it
continuous to perform the second test.
If the condition-2 is true, the statement-1 will be evaluated, otherwise the
statement-2 will be evaluated and then the control is transferred to the statement-X.
1. else – if ladder :
There is another way of putting ifs together when multipath decisions are
involved. A multipath decision is a chain of ifs in which the statement associated with
each else is an if. The general form is

P CHITRALINGAPPA 3
OBJECT-ORIENTED PROGRAMMING USING JAVA

This construct is known as the else-if ladder. The conditions are evaluated from
the top, downwards. As soon as a true condition is found, the statement associated with
it is executed and the control is transferred to the statement-x, when all the ‘n’
conditions became false, then the final else containing the default statement will be
executed
Switch statement:
When a number of conditions occurs in a problem and it is very difficult to solve
such type of complex problem with the help of ladder if statement, then there is need of
such type of statement which should have different automotives or different cases to
solve the problem in simple and easy way. For this purpose switch statement is used. It
is also called case statement because it has different cases and different block. It is also
called multi decision statement
having multiple blocks. The
general syntax of this
statement is
switch(expressions)
{
case value-1:
statement block;
break;
case value-2:
statement block;
break;
--------------------

P CHITRALINGAPPA 4
OBJECT-ORIENTED PROGRAMMING USING JAVA

case value-n:
statement block;
break;
default:
default statement block;
break;
}
statement-x;

Switch

expressio

Expression value-1

Statement-1
Expression value-2

Statement-2
Expression value-3

Statement-3

-n
No match(default block)

Default Statement

Statement-x

NextStatement

Iteration Statement:
The process of repeatedly executing a statements and is called as looping. The
statements may be executed multiple times (from zero to infinite number). If a loop
executing continuous then it is called as Infinite loop. Looping is also called as iterations.
In Iteration statement, there are three types of operation:
 for loop
 while loop
 do-while loop
Looping statements:
When a single statement or a group of statements will be executed again and
again in a program. Then such type processing is called loop. Loop is divided into two
parts.
a) Body of the loop
b) Control of loop.
Control of loop is further divided into two parts. i.e.,
P CHITRALINGAPPA 5
OBJECT-ORIENTED PROGRAMMING USING JAVA

1) Entry control loop


2) Exit control loop
Entry control loop:
In entry control loop first of all condition is checked, if it
is true, then body of the loop is executed. Otherwise, the
control is moved out of the loop i.e., we can exit from the loop,
when the condition becomes false.
Entry controlled loop is also called base loop. The while loop and for loop are
the examples of entry control loop.
Exit control loop:
In the exit control loop, first body of loop is executed and
then condition is checked. If condition is true, then again body of
the loop is executed. If the condition is false, then control will
move out from the loop. Exit control loop is also called derived
loop. Example of this loop is do (loop) statement.
The looping statements are
1) while loop or while - statement
2) do loop or do – statement
3) for loop or for – statement
While loop:
While statement or while loop is an entry control loop. In this, first of all
condition is checked and if it is true, then group of statements or body of loop is
executed. It will execute again and again till condition becomes false. The general syntax
is

while(condition)
{
Block of statements;
}
Statement-x;

Example:

P CHITRALINGAPPA 6
OBJECT-ORIENTED PROGRAMMING USING JAVA

Do – loop:
do statement is exit control loop. It is also called an do-while statement. In this
statement, first body of the loop is executed and then the condition is checked. If
condition is true, then the body of the loop is executed. When condition becomes false,
then it will exit from the loop. The syntax of do-loop is
do{
Block of statement;
}
while(condition);
statement-x;
Example:

for loop :
It is a looping statement, which repeats again and again till it sacrifices the
defined condition. It is one step loop, which initialize, check the condition and
increment or decrement the step in the loop in a single statement. The general syntax is
for(initialization; text condition; increment/decrement)
{
Body of loop;
}
Statement-x;
Example:

Nested for statement:


When a for statement is executed within another first statement is called nested
for statement. The general syntax is
for(initialization; text condition; increment/decrement)
{
Inner Body of the loop;
{
outer Body of the loop;
{ }
} }
Statement-x;
For one value of outer loop, inner loop will repeat up to text condition-2. so,
inner loop will be completed first and then outer loop will be completed. After
completion of inner and outer loop, statement-x will be executed.
Jumps in statement:
Statements or loops perform a set of operations continually until the control
variable will not satisfy the condition. But if we want to break the loop when condition
will satisfy then Java gives a permission to jump from one statement to end of loop or
beginning of loop as well as jump out of a loop. “break” keyword use for exiting from
loop and “continue” keyword use for continuing the loop.
P CHITRALINGAPPA 7
OBJECT-ORIENTED PROGRAMMING USING JAVA

Note: break is used to exit from loop or switch

Continue is used to skip one iteration of loop.

Example:

CLASSES, OBJECTS AND METHODS:

Class:
A class is a collection of objects of similar type. Once a class is defined, any
number of objects can be produced which belong to that class. Class is a model for
creating objects it contains properties and actions. Properties represent variables and
actions represent methods. The same variables and methods are also available in the
object because they are created from the class.
If we take Person class, we can write code in the class that specifies the
properties and actions performed by any person. For example, a person has properties
like name, age, etc. similarly a person can perform actions like talking, walking, etc. so
the class person contains these properties and actions, as shown here.

Object

P CHITRALINGAPPA 8
OBJECT-ORIENTED PROGRAMMING USING JAVA

Creating objects:
Creating an object is also referred to an instantiating an object. Objects in java
are created using the new operator. The new operator creates an object of the specified
class and returns reference to that object.
Syntax:
classname reference_variable = new classname();
Example: Person sai=new Person();

Accessing class members:


Syntax: Example:
reference_variable.data_member; {[Link]; and [Link];}
reference_variable.method(); {[Link]()}

Write a java program to create a class Person with the fields name, age and
include a method talk() and display person details.

CONSTRUCTOR:
A constructor is a similar to a method that is used to initialize the instance
variables. The purpose of a constructor is to initialize the instance variables. A
constructor has the following characteristics:
 The constructor name and class name should be same. but the constructor name
should end with a pair of simple braces.
 A constructor may or may not have parameters.
 A constructor does not return any value, not even void.
 A constructor is automatically called and executed at the time of creating an
object.
 A constructor is called and executed only once per object.
Constructors are mainly two types. They are:

P CHITRALINGAPPA 9
OBJECT-ORIENTED PROGRAMMING USING JAVA

1. Default constructors
2. Parameterized constructor
3. Copy constructor
A constructor does not have any parameters, it is called default constructor. If a
constructor has one or more parameters, it is called parameterized constructor.
Example for default and parameterized constructors:

METHOD OVERLOADING:
Polymorphism is two types i.e., Dynamic polymorphism and Static
polymorphism. The polymorphism exhibit at runtime is called Dynamic polymorphism
and the polymorphism exhibits at compile time is called Static polymorphism. Run time
polymorphism also called as dynamic binding. Dynamic polymorphism is used in the
methods i.e., method overloading and method overriding.
Overloading:
Writing two or more methods in the same class in such a way that each method
has same name but with different method signatures is called method overloading. Here
method signature represents the method name along with method parameters.

P CHITRALINGAPPA 10
OBJECT-ORIENTED PROGRAMMING USING JAVA

STATIC FIELDS AND METHODS:


A static method is declared by using the keyword static. Static methods are called
using [Link](). And a variable declared with the static keyword is
called static variable.
Since instance variable will have a separate copy in each object, when the value
of an instance variable is modified in an object, it does not affect the instance variables
in other objects.
Since a class variable (static variable) will have only one copy in memory and
that is shared by all the objects, any modification to it will also affect other objects.
Note:
In below instance variable program, we create two objects obj1, obj2 to Test
class. When x value in obj1 is incremented, it does not change the x value in obj2.

P CHITRALINGAPPA 11
OBJECT-ORIENTED PROGRAMMING USING JAVA

Note:
In above static variable program, we create two objects obj1, obj2 to Test class.
When x value in obj1 is incremented and also x value incremented in obj2.

NESTING OF METHODS:

A method can be called


by using only its name
by another method of
the same class. This is
known as nesting of
methods.

Output:
Largest value=50

P CHITRALINGAPPA 12

You might also like