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

4.1 Operators Operands Expressions 2

This document provides an overview of operators, expressions, and operands in Java. It discusses: - Classification of operators based on number of operands: unary, binary, ternary - Examples of unary, binary, and ternary operators - Definition of operands and how they are used in expressions - Evaluation of expressions and whether they return boolean or numeric values - Examples of arithmetic, relational, and logical expressions

Uploaded by

Sumedha Reddy
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)
25 views

4.1 Operators Operands Expressions 2

This document provides an overview of operators, expressions, and operands in Java. It discusses: - Classification of operators based on number of operands: unary, binary, ternary - Examples of unary, binary, and ternary operators - Definition of operands and how they are used in expressions - Evaluation of expressions and whether they return boolean or numeric values - Examples of arithmetic, relational, and logical expressions

Uploaded by

Sumedha Reddy
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
You are on page 1/ 26

Unit 4

Java Operators and Expressions


Where are you headed?
Content:
● Introduction to operators, operands and expressions
● Classification of operators
● Classification based on number of operands - Unary, Binary and Ternary
● Unary operators
● Binary operators
● Ternary operator

Skills covered:
● learn about the classifications of operators and the different operators that are present
in java and their purposes.
● learn to evaluate expressions.
● learn about implicit and explicit conversions of data types in expressions.
Observe: Hook Activity
– 14 % 3 = – 2 ● The value of relational expression is either true or
false.
– 14 % – 3 = – 2 ● * Only if the sum of values of a and b is equal to
the sum of values of c and d.
14 % – 3 = 2

● In modulo division the sign is the sign of Expression value


the first operand
4.5 <= 10 TRUE

4.5 < –10 FALSE

– 35 >= 0 FALSE

10 < 7 + 5 TRUE

a+b==c+d TRUE
Observations
● The examples given above are known as ‘Expressions’.
● An expression always evaluates (or is processed) to a result.
● One example of an arithmetic expression is 5 + 2.
● An expression consists of operands (5 and 2 in the sample expression), operators (+
sign in the sample expression).
● The examples given earlier evaluated only to boolean, i.e. true or false values.
● Expressions with logical and or relational operators always evaluate to boolean
answers.
● Expressions with arithmetic operations always evaluate to a numeric value.
What are operators?
Java supports a rich set of operators. An operator is a symbol that tells the computer to
perform certain mathematical or logical manipulations. Operators are used in programs to
manipulate data and variables. They usually form a part of mathematical or logical
expressions.Java's operators are classified by their number of operands:

● A unary operator has one operand, for example unary minus (e.g., -5).
● A binary operator has two operands, examples are multiplication and addition.
● A ternary operator has three operands; an example is the conditional operator (?:).

Java's operators are also classified by position:


● A prefix operator is a unary operator that precedes its operand (e.g., -5,--5,++5).
● A postfix operator is a unary operator that follows its operand (e.g., age++; -- add 1
to age's numeric value).
What are operands

A value involved in an operation is called an operand.

A unary operator is an operator that performs its operation on only one operand. An
operator is referred to as binary if it operates on two operands
Expressions
An expression is a combination of operators, constants and variables. An expression may consist
of one or more operands, and zero or more operators to produce a value.
Operators are classified
Unary operators
● Unary operators require only
one operand.
● Examples of unary operators
include logical not, increment
operator, decrement operator etc.
● The meaning of these operators
will be dealt with in detail later in
the unit.
● Examples of expressions with
unary operation include: -a,
++5,6++,--7, 8-- etc.
How does the postfix and prefix work
Try it using both increment and decrement operators.
Decrement
Binary Operators
● Binary operators require two operands to perform the operation. Examples include all
arithmetic operators, relational operators, logical operators etc.
● Example of expressions with binary operators include: 4 * 6, 9 /3, a > b, (3 > 5) && (4 <= 0) etc
Ternary Operators
Ternary statements get their name because they take three conditions. A ternary
operator evaluates whether a statement is true or false and returns a specified
value depending on the result of the operator.

Here is the syntax for a ternary operator in Java:

variable = (expression) ? expressionIsTrue : expressionIsFalse;


Example for ternary operators
public class EvaluateAge {

public static void main(String[] args) {

int age = 22;

String result = (age >= 16) ? "This user is over 16." : "This user is under 16."

System.out.println(result);

}
Try to code
● Write a program to check if a number is divisible by 5 and display a suitable
output.
● Write a program to check if an integer is positive, negative using the ternary
operator.
Ex 2
Assignment 1
Organize all the different types of operators based on the number of operators in
a Bubble Map With examples. This can be done in your notebooks.

Assignment 2:
Read about increment and decrement operators from their textbook.
Solve examples from page 70 of Text Book.
Ex 1,2,3 8,10.
Resources
Operators

Bitwise and shift operators

Resource 2
Pg 88. Textbook
Entry Ticket: Quiz Link
Take up this quick concept refresher quiz.
Arithmetic Operators
Exercise programs to solve arithmetic operations.
public class OperatorExample public class OperatorExample
{
public static void main(String {
args[])
public static void main(String args[])
{
int a=10; {
int b=5;
System.out.println(a+b);//15 System.out.println(10*10/5+3-1*4/2);
System.out.println(a-b);//5 }
System.out.println(a*b);//50
System.out.println(a/b);//2 }
System.out.println(a%b);//0
}
}
Assignment 3
Write a Java program that performs all the arithmetic operations (take 2
numbers and perform addition, subtraction etc and display the result).
Upload in Schoology.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy