0% found this document useful (0 votes)
2 views19 pages

Computer (Java)

The document outlines different types of statements in Java, including declaration, expression, control flow, and block statements, along with their definitions and examples. It also discusses entry-controlled and exit-controlled loops, basic operations on arrays such as searching, sorting, insertion, deletion, and merging, as well as components of methods like method headers, access specifiers, return types, and function overloading. Overall, it serves as a comprehensive guide to fundamental Java programming concepts.
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)
2 views19 pages

Computer (Java)

The document outlines different types of statements in Java, including declaration, expression, control flow, and block statements, along with their definitions and examples. It also discusses entry-controlled and exit-controlled loops, basic operations on arrays such as searching, sorting, insertion, deletion, and merging, as well as components of methods like method headers, access specifiers, return types, and function overloading. Overall, it serves as a comprehensive guide to fundamental Java programming concepts.
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

DIFFERENT TYPES OF STATEMENT IN

JAVA
1. Declaration Statement

Definition:
A declaration statement is used to declare variables, constants, or objects along with their data
types. It informs the compiler about the type and name of the variable.

Example:

2. Expression Statement

Definition:
An expression statement is a statement that performs an action such as assigning a value, calling
a method, or creating an object. It always ends with a semicolon.

Example:

3. Control Flow Statements

Definition:
Control flow statements are used to change the normal sequence of execution in a program. They
allow decisions, looping, and jumps.
(a) Decision-making statements

Used to choose one path of execution among multiple.


Example:

(b) Looping statements

Used to repeat a block of code multiple times until a condition is met.


Example:

(c) Jump statements

Used to transfer control from one part of the program to another.


Example:
4. Block Statement

Definition:
A block statement is a group of one or more statements enclosed in curly braces { }. It is used to
combine multiple statements where only one is allowed.

Example:

Entry-Controlled and Exit-Controlled Loops


Entry-Controlled Loop (Pre-Test Loop)

An entry-controlled loop is a loop in which the condition is checked before the loop body
executes.
This means the statements inside the loop will run only if the condition is true.
If the condition is false at the start, the loop body will not execute even once.

Types of Entry-Controlled Loops in Java

1. for loop
Used when the number of iterations is known. Condition checked before each repetition.

Example:
For (a=1;a<=10;a++)
{
[Link] (a);
S=s+a;
}
2. while loop
Used when the number of iterations is unknown. Condition checked before the loop body
runs.

Example:

int p=1000,r-10;
while (p<=10000)
{
si = (p*r)/100.0;
p = p+si;
}

Exit-Controlled Loop (Post-Test Loop)

An exit-controlled loop is a loop in which the condition is checked after the loop body
executes.
This means the statements inside the loop will run at least once, even if the condition is false in
the beginning.
Type of Exit-Controlled Loop in Java

1. do-while loop
Loop body executes first → then condition is checked → continues based on condition.

Example:

int p=1000,r-10;
double si;
do
{
Si = (p*r)/100.0;
P = p+si;
}
While (p<=10000);

Different Character Functions


Basic Operations on Arrays
1. Searching

Definition: Searching is the process of finding the location of a given element in an array.

Explanation: It checks whether the element exists or not, and if it exists, at which index it is
present.

 Linear Search: Each element is checked one by one. Works for both sorted and unsorted
arrays.
 Binary Search: The array is divided into halves repeatedly. Works only for sorted arrays
and is much faster.

2. Sorting

Definition: Sorting is the process of arranging the elements of an array in a specific order,
usually ascending or descending.

Explanation: It helps in organizing data, making it easier to understand and search. Sorting
also improves efficiency for searching methods like binary search.
Common methods include Bubble Sort, Selection Sort, Insertion Sort, Merge Sort, and Quick
Sort.
3. Insertion

Definition: Insertion is the process of adding a new element into an array at a specific position.

Explanation: To insert an element, the existing elements from that position onward are shifted
to make space. In Java, since arrays have fixed size, sometimes a new larger array must be
created to accommodate the additional element.

4. Deletion

Definition: Deletion is the process of removing an element from an array.

Explanation: After deletion, the elements after that index are shifted left to fill the empty
space. The size of the array remains fixed, so the last position may be unused or set to a default
value.

5. Merging

Definition: Merging is the process of combining two or more arrays into a single array.

Explanation: The elements of one array are copied into another. If the arrays are already sorted,
merging can be done in a way that maintains the sorted order. This is especially useful in
algorithms like Merge Sort.

Different String Functions


Different Components of Methods
1. Method Header (Function Signature)

The method header, also called the function signature, is the first part of a method and defines its
identity. It contains all the essential information about the method, such as its name, return type,
access specifier, and parameters. The header tells the compiler and the programmer what the
method does and what inputs and outputs it expects. Without a proper method header, the
method cannot be called or recognized in a program.

2. Access Specifier

The access specifier determines who can access the method in a program. Common access
specifiers in Java include public, private, and protected. A public method can be accessed
from anywhere, a private method is restricted to the same class, and a protected method is
accessible within the same package or by subclasses. Access specifiers are important for
controlling the visibility and security of a method.
3. Return Type

The return type specifies the type of value a method will return after execution. For example, a
method may return int, double, String, or any other data type. If the method does not return
any value, the return type is declared as void. This component is crucial because it informs the
caller what kind of result to expect from the method.

4. Method Name

The method name is a meaningful identifier used to call the method whenever its functionality
is needed. It should follow Java naming conventions, usually starting with a lowercase letter and
using camelCase for multiple words. A good method name describes what the method does,
making the code readable and easier to maintain.

5. Parameter List

The parameter list contains the input values that a method can use for its processing. Parameters
are specified within parentheses in the method header and can include zero, one, or more inputs,
each with a type and name. Parameters allow methods to perform tasks on different data without
rewriting the code.

6. Method Block (Body)

The method block is the code enclosed in curly braces { } that defines what the method
actually does. It contains all the statements, calculations, or instructions that are executed when
the method is called. The method block is the main part of a method where the functionality is
implemented.

7. Return Statement

The return statement is used to send a value back to the caller if the method has a return type
other than void. It allows the method to provide a result after execution. If the method does not
return a value, the return statement can be omitted. Proper use of the return statement ensures
that methods can interact with other parts of the program efficiently.
Function Overloading

Function Overloading

Java language facilitates the designing of a number of functions with the same function name.
However, their parametric types must be different. Such functions are called Overloaded
Functions and the process is said to be Function Overloading.

You have already learnt that Polymorphism is the process of using a function for multiple
purposes. In Java, through function overloading implements Polymorphism. On invoking the
function, it finds the best match of the arguments with the function parameters (i.e., types and
number of arguments should match) to decide which of the overloaded function is to be called
for operation.

This system is known as Early Binding or Static Binding.


Programs
Program 1
Program 2
Program 3
Program 5

You might also like