0% found this document useful (0 votes)
15 views

Module 1 Continue

Uploaded by

kumar
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)
15 views

Module 1 Continue

Uploaded by

kumar
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
You are on page 1/ 49

MODULE 1

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

Unary postfix a++ ,b--

prefix ++a ,--b

Arithmetic multiplicative */%

additive +-

Shift shift << >> >>>

Relational comparison < > <= >= instanceof

equality == !=

Bitwise bitwise AND &

bitwise exclusive OR ^

bitwise inclusive OR |

Logical logical AND &&

logical OR ||

Ternary ternary ?:

Assignment assignment = += -= *= /= %= &= ^= |= <<= >>=


>>>=
Java Unary Operator Example: ++ and --
public class OperatorExample{
public static void main(String args[]){
int x=10;
System.out.println(x++);
System.out.println(++x);
System.out.println(x--);
System.out.println(--x);
}}
Java Arithmetic Operators

• Java arithmetic operators are used to perform addition, subtraction, multiplication,


and division. They act as basic mathematical operations.
• Java Arithmetic Operator Example
public class OperatorExample{
public static void main(String args[]){
int a=10;
int b=5;
System.out.println(a+b);//15
System.out.println(a-b);//5
System.out.println(a*b);//50
System.out.println(a/b);//2
System.out.println(a%b);//0
}}
Java Ternary Operator
• Java Ternary operator is used as one line replacement for if-then-else statement and
used a lot in Java programming. It is the only conditional operator which takes three
operands.
• Java Ternary Operator Example

public class OperatorExample


{
public static void main(String args[])
{
int a=2;
int b=5;
int min=(a<b)?a:b;
System.out.println(min);
}
}
Output:
2
Java AND Operator Example: Logical && and Bitwise &

• 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

public class OperatorExample


{
public static void main(String args[])
{
int a=10;
int b=20;
a+=4;//a=a+4 (a=10+4)
b-=4;//b=b-4 (b=20-4)
System.out.println(a);
System.out.println(b);
}
}
Relational
Operator
Operators
Name Example

== Equal to x == y

!= Not equal x != y

> Greater than x>y

< Less than x<y

>= Greater than or equal to x >= y

<= Less than or equal to x <= y


Example
public class Main {
public static void main(String[] args) {
int x = 5;
int y = 3;
System.out.println(x > y); // returns true because 5 is greater than 3
}
}
• In java, the selection statements are also known as
decision making statements or branching statements
or conditional control statements. The selection
statements are used to select a part of the program to
be executed based on a condition. Java provides the
following selection statements.
• if statement
• if-else statement
• nested if statement
• if-else if statement
• switch statement
The if Statement
• In java, we use the if statement to test a condition
and decide the execution of a block of statements
based on that condition result.
• The if statement checks, the given condition then
decides the execution of a block of statements.
• If the condition is True, then the block of statements
is executed and if it is False, then the block of
statements is ignored.
• The syntax and execution flow of if the statement is as follows.
import java.util.Scanner;

Example {
public class IfStatement

public static void main(String[] args)


{
Scanner read = new Scanner(System.in);
System.out.print("Enter any number: ");
int num = read.nextInt();
if((num % 5) == 0)
{
System.out.println("Given number is divisible by 5!!");
}
System.out.println("We are outside the if-block!!!");
}
if-else statement in java

• 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;

for (int i = 1; i <= n; ++i)


{
System.out.println(“Welcome all");
}
}
}
Output
Welcome all
Welcome all
Welcome all
Welcome all
Welcome all
While loop
while loop
Java while loop is used to run a specific code until a certain condition is
met.
The syntax of the while loop is:
while (testcondition)
{
// body of loop
}
Here,
A while loop evaluates the textExpression inside the parenthesis ().
If the textExpression evaluates to true, the code inside
the while loop is executed.
The textExpression is evaluated again.
This process continues until the textExpression is false.
When the textExpression evaluates to false, the loop stops.
// Program to display numbers from 1 }
to 5
class Whileloop
{
public static void main(String[] args)

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

The do...while loop is similar to while loop. However, the body


of do...while loop is executed once before the test expression is
checked. For example,
do
{
// body of loop
} while(text_condition);
Do-while loop
• The body of the loop is executed at first. Then the text_condition is
evaluated.
• If the text condition evaluates to true, the body of the loop inside
the do statement is executed again.
• This process continues until the text condition evaluates to false.
Then the loop stops.
// Java Program to display numbers from 1 to 5

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.

• To declare an array, define the variable type


with square brackets:

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

There are two types of array.


Single Dimensional Array
Multidimensional Array
Single Dimensional Array in Java
• Syntax to Declare an Array in Java
• dataType[] arr; (or)
• dataType []arr; (or)
• dataType arr[];
• Instantiation of an Array in Java
• Array_name=new datatype[size];
• Example
• int a[ ]=new int[5];//declaration and instantiation
Example

//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

• A multidimensional array is an array of arrays. That is, each element


of a multidimensional array is an array itself.
For example,
int [ ][ ] a = {{1, 4, 3}, {2 ,1,4} };
public class Tester
{
public static void main(String[ ] args)
{
int[ ][ ] multidimensionalArray = {{1,2},{2,3}, {3,4} };
for(int i = 0 ; i < 3 ; i++)
{
for(int j = 0 ; j < 2; j++)
{
System.out.print(multidimensionalArray[i][j] + " ");
} Output
System.out.println( ); 1 2
} 2 3
} 3 4
}

You might also like