0% found this document useful (0 votes)
89 views22 pages

Java Array Cloning and Control Flow

The document provides an overview of Java fundamentals, focusing on control flow statements, arrays, and their properties. It covers decision-making, looping, and branching statements, as well as array manipulation, including cloning and jagged arrays. Additionally, it includes code examples and quizzes to test understanding of the concepts presented.

Uploaded by

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

Java Array Cloning and Control Flow

The document provides an overview of Java fundamentals, focusing on control flow statements, arrays, and their properties. It covers decision-making, looping, and branching statements, as well as array manipulation, including cloning and jagged arrays. Additionally, it includes code examples and quizzes to test understanding of the concepts presented.

Uploaded by

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

INSTITUTE : UIE

DEPARTMENT : CSE
Bachelor of Engineering (Computer Science &
Engineering)

Object Oriented Programming using Java


(23CSP-202 )
TOPIC OF PRESENTATION:
Java Fundamentals Prepared by:
Bhavneet Kaur
Master Subject
Coordinator

DISCOVER . LEARN . EMPOWER


Lecture
Objectives

In this lecture, we will discuss:


• About flow control statements
in java.
• Various concepts of array will
be part of this ppt.

2
Control Flow Statements In Java

Control flow statements, change or break the flow of execution by implementing decision
making, looping, and branching your program to execute particular blocks of code based
on the conditions.
Types of control flow statements:
• Decision-making statements : if-then, if-then-else, switch
• Looping statements : for, while, do-while
• Branching statements : break, continue, return
Comparison of for while and do
while
Predict the output?
What will be the result, if we try to compile and execute the following code?

class Sample{
public static void main(String[]args) {
boolean b = true;
if(b){
[Link](" if block ");
}
else {
[Link](“ else block ");
}
}
Java program to demonstrate the use of infinite for loop
which prints a statement

public class ForExample {


public static void main(String[] args) {
//Using no condition in for loop
for(;;){
[Link]("infinitive loop");
}
}
}
Predict the output
-while loop
What will be the result, if we try to compile and execute the following code snippets
1.
class Sample {
public static void main(String[] args) {
while(false)
[Link]("while loop");
}
}
2.
class Sample {
public static void main(String[] args) {
for( ; ; )
[Link]("For loop");
}
.

Concept of enhanced for loop


• Iterate through elements of arrays.
• Also known as for each or enhanced loop in java.

public class Sample {


public static void main(String[] args) { numbers is a collection
int [] numbers = {10, 20, 30, 40, 50};
for(int i : numbers ) { (like a list, set, or array) of
[Link]( "i: "+i ); integers
}
}
int i is a variable that holds the
} current element from the
numbers collection during each
The loop automatically iterates over all elements in the iteration.
numbers collection without requiring explicit index handling

Output:
i:10
i:20
i: 30
i:40
i:50
Break Statement
Predict the output ?

class BreakDemo
{
public static void main(String args[])
{
int arr[] = {5,10,15,20,25,30};
int n ;
int search = 15;
boolean b = false;
for(n = 0; n < [Link]; n++)
{
if(arr[n] == search)
{
b = true;
break;
}
}
if(b)
[Link]("Number found " +search+" at index of "+n);
else
[Link]("Element is not found"); Output:
} Number found 15 at index of 2
}
Continue statement
Predict the output?

class ContinueDemo
{
Continue Statement public static void main(String []args)
{
Continue statement is used to skip the for (int j = 1; j <= 10; j++)
current iteration of while, for or do-while {
[Link](j+" ");
loop. if (j % 2 != 0)
It causes the immediate jump to next continue;
[Link](" ");
iteration of loop. }
}
}
Output:
1 2
3 4
5 6
7 8
9 10
Program for Continue statement
Predict the output

class ContinueDemo
{
public static void main(String []args)
{
for (int j = 1; j <= 10; j++)
{
[Link](j+" ");
if (j % 2 != 0)
continue;
[Link](" ");
}
}
}
Output:
1 2
3 4
5 6
7 8
9 10
Comparison of Break and
continue
Arrays in Java
• In Java all arrays are dynamically allocated.
• Since arrays are objects in Java, we can find their length using member length.
• To find length of Array, sizeof is used.
• A Java array variable can also be declared like other variables with [] after the data type.
• The variables in the array are ordered and each have an index beginning from 0.
• Java array can be also be used as a static field, a local variable or a method parameter.
• The size of an array must be specified by an int value and not long or short.
• The direct superclass of an array type is Object.
• Every array type implements the interfaces Cloneable and [Link].
Cloning of array

Cloning of an array means creating a new


array that is a copy of the original array.
In Java, you can clone an array using the clone() Syntax of Cloning:
method, which creates a shallow copy of the array.
int[] originalArray = {1, 2, 3, 4, 5};
When an array is cloned using the clone() method, a new array
is created with the same elements as the original array. // Cloning the array
int[] clonedArray = [Link]();
If the array contains primitive types (e.g.,
int, double), the actual values are copied.
If the array contains reference types (e.g., objects), only
the references are copied, not the objects themselves.

14
Demonstrating cloning of array objects
public class ArrayCloneDemo
{
public static void main(String[] args)
{
int ai[] = {1, 2, 3, 4, 5};
int aic[] = ai;
aic[2] = -9;
[Link](aic[2]);
[Link](ai[2]);
aic = [Link]();
OUTPUT:
aic[1] = -15;
[Link](aic[1]); 9
[Link](ai[1]); -9
---
-15
} 2
}
Is an array a primitive type or an object in
Java?
An array in Java is an object.
class Test
{
public static void main(String[] args)
{ int[] ia = new int[3];
[Link]([Link]());
[Link]([Link]().getSuperclass()); } }
}
}
OUTPUT:
class [I class [Link] where the string "[I" is the run-time type signature for the class
object "array with component type int".
ArrayStoreException
• Arrays have the property that an array of one type is assignable to an array of its
supertype, it is possible to play games with the compiler and try to trick it into
storing the wrong kind of object in an array.
• Java may not be able to check the types of all objects that you place into arrays at
compile time.
• In those cases, it’s possible to receive an ArrayStoreException at runtime if you
try to assign the wrong type of object to an array element.
• For example:
String [] strings = new String [10];
Object [] objects = strings; // alias String [] as Object []
objects[0] = new Date(); // Runtime ArrayStoreException!
Anonymous Array in Java
• Java supports the feature of an anonymous array, so you
don't need to declare the array while passing an array to
the method.
//Java Program to demonstrate the way of passing an anonymous array
//to method.
public class TestAnonymousArray{
//creating a method which receives an array as a parameter
static void printArray(int arr[]){
for(int i=0;i<[Link];i++)
[Link](arr[i]);
}
public static void main(String args[]){
printArray(new int[]{10,22,44,66}); //passing anonymous array to method
}}
JAGGED ARRAY
Jagged arrays in java are arrays containing arrays of
different length. Jagged arrays are also
multidimensional arrays.
Jagged arrays in java sometimes are also called
as ragged arrays.

public class JaggedArraysInJava


{
public static void main(String[] args)
{
//One Dimensional Array of length 3
int[] OneDimensionalArray1 = {1, 2, 3};

//One Dimensional Array of length 4


int[] oneDimensionalArray2 = {4, 5, 6, 7};

//One Dimensional Array of length 5


int[] oneDimensionalArray3 = {8, 9, 10, 11, 12};
JAGGED ARRAY (cont…)
//Jagged Two-Dimensional Array
int[][] twoDimensionalArray = {OneDimensionalArray1, oneDimensionalArray2,
oneDimensionalArray3};

//Printing elements of Two Dimensional Array


for (int i = 0; i < [Link]; i++)
{
for (int j = 0; j < twoDimensionalArray[i].length; j++)
{
[Link](twoDimensionalArray[i][j]+"\t");
}
[Link]();
}
}
QUIZ
Q1. Select which of the following are valid array definition
1. int[] a;
a = new int[5];
2. int a[] = new int[5]
3. int a[5] = new int[5];
4. int a[] = {1,2,3};
5. int[] a = new int[]{1,2,3};
6. int[] a = new int[5]{1,2,3,4};
QUIZ
Q2. What will be the result, if we try to compile and execute the following
codes
class Sample {
public static void main(String[] args) {
int[] a = new int[5]{1,2,3};
for(int i : a)
[Link](i);
}
}

You might also like