Module 1 Continue
Module 1 Continue
Operators in Java
• Operator in Java is a symbol that is used to perform
operations. For example: +, -, *, / etc.
• There are many types of operators in Java which are
given below:
Unary Operator,
Arithmetic Operator,
Shift Operator,
Relational Operator,
Bitwise Operator,
Logical Operator,
Ternary Operator and
Assignment Operator.
Operator Type Category Precedence
additive +-
equality == !=
bitwise exclusive OR ^
bitwise inclusive OR |
logical OR ||
Ternary ternary ?:
• The logical && operator doesn't check the second condition if the first
condition is false. It checks the second condition only if the first one is
true.
• The bitwise & operator always checks both conditions whether first
condition is true or false.
public class OperatorExample
{
public static void main(String args[])
{
int a=10;
int b=5;
int c=20;
System.out.println(a<b&&a<c);//false && true = false
System.out.println(a<b&a<c);//false & true = false
}
}
Output:
false false
Java Assignment Operator
• Java assignment operator is one of the most common operators. It is used to
assign the value on its right to the operand on its left.
• Java Assignment Operator Example
== Equal to x == y
!= Not equal x != y
Example {
public class IfStatement
• In java, we use the if-else statement to test a condition and pick the
execution of a block of statements out of two blocks based on that
condition result.
• The if-else statement checks the given condition then decides which
block of statements to be executed based on the condition result.
• If the condition is True, then the true block of statements is executed
and if it is False, then the false block of statements is executed
• The syntax and execution flow of if-else statement is as follows.
import java.util.Scanner;
public class IfElseStatementTest
{
public static void main(String[] args)
{
Scanner read = new Scanner(System.in);
System.out.print("Enter any number: ");
int num = read.nextInt();
if((num % 2) == 0)
{
System.out.println("Given number is EVEN number!!");
}
else
{
System.out.println("Given number is ODD number!!");
}
}
Switch statement in java
Using the switch statement, one can select only one
option from more number of options very easily.
In the switch statement, we provide a value that is to
be compared with a value associated with each
option.
Whenever the given value matches the value
associated with an option, the execution starts from
that option. In the switch statement, every option is
defined as a case.
import java.util.Scanner;
public class SwitchStatementTest case 3:
System.out.println(“ u entered THREE") ;
{
break ;
public static void main(String[] args) case 4: System.out.println(“ u enterd FOUR") ;
{ break ;
Scanner read = new Scanner(System.in); case 5: System.out.println(“u enterd FIVE") ;
break ;
System.out.print("Press any digit: "); case 6: System.out.println(“u enterd SIX") ;
int value = read.nextInt(); break ;
switch( value ) case 7:
System.out.println(“ u enterd SEVEN") ;
{ break ;
case 0: System.out.println("ZERO") ; case 8:
break ; System.out.println(“u enterd EIGHT") ;
break ;
case 1: case 9:
System.out.println(“ u entered ONE") ; System.out.println(“u enterd NINE") ;
break ; break ;
default: System.out.println("Not a Digit") ;
case 2:
}
System.out.println(“ u entered TWO") ; }
LOOPS IN JAVA
• The java programming language provides a set of
iterative statements that are used to execute a
statement or a block of statements repeatedly as long
as the given condition is true. The iterative statements
are also known as looping statements or repetitive
statements. Java provides the following iterative
statements.
while statement
do-while statement
for statement
Java for Loop
Java for loop is used to run a block of code for a certain number of times. The syntax
of for loop is:
for (initialization; testcondition; increment/decrement)
{
// body of the loop
}
Here,
The initialization initializes and/or declares variables and executes
only once.
The condition is evaluated. If the condition is true, the body of
the for loop is executed.
The increment /decrement updates the value of initial value.
The condition is evaluated again. The process continues until
the condition is false.
// Example Program to print a text 5 times
class Forloop
{
public static void main(String[] args)
{
int n = 5;
Output
{
1
// declare variables
2
int i = 1, n = 5;
3
// while loop from 1 to 5
4
while(i <= n)
5
{
System.out.println(i); i++;
}
}
Java do...while loop
import.java.util.Scanner;
class Number Output
1
{
2
public static void main(String[] args)
3
{ 4
int i = 1, n = 5; 5
do
{
System.out.println(i);
i++;
} while(i <= n);
}
ARRAYS
Java Arrays
• Arrays are used to store multiple values in a single
variable, instead of declaring separate variables for
each value.
• An array is a collection of similar type of elements
which has contiguous memory location.
String[] cars;
Declaring Arrays
The general form of a one-dimensional array declaration is
Data type var-name[];
OR
Datatype[] var-name;
Example
int[] a;
Or
int a[];
To insert values to it, we can use an array literal - place the values in a
comma-separated list, inside curly braces:
• String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
To create an array of integers, you could write:
• int[] num = {10, 20, 30, 40};
Initializing an array and assigning values:
An array can also be initialized during declaration.
class HelloWorld
{
public static void main( String args[] )
{
int[] array = {11,12,13,14,15};
//Printing the elements of array
for (int i =0;i < 5;i++)
{
System.out.println(array[i]);
}
}
}
Initializing an array after a declaration:
An array can also be initialized after declaration.
class HelloWorld
{
public static void main( String args[] )
{
int[ ] array;
array = new int[ ]{1,2,3,4,5};
for (int i =0;i < 5;i++)
{
System.out.println(array[i]);
}
}
}
Advantages
Code Optimization: It makes the code optimized, we
can retrieve or sort the data efficiently.
Random access: We can get any data located at an
index position.
Disadvantages
Size Limit: We can store only the fixed size of
elements in the array. It doesn't grow its size at
runtime. To solve this problem, collection framework
is used in Java which grows automatically.
Types of Array in java
//Java Program to illustrate how to declare, instantiate, initialize and print array values
class Testarray
{
public static void main(String args[])
{
int a[ ]=new int[5];//declaration and instantiation
a[0]=10;//initialization
a[1]=20;
a[2]=70;
a[3]=40;
a[4]=50;
for(int i=0;i<a.length;i++)
System.out.println(a[i]);
}
}
Output:
10 20 70 40 50
Multidimensional Arrays