Lecture-1.3
Lecture-1.3
DEPARTMENT : CSE
Bachelor of Engineering (Computer Science & Engineering)
Java Programming (20CST-218)
TOPIC OF PRESENTATION:
Java Fundamentals
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){
System.out.println(" if block ");
}
else {
System.out.println(“ else block ");
}
}
}
Java program to demonstrate the use of infinite for loop which prints an statement
public class ForExample {
public static void main(String[] args) {
//Using no condition in for loop
for(;;){
System.out.println("infinitive loop");
}
}
}
MCQ based on 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)
System.out.println("while loop");
}
}
2.
class Sample {
public static void main(String[] args) {
for( ; ; )
System.out.println("For loop");
}
}
Concept of enhanced for loop
public class Sample {
public static void main(String[] args) {
int [] numbers = {10, 20, 30, 40, 50};
for(int i : numbers ) {
System.out.println( "i: "+i );
}
}
}
Output:
i:10
i:20
i: 30
i:40
i:50
Comparison of Break and continue
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 < arr.length; n++)
{
if(arr[n] == search)
{
b = true;
break;
}
}
if(b)
System.out.println("Number found " +search+" at index of "+n);
else
System.out.println("Element is not found");
}
}
Break statement
Output:
Continue Statement
Continue statement is used to skip the current iteration of while, for or do-while
loop.
It causes the immediate jump to next iteration of loop.
Program for Continue statement
Predict the output
class ContinueDemo
{
public static void main(String []args)
{
for (int j = 1; j <= 10; j++)
{
System.out.print(j+" ");
if (j % 2 != 0)
continue;
System.out.println(" ");
}
}
}
Output:
1 2
3 4
5 6
7 8
9 10
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 java.io.Serializable.
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;
System.out.println(aic[2]);
System.out.println(ai[2]);
aic = ai.clone();
aic[1] = -15;
System.out.println(aic[1]);
System.out.println(ai[1]);
}
}
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;
System.out.println(aic[2]);
System.out.println(ai[2]);
aic = ai.clone();
aic[1] = -15;
System.out.println(aic[1]);
System.out.println(ai[1]);
}
}
Demonstrating cloning of array objects
OUTPUT:
9
-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];
System.out.println(ia.getClass()); System.out.println(ia.getClass().getSuperclass()); } }
}
}
OUTPUT
class [I class java.lang.Object 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<arr.length;i++)
System.out.println(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 lenghth 3
int[] OneDimensionalArray1 = {1, 2, 3};
//One Dimensional Array of lenghth 4
int[] oneDimensionalArray2 = {4, 5, 6, 7};
//One Dimensional Array of lenghth 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 < twoDimensionalArray.length; i++)
{
for (int j = 0; j < twoDimensionalArray[i].length; j++)
{
System.out.print(twoDimensionalArray[i][j]+"\t");
}
System.out.println();
}
}
}
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)
System.out.println(i);
}
}
Summary:
Books:
1. Balaguruswamy, Java.
2. A Primer, E.Balaguruswamy, Programming with Java, Tata McGraw Hill
Companies
3. John P. Flynt Thomson, Java Programming.
Video Lectures :
https://www.youtube.com/watch?v=dZb5ofv0twk
https://www.youtube.com/watch?v=-oi9Pg0128M
THANK YOU