Operators in java Operator operands.

Similar documents
JAVA OPERATORS GENERAL

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

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 groups:

Operators. Java operators are classified into three categories:

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

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

Chapter 3: Operators, Expressions and Type Conversion

Operators in C. Staff Incharge: S.Sasirekha

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

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

The Arithmetic Operators

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

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

Data Types and Variables in C language

SECTION II: LANGUAGE BASICS

More Programming Constructs -- Introduction

Department of Computer Science

Operators and Expressions:

Prof. Navrati Saxena TA: Rochak Sachan

Constants and Variables

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

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

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

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

Operators & Expressions

Fundamentals of Programming CS-110. Lecture 3

UNIT 3 OPERATORS. [Marks- 12]

Java Basic Programming Constructs

Expression and Operator

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

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

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

UNIT- 3 Introduction to C++

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

CS313D: ADVANCED PROGRAMMING LANGUAGE

Expressions and Precedence. Last updated 12/10/18

Engineering Computing I

Arithmetic Operators. Portability: Printing Numbers

JAVA Programming Fundamentals

CSC 1214: Object-Oriented Programming

Programming with Java

Quick Reference Guide

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Prepared by: Shraddha Modi

CONTENTS: Compilation Data and Expressions COMP 202. More on Chapter 2

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

Information Science 1

Operators and Expressions

Infix to Postfix Conversion

CT 229. Java Syntax 26/09/2006 CT229

Declaration and Memory

Java Notes. 10th ICSE. Saravanan Ganesh

Selenium Class 9 - Java Operators

4 Programming Fundamentals. Introduction to Programming 1 1

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

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

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

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

ECE 122 Engineering Problem Solving with Java

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

Guide for The C Programming Language Chapter 1. Q1. Explain the structure of a C program Answer: Structure of the C program is shown below:

Operators And Expressions

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Unit 3. Operators. School of Science and Technology INTRODUCTION

A First Program - Greeting.cpp

Chapter 6 Primitive types

Introduction to Programming (Java) 2/12

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

AYBUKE BUYUKCAYLI KORAY OZUYAR MUSTAFA SOYLU. Week 21/02/ /02/2007 Lecture Notes: ASCII

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.

Programming in C++ 5. Integral data types

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

COMP6700/2140 Operators, Expressions, Statements

Lecture Set 4: More About Methods and More About Operators

Computers Programming Course 6. Iulian Năstac

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

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

SOFTWARE DEVELOPMENT 1. Operators 2018W A. Ferscha (Institute of Pervasive Computing, JKU Linz)

Objects and Types. COMS W1007 Introduction to Computer Science. Christopher Conway 29 May 2003

A Java program contains at least one class definition.

Data Conversion & Scanner Class

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

Basics of Programming

3. Java - Language Constructs I

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

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

More Things We Can Do With It! Overview. Circle Calculations. πr 2. π = More operators and expression types More statements

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

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

C expressions. (Reek, Ch. 5) 1 CS 3090: Safety Critical Programming in C

Lecture Set 4: More About Methods and More About Operators

Review of the C Programming Language for Principles of Operating Systems

Chapter 7. Expressions and Assignment Statements ISBN

Logical and Bitwise Expressions

Lecture 3 Operators MIT AITI

Chapter 7. Additional Control Structures

Lexical Considerations

Chapter 2 Primitive Data Types and Operations

Writing Program in C Expressions and Control Structures (Selection Statements and Loops)

Transcription:

Operators in java Operator in java is a symbol that is used to perform operations and the objects of operation are referred as operands. There are many types of operators in java such as unary operator, arithmetic operator, relational operator, shift operator, bitwise operator, ternary operator and assignment operator.

Operators [] () L to R Precedence postfix expr++ expr-- (R to L) unary ++expr --expr +expr -expr ~! (R to L) multiplicative * / % additive + - shift << >> relational Equality, NON-EQUALITY ==!= bitwise AND bitwise exclusive OR bitwise inclusive OR logical AND < > <= >= instance of & ^ && logical OR ternary? : assignment = += -= *= /= %= &= ^= = <<= >>= >>>=

Interger Arithmatic and Real Arithmatic class IntA { public static void main (String args [ ] ) { int a = 14, b= 4; System.out.println ("a = " + a) ; System.out.println (" b = " + b) ; System.out.println ("a+b = " + (a+b)) ; System.out.println (" a-b = " + (a-b)) ; System.out.println (" a*b = " + (a*b)) ; System.out.println (" a/b = " + (a/b) ); System.out.println (" a%b = " + (a%b)) ; } }

Interger Arithmatic and Real Arithmatic class reala { public static void main (String args [ ] ) { float a = 20.5F, b= 6.4F; System.out.println ("a = " + a) ; System.out.println (" b = " + b) ; System.out.println ("a+b = " + (a+b)) ; System.out.println (" a-b = " + (a-b)) ; System.out.println (" a*b = " + (a*b)) ; System.out.println (" a/b = " + (a/b) ); System.out.println (" a%b = " + (a%b)) ; } }

Relational Operators Meaning < Is less than <= Is less than or equal to > Greater than >= Greater than or equal to = = Is equal to (equality op)! Is Not equal to

INCREMENT / DECREMENT OPERATORS(++, - -) These operators usually not found in other languages. C++ name itself is influenced by the increment operator ++. The operator ++ adds 1 to its operand, and - - subtracts one.

a = a+1; is same as ++a ; or a++; And a=a-1; is same as - - a; or a - - ;

Both the increment and decrement operators come in two forms: 1. Prefix ( e.g. ++a and - - a) precede the operand 2. Postfix ( e.g. a++ and a- -) comes after the operand The two versions have the same effect upon the operand, but they differ when they take place in expression.

Working with prefix version When an increment or decrement operator precedes its operand, C++ performs the increment or decrement before using the value of the operand. For example, The expression sum = sum +(++count); Taking initial values of sum and count to be 0 and 10 respectively. sum= 0+(++count) sum = 0 + 11; sum=11;

Similarly the expression p = p*- - n; Where initial value of p=4 and n=8; Evaluates as: p=4*7; p=28; p=4*(- - n); The prefix increment or decrement operators follow change- then-use rule. They first change the value of the operand, then use the new value in evaluating the expression.

Working with postfix version When an increment or decrement operator follows its operand, C++ performs the increment or decrement after using the value of the operand. For example, The expression sum = sum +(count++); Taking initial values of sum and count to be 0 and 10 respectively. sum= 0+(count++) sum = 0 + 10; sum=10; // then count becomes 11

Similarly the expression p = p*n- -; Where initial value of p=4 and n=8; Evaluates as: p=4*8; p=4*(n- -); p=32; // now n becomes 7 The postfix increment or decrement operators follow use- then-change rule. They first use the value of the operand in evaluating the expression and then change the operand s value.

int Number=1000; Number++; //Post increment in an independent statement OR int Number=1000; ++Number; //Pre increment in an independent statement //will have the same meaning int Total=50,Number=10; Total=Total + Number++; //post increment - /* first the Total is increased by the current value of Number and then Number is incremented by 1 So, after execution of the expression Total will be 60 and Number will be 11 */

int Total=50,Number=10; Total=Total + ++Number; //pre-increment - //first the Number gets incremented //by 1 and then gets added to Total //So, after execution of the expression //Total will be 61 and Number will be 11

Conditional Operator?:

Conditional Operator?: (Ternary Operator) If the Exp1 evaluates to be true then Exp2 is evaluated and exp3 is ignored,otherwise Exp3 is evaluated and Exp2 is ignored. Exp1? Exp2: exp3 For example: Max= (a>b)? a: b;

Logical Operators (!, &&, ) Logical operators refers to the ways in which the relationships (among values) can be connected together. These are: 1. The logical AND (&&) operator 2. The logical OR ( ) operator 3. The logical NOT (!) operator

The logical AND (&&) operator The logical AND operators, written as &&, combines two expressions into one. The resulting expression has the value 1(true) only if both of the original expressions (its operands) are true. For example: (6==3) && (4==4) // 0(false) because exp1 is false (4==4) && (8==8) // 1(true) because both are true 6<9 && 4<2 // 0(false) exp2 is false 6>9 && 4>6 // 0(false) both false

The logical OR ( ) operator The logical OR operators, written as, combines two expressions into one. The resulting expression has the value 1(true) only if one or both of the original expressions (its operands) are true. For example: (6==3) (4==4) // 1(true) because exp2 is true (4==4) (8==8) // 1(true) because both are true 6<9 4<2 // 1(true) because exp1 is true 6>9 4>6 // 0(false) both false

The logical NOT (!) operator Works on single expression or operand ie. It is unary operator. It negates the the truth value of expression following it i.e., if the expression is true, then!expression is false, and vice versa.!(5) // 0(false) because 5 is non-zero(ie. True)!(5>2) // results in 0 because exp 5>2 is true i.e. 1

Shorthand Assignment operator: Operator Use Result += A+=B A= A+B *= A*=B A=A*B -= A-=B A=A-B /= A/=B A=A/B

Character Manipulation 1. When the operands are of Type String, + operator performs the concatenation. 2. In case of char literals, ASCII code is added for respective operands. 3. In case of numeric value we need to enclose them in () to make it expression. For Example: System.out.print( H + I ); System.out.print( A + a ); System.out.print( 5+7= +2+3); System.out.print( 5+7= +(2+3));

Evaluation of arithmetic operators I x= a-b/3 + c*2-1; When a= 9,b=12 and c=3 II x= a-b/ (3+c)*(2-1);

Evaluation of arithmetic operators I x= a-b/3 + c*2-1; When a= 9,b=12 and c=3 II x= a-b/ (3+c)*(2-1);

Bitwise operators Java defines several bitwise operators that can be applied to the integer types long, int, short, char and byte. operator description & Bitwise AND Bitwise OR ^ Bitwise exclusive OR << left shift >> right shift

truth table for bitwise &, and ^ a b a & b a b a ^ b ( A.B + A.B) 0 0 0 0 0 0 1 0 1 1 1 0 0 1 1 1 1 1 1 0 The bitwise shift operators shifts the bit value. The left operand specifies the value to be shifted and the right operand specifies the number of positions that the bits in the value are to be shifted. Both operands have the same priority. Example a = 0001000 b= 2 a << b= 0100000 a >> b= 0000010