Operators. Java operators are classified into three categories:

Similar documents
Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

JAVA OPERATORS GENERAL

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

Operators in C. Staff Incharge: S.Sasirekha

GO - OPERATORS. This tutorial will explain the arithmetic, relational, logical, bitwise, assignment and other operators one by one.

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and

The Arithmetic Operators

Chapter 3: Operators, Expressions and Type Conversion

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

UNIT- 3 Introduction to C++

Department of Computer Science

A complex expression to evaluate we need to reduce it to a series of simple expressions. E.g * 7 =>2+ 35 => 37. E.g.

SECTION II: LANGUAGE BASICS

Operators in java Operator operands.

Unit-2 (Operators) ANAND KR.SRIVASTAVA

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following g roups:

Expressions and Precedence. Last updated 12/10/18

CT 229. Java Syntax 26/09/2006 CT229

A flow chart is a graphical or symbolic representation of a process.

Information Science 1

Sir Muhammad Naveed. Arslan Ahmed Shaad ( ) Muhammad Bilal ( )

CSC 1214: Object-Oriented Programming

Le L c e t c ur u e e 2 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Variables Operators

Operators and Expressions

More Programming Constructs -- Introduction

Will introduce various operators supported by C language Identify supported operations Present some of terms characterizing operators

Data Types and Variables in C language

Operators & Expressions

Outline. Performing Computations. Outline (cont) Expressions in C. Some Expression Formats. Types for Operands

Unit 3. Operators. School of Science and Technology INTRODUCTION

Operators and Expressions in C & C++ Mahesh Jangid Assistant Professor Manipal University, Jaipur

Computers Programming Course 6. Iulian Năstac

CT 229 Java Syntax Continued

Fundamentals of Programming CS-110. Lecture 3

Informatics Ingeniería en Electrónica y Automática Industrial

bitwise inclusive OR Logical logical AND && logical OR Ternary ternary? : Assignment assignment = += -= *= /= %= &= ^= = <<= >>= >>>=

RUBY OPERATORS. Ruby Arithmetic Operators: Ruby Comparison Operators:

Prof. Navrati Saxena TA: Rochak Sachan

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

CS313D: ADVANCED PROGRAMMING LANGUAGE

Expression and Operator

LECTURE 3 C++ Basics Part 2

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

Programming in C++ 5. Integral data types

COMP6700/2140 Operators, Expressions, Statements

Operators. Lecture 3 COP 3014 Spring January 16, 2018

Java Primer 1: Types, Classes and Operators

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

Chapter 7. Expressions and Assignment Statements ISBN

Basics of Java Programming

Java enum, casts, and others (Select portions of Chapters 4 & 5)

Operators And Expressions

Operators and Type Conversion. By Avani M. Sakhapara Assistant Professor, IT Dept, KJSCE

C++ Programming: From Problem Analysis to Program Design, Third Edition

Fundamental of Programming (C)

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Expressions & Assignment Statements

JAVA Programming Fundamentals

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

Index COPYRIGHTED MATERIAL

COMP Primitive and Class Types. Yi Hong May 14, 2015

Full file at

Prepared by: Shraddha Modi

Java Notes. 10th ICSE. Saravanan Ganesh

Chapter 2: Using Data

DEPARTMENT OF MATHS, MJ COLLEGE

C++ Basic Elements of COMPUTER PROGRAMMING. Special symbols include: Word symbols. Objectives. Programming. Symbols. Symbols.

Lecture 3 Tao Wang 1

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

Chapter 7. Expressions and Assignment Statements

Chapter 7. Expressions and Assignment Statements (updated edition 11) ISBN

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

UNIT 3 OPERATORS. [Marks- 12]

Expressions and Statementst t. Assignment Operator. C Programming Lecture 6 : Operators. Expression

Review of the C Programming Language for Principles of Operating Systems

A variable is a name that represents a value. For

Chapter 4: Basic C Operators

Slide 1 CS 170 Java Programming 1 Expressions Duration: 00:00:41 Advance mode: Auto

Operators and Expressions:

Visual C# Instructor s Manual Table of Contents

Add Subtract Multiply Divide

Constants and Variables

JAC444 - Lecture 1. Introduction to Java Programming Language Segment 4. Jordan Anastasiade Java Programming Language Course

3. EXPRESSIONS. It is a sequence of operands and operators that reduce to a single value.

Engineering Computing I

Chapter 3 Structure of a C Program

ESCI 386 IDL Programming for Advanced Earth Science Applications Lesson 1 IDL Operators

Chapter 12 Variables and Operators

Chapter 2: Using Data

Language Reference Manual simplicity

Lesson #3. Variables, Operators, and Expressions. 3. Variables, Operators and Expressions - Copyright Denis Hamelin - Ryerson University

Expressions and Assignment Statements

Week 2: Console I/O and Operators Arithmetic Operators. Integer Division. Arithmetic Operators. Gaddis: Chapter 3 (2.14,3.1-6,3.9-10,5.

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

WEEK 4 OPERATORS, EXPRESSIONS AND STATEMENTS

CP FAQS Q-1) Define flowchart and explain Various symbols of flowchart Q-2) Explain basic structure of c language Documentation section :

Fundamentals of Programming

Arithmetic Operators. Portability: Printing Numbers

Transcription:

Operators Operators are symbols that perform arithmetic and logical operations on operands and provide a meaningful result. Operands are data values (variables or constants) which are involved in operations. Operands of an operator are always evaluated from left to the right. Java operators are classified into three categories: Unary operator Binary operator Ternary operator (conditional operator) These operators operate on only one operand These operators operate on two operands These operators operate on three operands. The value returned by the expression depends upon a condition. Arithmetic operators: These are used to perform basic mathematical calculations. There are two types of arithmetic operator: Binary arithmetic operators (need two operands to perform calculation.) Addition + Subtraction - Multiplication * Division / Remainder % Adds values of its operands Subtracts values of its operands Multiplies values of its operands Divides the first operand by the second. Displays the quotient Finds the modulus of its first operand relative to the second. Displays the remained. Unary arithmetic Operators ( operators that act on one operand) Unary + Unary - This operator precedes an operand. The operands must have arithmetic type and the result is the value of the argument. This operator proceeds an operand. The operands must have arithmetic type and the result is the negation of its operand s value. ( 1 )

Increment (++) / Decrement Operators(- -) These are used to increase or decrease the value stored in a variable by 1 respectively. The operator ++ adds 1 to its operand. The operator - - Subtracts 1 from its operand. These operators can be used both as a prefix and as a suffix. Prefix Increment (++x) Postfix Increment (x++) Prefix Decrement (- -x) Postfix Decrement (x- -) When an increment operator precedes an operand, it is prefix increment. It increases the value of operand by 1 and then evaluates the expression. When an increment operator follows its operand, it is postfix increment. It evaluates the expression, and then increases the value of operand by 1 It decreases the value of operand by 1 and then evaluates the expression. It evaluates the expression, and then decreases the value of operand by 1 Relational Operator These are used to show the relationship between the operands. These operators compare the values of variables and determine the result in the Boolean expression format (true/false). These operators have lower precedence than arithmetic operators, but a higher precedence than the assignment operator. Operator Greater than > Less than < Greater than or equal to >= Less than or equal to <= Equal to = = Not equal to!= Symbol Logical Operator These are used to construct more complex decision making expressions. They provide result in the form of true or false, depending upon the evaluation of different relational expressions. ( 2 )

Java supports five binary and one unary logical operators: Operator Use Description && op1 && op2 Returns true if op1 and op2 are both true. It has lower precedence than the relational operators. op1 op2 Returns true if either op1 or op2 is true. It has lower precedence than the relational operators.!! op It works on single expression or operand. It is a unary operator. It reverse the result of the expression following it. It has higher precedence than any of the relational or arithmetic operators. Bitwise Operators Bitwise operators are special types of operators, which perform manipulations on binary data (bits). These are used for addition, multiplication, shifting and negating the binary data. These are also called binary operators as they are based on Boolean algebra. They work only with byte, short, int and long type operands. Java provides four Bitwise operators: Operators & Meaning Bitwise AND Bitwise OR ^ Bitwise XOR (exclusive OR) ~ Bitwise complement Assignment Operator ( = ) It is used to assign a value of an expression to a variable. It assigns the value on its right side to the variable written on the left of it. If we try to assign a new value to an operand, which already contains some value, then the existing value will be replaced by the new one. Syntax: variable = expression; Java Shorthand assignment Operators: Operator Expression Shorthand assignment expression + = a=a+1 a+=1 ( 3 )

-= a=a-1 a-=1 *= a=a*n a*=n /= a=a/n a/=n %= a=a%n a%=n = a=a n a =n ^= a=a^n a^=n <<= a=a<<n a<<=n >>= a=a>>n a>>=n >>>= a=a>>>n a>>>=n Ternary Operator(?:) (Conditional Operator) This operator needs three operands to perform an operation. Syntax: variable=<test expression>?<expression1>:<expression2>; Test expression1 is evaluated first. If it evaluates to true then the variable contains the value of expression1, otherwise it will contain the value of expression 2. Conditional operator has lower precedence than most of the operators. Special Operators The [ ] operator The. operator The ( ) operator The type operator The new operator Instanceof operator (Object reference) The square brackets are used to declare arrays, to create arrays and to access a particular element in an array. The dot operator accesses instant members of an object or class members of a class When declaring or calling a method, the method s arguments are listed between parenthesis( ). One can also pass an empty argument. This operator casts or converts a value in a variable to the specified type. Used to create a new object or a new array. It is used to check whether the object belongs to a particular class. It returns the value true if its first operand is an instance of its second. ( 4 )

Operator precedence Operator precedence determines the order in which expressions are evaluated. Java Operator Precedence Table Precedence Operator Type Associativity () [] Parentheses Array subscript Member selection Left to Right High precedence ++ -- Unary post-increment Unary post-decrement Right to left ++ -- + -! ~ ( type ) Unary pre-increment Unary pre-decrement Unary plus Unary minus Unary logical negation Unary bitwise complement Unary type cast Right to left * / % Multiplication Division Modulus + - Addition Subtraction << >> >>> Bitwise left shift Bitwise right shift with sign extension Bitwise right shift with zero extension < <= > >= instanceof Relational less than Relational less than or equal Relational greater than Relational greater than or equal Type comparison (objects only) ==!= Relational is equal to Relational is not equal to & Bitwise AND ^ Bitwise exclusive OR Bitwise inclusive OR ( 5 )

&& Logical AND Logical OR? : Ternary conditional Right to left = += -= *= /= %= Assignment Addition assignment Subtraction assignment Multiplication assignment Division assignment Modulus assignment Right to left Expressions An expression in Java is a valid combination of operators, constants and variables i.e. a legal combination of JAVA TOKENS. The type of operators used in an expression determine the expression type. An expression is composed of one or more operands and returns a value as a result. An expression can be mathematical, relational (or logical) or it could be a compound expression depending upon the result it produces. Arithmetic Expressions: These can either be pure integer expressions or pure real expressions. Sometimes a mixed expression can also be formed which is a mixture of real and integer expressions. An arithmetic expression may include variables, constants and arithmetic operators to provide a meaningful result. a. Pure Arithmetic Expression: An expression that has all operands of same data type. These expressions can be pure integer or pure real. Here all operands are of the same type. b. Mixed Arithmetic Expression: If an expression contains both integer and real values, then it is a mixed arithmetic expression. Here operands are of mixed or different data type. Boolean Expressions: Evaluations of expressions that result into true or false are called Boolean expressions. These expressions are valid combinations of constants, variables, relational and logical operators. ( 6 )

Statements Statements direct the computer to perform an action. They can alter the value of variables, process input, generate output or respond to user mouse or keyboard actions. Statements are the smallest executable unit and are always terminated with a semi colon. Java Statements: o Expression Statements: These are valid Java expressions which are terminated by a semi-colon. Assignment expressions e.g: a=5; Any use of ++ or - - e.g: i++, i- -; Method calls e.g: System.out.println( Statement ); Object creation expressions e.g: School grade=new(school); o Assignment Statement: It is used to assign a value to a variable on the left of equals sign(=) with the value of expression given on the right side of equals sign. Declaration Statement: It is used to declare a variable. Control Statement: It is used to control the flow of a program.(if, ifelse,for,while break etc.) o Null Statement(;) :The simplest statement in Java is the empty or null statement. It is written as a single semi-colon. It can be used to indicate an empty loop. o Compound Statement: This is a sequence of statements grouped together within a pair of curly braces{ }. o Block Statement: A block is a group of zero or more statements between the braces and can be used anywhere in the program. ( 7 )