0% found this document useful (0 votes)
2 views29 pages

Core Java - Lecture 2: Java Operators

The document provides an overview of operators and control statements in Java, including types of operators, examples of their usage, and various control statements like if, if-else, switch, and nested if statements. It also discusses modifiers and provides examples of their implementation. Additionally, it includes assignments for practical application of the concepts covered.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views29 pages

Core Java - Lecture 2: Java Operators

The document provides an overview of operators and control statements in Java, including types of operators, examples of their usage, and various control statements like if, if-else, switch, and nested if statements. It also discusses modifiers and provides examples of their implementation. Additionally, it includes assignments for practical application of the concepts covered.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

AIIT

Core Java

Lecture-2

1
Operators in Java AIIT

Operator in Java is a symbol which is used to perform


operations. For example: +, -, *, / etc.

There are many types of operators in Java which are


given below:

2
Operators in Java AIIT

Operator Type Category Precedence


Unary postfix expr++ expr--
prefix ++expr --expr +expr -expr ~ !

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 = += -= *= /= %= &= ^= |= <<=
>>= >>>=
3
Operator Examples AIIT

class OperatorExample{
public static void main(String args[]){
int x=10;
[Link](x++);//10 (11)
[Link](++x);//12
[Link](x--);//12 (11)
[Link](--x);//10
}
}

class OperatorExample{
public static void main(String args[]){
int a=10;
int b=10;
[Link](a++ + ++a);//10+12=22
[Link](b++ + b++);//10+11=21
}
}

4
~ and ! AIIT

class OperatorExample{
public static void main(String args[]){
int a=10;
int b=-10;
boolean c=true;
boolean d=false;
[Link](~a);//-11 (minus of total positive value which
starts from 0)
[Link](~b);//9 (positive of total minus, positive starts from 0)
[Link](!c);//false (opposite of boolean value)
[Link](!d);//true
}
}

5
<< Vs. >> AIIT

class OperatorExample{
public static void main(String args[]){
[Link](10<<2); //10*2^2=10*4=40
[Link](10<<3); //10*2^3=10*8=80
[Link](20<<2); //20*2^2=20*4=80
[Link](15<<4); //15*2^4=15*16=240
}
}

class OperatorExample{
public static void main(String args[]){
[Link](10>>2); //10/2^2=10/4=2
[Link](20>>2); //20/2^2=20/4=5
[Link](20>>3); //20/2^3=20/8=2
}
}

6
Logical && vs Bitwise & AIIT

class OperatorExample{
public static void main(String args[]){
int a=10;
int b=5;
int c=20;
[Link](a<b&&a++<c);//false && true = false
[Link](a);//10 because second condition is not checked
[Link](a<b&a++<c);//false && true = false
[Link](a);//11 because second condition is checked
}
}

7
Ternary Operator AIIT

Ternary operator is used as one liner replacement for if-then-else


statement and used a lot in Java programming. it is the only conditional
operator which takes three operands.

class OperatorExample{
public static void main(String args[]){
int a=10;
int b=5;
int min=(a<b)?a:b;
[Link](min);
}
}

8
Modifiers AIIT

Modifiers are keywords that you add to those definitions to change their
meanings.

Java language has a wide variety of modifiers, including the following :


Java Access Modifiers
Non Access Modifiers

To use a modifier, you include its keyword in the definition of a class,


method, or variable. The modifier precedes the rest of the statement.

E.g.: public class className {

9
Access Control Modifiers AIIT

Java provides a number of access modifiers to set


access levels for classes, variables, methods and
constructors.
The four access levels are −

-Visible to the package, the default. No modifiers are needed.


-Visible to the class only (private).
-Visible to the world (public).
-Visible to the package and all subclasses (protected). 10
Non-Access Modifiers AIIT

• Java provides a number of non-access modifiers to achieve many other


functionality.

• The static modifier for creating class methods and variables.

• The final modifier for finalizing the implementations of classes, methods,


and variables.

• The abstract modifier for creating abstract classes and methods.

• The synchronized and volatile modifiers, which are used for threads.
11
Control Statements: If Statement AIIT

The 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 ladder
nested if statement

if Statement
The Java if statement tests the condition. It executes the if block if condition is true.
Syntax:

if(condition){
//code to be executed
}

12
If Statement
AIIT

13
Example AIIT

//Java Program to demonstate the use of if statement.


public class IfExample {
public static void main(String[] args) {
//defining an 'age' variable
int age=20;
//checking the age
if(age>18){
[Link]("Age is greater than 18");
}
}
}

Output: Age is greater than 18

14
if-else Statement AIIT

The Java if-else statement also tests the condition. It executes the if
block if condition is true otherwise else block is executed.

Syntax:

if(condition){
//code if condition is true
}else{
//code if condition is false
}
15
Example AIIT

//A Java Program to demonstrate the use of if-else statement.


//It is a program of odd and even number.
public class IfElseExample {
public static void main(String[] args) {
//defining a variable
int number=13;
//Check if the number is divisible by 2 or not
if(number%2==0){
[Link]("even number");
}else{
[Link]("odd number");
}
}
}

Output: odd number

16
if-else-if ladder Statement AIIT
The if-else-if ladder statement executes one condition from multiple statements.

Syntax:

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
}

17
Nested if statement AIIT

The nested if statement represents the if block within another if


block. Here, the inner if block condition executes only when outer if
block condition is true.
Syntax:
if(condition){
//code to be executed
if(condition){
//code to be executed
}
}

18
Example AIIT

//Java Program to demonstrate the use of Nested If Statement.


public class JavaNestedIfExample2 {
public static void main(String[] args) {
//Creating two variables for age and weight
int age=25;
int weight=48;
//applying condition on age and weight
if(age>=18){
if(weight>50){
[Link]("You are eligible to donate blood");
} else{
[Link]("You are not eligible to donate blood");
}
} else{
[Link]("Age must be greater than 18");
}
} }

Output: You are not eligible to donate blood

19
Switch Statement AIIT

• The Java switch statement executes one statement from multiple


conditions.

• It is like if-else-if ladder statement.

• The switch statement works with byte, short, int, long, enum types,
String and some wrapper types like Byte, Short, Int, and Long.

• Since Java 7, you can use strings in the switch statement.

• There can be one or N number of case values for a switch expression.

20
Switch Statement AIIT

• The case value must be of switch expression type only. The case
value must be literal or constant. It doesn't allow variables.

• The case values must be unique. In case of duplicate value, it renders


compile-time error.

• The Java switch expression must be of byte, short, int, long (with its
Wrapper type), enums and string.

• Each case statement can have a break statement which is optional.


When control reaches to the break statement, it jumps the control
after the switch expression. If a break statement is not found, it
executes the next case.

• The case value can have a default label which is optional.


21
Example AIIT

public class SwitchExample {


public static void main(String[] args) {
//Declaring a variable for switch expression
int number=20;
//Switch expression
switch(number){
//Case statements
case 10: [Link]("10");
break;
case 20: [Link]("20");
break;
case 30: [Link]("30");
break;
//Default case statement
default:[Link]("Not in 10, 20 or 30");
}
}
}
Output: 20

22
Switch Statement AIIT

Java allows us to use strings in switch expression since Java SE 7. The case
statement should be string literal.

//Java Program to demonstrate the use of Java Switch


//statement with String
public class SwitchStringExample {
public static void main(String[] args) {
//Declaring String variable
String levelString="Expert";
int level=0;

23
Switch Statement AIIT

//Using String in Switch expression


switch(levelString){
//Using String Literal in Switch case
case "Beginner": level=1;
break;
case "Intermediate": level=2;
break;
case "Expert": level=3;
break;
default: level=0;
break;
}
[Link]("Your Level is: "+level);
}

Output: Your Level is: 3

24
Use of Enum AIIT

//Java Program to demonstrate the use of Enum


//in switch statement

public class JavaSwitchEnumExample {


public enum Day { Sun, Mon, Tue, Wed, Thu, Fri, Sat }
public static void main(String args[])
{
Day[] DayNow = [Link]();
for (Day Now : DayNow)
{
switch (Now)
{
case Sun:
[Link]("Sunday");
break;
case Mon:
[Link]("Monday");
break;
case Tue:
[Link]("Tuesday");
break;
25
Use of Enum AIIT

case Wed:
[Link]("Wednesday");
break;
case Thu:
[Link]("Thursday");
break;
case Fri:
[Link]("Friday");
break;
case Sat:
[Link]("Saturday");
break;
}
}
}
}
26
ASET
Some Assignments ☺
int a_number=1; // (range: 1 to 5 including both)
Print the value of a_number in word. For example, it should print “Four” if a_number
contains 4.

1. Use equality ‘= =’ operator.


2. Do not use equality ‘= =’ operator.

3. WAP to check whether a year entered by user is leap year or not. (Hint: A year is
leap, if it is divisible by 4 and 400. But, not by 100.)

4. WAP to create a Grading System for Students. The program will print the grade
according to the marks of student and grading system should include following
grades fail, D grade, C grade, B grade, A grade and A+. (Hint: use if-else ladder)

5. WAP to check whether a number is POSITIVE, NEGATIVE or ZERO.


ASET
End

Thank you…

nlp-ai@[Link]
REFRENCES AIIT

1. Java : The Complete Reference , Patrick Naughton, Herbert Schildt

2. Thinking in Java ([Link] , Bruce Eckel


3. [Link]
4. [Link]

29

You might also like