[go: up one dir, main page]

0% found this document useful (0 votes)
311 views7 pages

Operators and Expressions

The document defines various types of operators in Java including unary, binary, and ternary operators. It describes arithmetic operators like addition and subtraction, assignment operators, relational operators that compare values, and logical operators that return true or false. The precedence of operators is also discussed, with parentheses and postfix operators having higher precedence than arithmetic, relational, and logical operators. Expressions in Java combine operators and operands to perform calculations or return boolean values.

Uploaded by

VinayKumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
311 views7 pages

Operators and Expressions

The document defines various types of operators in Java including unary, binary, and ternary operators. It describes arithmetic operators like addition and subtraction, assignment operators, relational operators that compare values, and logical operators that return true or false. The precedence of operators is also discussed, with parentheses and postfix operators having higher precedence than arithmetic, relational, and logical operators. Expressions in Java combine operators and operands to perform calculations or return boolean values.

Uploaded by

VinayKumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

OPERATOR: An operator is a symbol which is used to perform some arithmetic or logical operations on

operands.
For example, in a +b ,+ is an operator.

OPERAND: Operands are the values (constants or variables) on which operator operates.
For example, in a +b, a and b are operands.

Java operators can be classified as:


· UNARY OPERATOR: Operators that works on one operand( E.g., +,-,++,--)
· BINARY OPERATOR: Operators that works on two operands(E.g., +,-,*,/,%,>,< etc)
· TERNARY OPERATOR: Operators that works on three operands (E.g., ?:(conditional operator)

TYPES OF OPERATORS:

1. ARITHMETIC OPERATORS: Arithmetic operators are those operators which are used to perform
arithmetic operations like addition, subtraction, multiplication, division etc.
Types of Arithmetic operators:
· Unary arithmetic operators
· Binary Arithmetic operators

· Unary Arithmetic operators: These operator works on one operand. They are:
o Unary plus (+): This operator is applied before the operand, which indicates that the
operand is positive. For example +3
o Unary minus (-):This operator is applied before the operand, which indicates that the
opera
o Increment operator(++):This operator increases the value of operand by one.
For example x++ or ++x means x=x+1
o Decrement operator(--):This operator decreases the value of operand by one.
For example x-- or --x means x=x-1

The increment and decrement operators can be further classified as:


· Prefix increment: When increment operator is applied before the operand then it is called
as Prefix increment. For example ++a
This operator works on the principle “CHANGE THEN USE”. This means that the value of
the variable changes before it is used in expression.
For example,
int a=3,b;
b=++a;
The values of a and b will be a=4 and b=4

Page 1
· Postfix increment: When increment operator is applied after the operand then it is called
as Postfix increment. For example a++
This operator works on the principle “USE THEN CHANGE”. This means that the value of
the variable is used in expression and then changes .

For example,int a=3,b; b=a++;


The values of a and b will be a=4 and b=3

· Prefix decrement: When decrement operator is applied before the operand then it is called
as Prefix decrement. For example --a
This operator works on the principle “CHANGE THEN USE”. This means that the value of
the variable changes before it is used in expression.
For example,
int a=3,b;
b=--a;
The values of a and b will be a=2 and b=2

· Postfix decrement: When decrement operator is applied after the operand then it is called
as Postfix increment. For example a--
This operator works on the principle “USE THEN CHANGE”. This means that the value of
the variable is used in expression and then changes .
int a=3,b;
b=a--;
The values of a and b will be a=2 and b=3

· Binary Arithmetic operators: These operator works on two operands. They are:
o Addition(+) : This operator add two operands and returns the sum.
o Subtraction(-) : This operator subtract one operand from other operand and returns
difference.
o Multiplication(*) : This operator is used to multiply two operands and returns product.
o Division(/) : This operator is used to divide two operands and returns Quotient.
o Modulus or Remainder(%) : This operator is used to divide two operands and returns
Remainder.

2. ASSIGNMENT OPERATOR: It is used to assign a value(variable/constant) to a variable. It assigns


the value on its right side to the left of it.
For example a=3;
b=a;

Page 2
Java provides special shorthand assignment operators.
+= a+=2 means a=a+2-= a-=4 means a=a-4
*- a*=5 means a=a*5
/= a/=4 means a=a/4
%= a%=5 means a=a%5

3. RELATIONAL OPERATORS: They are used to show relationship among the operands. They
compare variables and constants and returns true or false. The six relational operators are:
> greater than
< less than
>= greater than or equal to
<= less than or equal to
== equal to
!= not equal to
4. LOGICAL OPERATORS: They are used to perform logical operations. They result in true or false.
There are three logical operators in Java:
&& logical AND
|| logical OR
! logical NOT

· Logical AND: This operator works on two operands(result of relational expressions) and
returns true if both the expressions are evaluated to true.
e.g., 5>4 && 7<8 results true as both expressions results in true.

· Logical OR:This operator works on two operands (result of relational expressions) and
returns true if any of the expressions is evaluated to true.
e.g., 5>7 || 5<8 results true as result of second expression is true.
· Logical NOT: It is unary operator which reverts the outcome of expression.
e.g., !(8>3) is false as 8>3 is true.

TERNARY OPERATOR(CONDITIONAL OPERATOR)


It deals with three operands. Its syntax is:
SYNTAX:
variable=(test expression)?expression1:expression2
Here test expression in evaluated. If it is true than the variable contains value of expression1
otherwise variable contains value of expression2. For example,
int a=4,b=3,lar;
lar=(a>b)?a:b;
Here lar contains value of a i.e., 4
Page 3
new operator:new operator is used allocate dynamic space in memory. It is used for object creation.
dot operator:
This operator is used to refer data members or member functions of a class.

HIERARCHY OF OPERATORS(OPERATOR PRECEDENCE)


It decides the order in which different operators in an expression are evaluated. The hierarchical
order in which the operators are used for operation in expression is known as operator precedence.

HIGHER PRECEDENCE

· BRACKET()
· POST INCREMENT/DECREMENT
· PRE INCREMENT/DECREMENT
· UNARY PLUS/MINUS
· ARITHMETIC OPERATORS
o EXPONENT
o MULTIPLICATION, DIVISION(*,/,%)
o ADDITION,SUBTRACTION(+,-)
· RELATIONAL OPERATORS
· LOGICAL OPERATORS
o AND(&&)
o OR(| |)
· TERNARY
· ASSIGNMENT
LOWER PRECEDENCE

EXPRESSION:
An expression is a valid combination of operator and operands. The operands can be variables and
constants. For example c=a+b;
Types of expression:
· ARITHMETICAL EXPRESSION
An expression that contains variables , constants and arithmetical operators to perform any
calculation is called as arithmetical expression.
For example: a+b
x*y+z/8
b*b-4*a*c

Page 4
It is of two types:
o PURE ARITHMETIC EXPRESSION
An expression that contains all the operands of same data type is called as pure
expression. They are further of two types:
▪ Integer arithmetic expression: are those which contain only integer
operands. For example:
inta,b,c;
c=a+b;
▪ Real arithmetic expression: are those which contain only real(float-
ing) operands.For example:
double a,b,c;
c=a+b;
o IMPURE(MIXED) ARITHMETIC EXPRESSION
An expression which contains different types of operands is called as Impure
expression. For example,
int a;
float b;
double c=a+b;
· BOOLEAN EXPRESSION
Expressions that result into true or false are called a s Boolean Expressions.

TYPE CONVERSION:
Java performs any operation on data types which are compatible i.e., of same data type. When in
an expression (mixed expression) the operands are of different data types i.e., not compatible then one
data type will be converted into another before performing the operation. This is known as type conver-
sion. The type conversion can be done in two ways:
· Implicit type conversion
· Explicit type conversion

· Implicit type conversion(Coercion):


This is an automatic type conversion in which lower data type is converted (promoted) into higher
data type without any user’s intervention. This is also known as coercion or type promotion.

Hierarchy of data types:


byte
char, short
int
long
float
double Page 5
For example:
int a;
float b,c;
c=a+b;
Here “a” is int and “b” is float so “a” will be promoted to float before addition and the result will
be of float type.

· Explicit type conversion(Type casting):


In this type of conversion a higher data type is converted forcibly into lower data type
according to user’s choice. Here the data type gets converted into another data type by
user’s intervention.
For example:
double d=5.6;
int a=(int)d;
Here “d” is converted into int.

TESTING AND DEBUGGING:


Testing is a process of making sure that the programs performs the intended task. It is a process in
which a program is validated.
Debugging is the process of locating and eliminating any errors in program.

TYPES OF ERRORS IN A JAVA PROGRAM:


Errors (also called as bugs) are mistakes that affect the output of program. Generally there are
three types of errors that come in java program:
1. SYNTAX ERROR:
These errors occurs when the rules or grammar of the programming language are not fol-
lowed. These errors are caused due to mistakes that we make while typing code which
may include incorrect punctuation, undefined terms, wrong keywords, writing incomplete
statements etc. Some common syntax errors are:
▪ Missing semicolon
▪ Wrong punctuation
▪ Misspelt words
▪ Missing braces
▪ Use of variables which are not declared
2. LOGICAL ERROR:
These errors occur in planning the program’s logic. The computer actually doesn’t know
that an error has been made. The program with logical errors may get compiled and exe-
cuted but such programs may not give the desired output. Some common logical errors are:
▪ use of incorrect formula
▪ use of wrong algorithm
Page 6
3. RUNTIME ERROR
These errors occur during the execution of the program due to some illegal operation per-
formed in the program. Some illegal operations that may cause run time errors are:
▪ Dividing number by zero
▪ Square root of -ve values
▪ “A23” cannot be converted to an integer.

Difference between print() and println() methods:


print() method will print any message on the screen. The next message will be printed on same line.
For example:
System.out.print(“GOOD”); Sys-
tem.out.print(“MORNING”);
OUTPUT: GOODMORINING

println() will print the message on the screen and then changes the line causing the next output to be
printed on different line.
For example:
System.out.println(“GOOD”);
System.out.println(“MORNING”);
OUTPUT: GOOD
MORNING

Page 12
10
11
Page 7
8
9

You might also like