Download as pdf or txt
Download as pdf or txt
You are on page 1of 24

Unit 1: Introduction To Java

for(initialize counter; condition; increment/decrement counter)


{
// Statements that will execute till the above condition is true
// If the condition is false, controls flows out of this block
}
while (condition)
{
// code block to be executed
}
do{
//code to be executed / loop body
//update statement
}while (condition);
⚫ The switch
statement is a
multi-way branch
statement. In
simple
words, the Java switch
statement executes
one statement from
multiple conditions. It
is like
an if-else-if
ladder statement.
 The Java if statement is used to test the condition.
 It checks boolean condition: true or false.
 There are various types of if statement in Java.
 if statement
◦ if-else statement
◦ if-else-if statement
◦ if-else-if ladder
◦ nested if statement
if(condition){
//code to be executed
}
if(condition){
//code if condition is
true
}else{
//code if condition is
false
}
if(condition1){
//code to be executed if
condition1 is true
}else if(condition2){
//code to be executed if
condition2 is true
}
else if(condition3){
//code to be executed if
condition3 is true
}
...
else{
//code to be executed if all
the conditions are false
}
if(condition){
//code to be executed

if(condition){
//code to be exe
cuted
}
}
 An array is a collection of similar type of elements which has
contiguous memory location.
 Java array is an object which contains elements of a similar data
type.
 Additionally, The elements of an array are stored in a contiguous
memory location.
 Storage of arrays helps us randomly access the elements of an array.
 Advantages:
 Code Optimization
 Random access
 Easy to manipulate and store large data.
 Disadvantages:
 Can store a single type of primitives only.
 Fixed size. Can not be increased or decrease once declared.

Way Of Increase An Array Size:

 Increase an Array Size by Creating Another New Array in Java


 Increase the Array Size Using the Arrays.copyOf() Method in Java
 Increase the Array Size Using the ArrayList Array in Java
import java.util.Arrays;

public class Main


{
public static void main(String[] args) {
int[] numberArray = { 12, 24, 63, 45 };
System.out.println("Array before ReSize: ");
for (int i = 0; i < numberArray.length; i++) {
System.out.println(numberArray[i]);
}

numberArray = Arrays.copyOf(numberArray, 6);


numberArray[4] = 11;
numberArray[5] = 55;

System.out.println("Array after ReSize: ");


for (int i = 0; i < numberArray.length; i++) {
System.out.println(numberArray[i]);
}
}
 Types of Array in java:
There are two types of array.
1. Single Dimensional Array
2. Multidimensional Array

Single Dimensional Array:


Syntax:
dataType[] arr; (or)
dataType arr[];
Instantiating an Array:
var-name = new type [size];
Example:
int[] intArray = new int[20];
 Array Literal
◦ In a situation where the size of the array and variables of the array
are already known, array literals can be used.

Example:
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 };
// Declaring array literal

 Arrays of Objects
 An array of objects is created like an array of primitive-type data
items in the following way.

Example:
Stud[] s=new Stud[5];
Multidimensional Array:
Syntax:
dataType[][] arr; (or)
dataType arr[][];
Example:
int[][] arr = new int[20][30];
 The java command-line argument is an argument i.e. passed at the
time of running the java program.
 The arguments passed from the console can be received in the java
program and it can be used as an input.
 So, it provides a convenient way to check the behaviour of the
program for the different values. You can pass N (1,2,3 and so on)
numbers of arguments from the command prompt.
class CmdExample
{
public static void main(String[] args)
{
System.out.println("Your first argument is: "+args[0]);
}
}

You might also like