Prof. Navrati Saxena TA: Rochak Sachan

Similar documents
JAVA OPERATORS GENERAL

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

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

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

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

Selenium Class 9 - Java Operators

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

Java Programming Language. 0 A history

Operators Questions

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

Operators. Java operators are classified into three categories:

Operators in java Operator operands.

CSC 1214: Object-Oriented Programming

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

The Arithmetic Operators

CT 229. Java Syntax 26/09/2006 CT229

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

Lecture Set 4: More About Methods and More About Operators

SECTION II: LANGUAGE BASICS

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

Operators and Expressions

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

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

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

Lecture Set 4: More About Methods and More About Operators

Unit 3. Operators. School of Science and Technology INTRODUCTION

Arithmetic Operators. Portability: Printing Numbers

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

Operators in C. Staff Incharge: S.Sasirekha

Java Basic Programming Constructs

Chapter 4: Basic C Operators

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

Operators & Expressions

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

Data Types and Variables in C language

More Programming Constructs -- Introduction

Unit-2 (Operators) ANAND KR.SRIVASTAVA

Department of Computer Science

Java Simple Data Types

CMPT 125: Lecture 3 Data and Expressions

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

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

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

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

9/10/10. Arithmetic Operators. Today. Assigning floats to ints. Arithmetic Operators & Expressions. What do you think is the output?

Chapter 3 Selection Statements

Object Oriented Programming Lecture (2.1) Supervisor Ebtsam AbdelHakam Department of Computer Science Najran University

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Outline. Parts 1 to 3 introduce and sketch out the ideas of OOP. Part 5 deals with these ideas in closer detail.

Chapter 4. Operations on Data

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

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

Object-Oriented Programming. Topic 2: Fundamental Programming Structures in Java

Expressions and Precedence. Last updated 12/10/18

Chap 6 - Introduction to HDL (b)

Information Science 1

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

ISA 563 : Fundamentals of Systems Programming

Java Simple Data Types

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

Object-Oriented Programming

Key Java Simple Data Types

Programming Basics. Digital Urban Visualization. People as Flows. ia

Constants and Variables

Lecture 3. More About C

Operators And Expressions

CMBUkTI3 sbaøannbvnþelx 3

Expressions and Assignment Statements

Computers Programming Course 6. Iulian Năstac

Operators and Expressions:

Lecture 3 Operators MIT AITI

Fundamentals of Programming

CS313D: ADVANCED PROGRAMMING LANGUAGE

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

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

Chapter 7. Expressions and Assignment Statements ISBN

Chapter 2 Primitive Data Types and Operations

Basics of Java Programming

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

CCHAPTER SELECTION STATEMENTS HAPTER 3. Objectives

Program Fundamentals

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

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

UNIT 3 OPERATORS. [Marks- 12]

CT 229 Java Syntax Continued

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

Zheng-Liang Lu Java Programming 45 / 79

LECTURE 3 C++ Basics Part 2

Motivating Examples (1.1) Selections. Motivating Examples (1.2) Learning Outcomes. EECS1022: Programming for Mobile Computing Winter 2018

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.

Operators. Lecture 3 COP 3014 Spring January 16, 2018

Chapter 7. Expressions and Assignment Statements

ECE 122 Engineering Problem Solving with Java

Expressions & Assignment Statements

Expression and Operator

Chapter 2: Using Data

Basic C Programming (2) Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Building Java Programs

Transcription:

JAVA Prof. Navrati Saxena TA: Rochak Sachan

Operators

Operator Arithmetic Relational Logical Bitwise

1. Arithmetic Operators are used in mathematical expressions. S.N. 0 Operator Result 1. + Addition 6. += 2. Subtraction 7. = 3. * Multiplication 8. *= 4. / Division 9. /= 5. % Modulus 10. %= S.N. Operator Result Addition assignment Subtraction assignment Multiplication assignment Division assignment Modulus assignment 11. Decrement

Arithmetic Operators (Cont ) The operands of the arithmetic operators must be of a numeric type. Can not be used on boolean types, but you can use them on char types, since the char type in Java is a subset of int.

Basic Arithmetic Operators - addition, subtraction, multiplication, and division. // Demonstrates the basic arithmetic operators. class BasicMath { public static void main(string args[]) { int a = 1 + 1; int b = a * 3; int c = b / 4; int d = c - a; int e = -d; System.out.println("a = " + a); System.out.println("b = " + b); System.out.println("c = " + c); System.out.println("d = " + d); System.out.println("e = " + e); } }

Modulus Operator (%) - returns the remainder of a division operation. - can be applied to floating-point types as well as integer types. (This differs from C/C++, in which the % can only be applied to integer types.)

Modulus Operator (%) // Demonstrates the % operator. class Modulus { public static void main(string args[]) { int x = 42; double y = 42.25; System.out.println("x mod 10 = " + x % 10); System.out.println("y mod 10 = " + y % 10); } }

Arithmetic Assignment Operators // Demonstrates several assignment operators. class OpEquals { public static void main(string args[]) { int a = 1; int b = 2; int c = 3; a += 5; b *= 4; c += a * b; c %= 6; System.out.println("a = " + a); System.out.println("b = " + b); System.out.println("c = " + c); } }

Increment and Decrement // Demonstrates ++. class IncDec { public static void main(string args[]) { int a = 1; int b = 2; int c; int d; c = ++b; d = a++; c++; System.out.println("a = " + a); System.out.println("b = " + b); System.out.println("c = " + c); System.out.println("d = " + d); } }

2. Bitwise Operators Java defines several bitwise operators which can be applied to the integer types, long, int, short, char, and byte.

Bitwise Operators S.N. Operator Result S.N. Operator Result 1. ~ Bitwise unary NOT 8. &= 2. & Bitwise AND 9. = 3. Bitwise OR 10. ^= 4. ^ Bitwise exclusive OR 11. >>= 5. >> Shift right 12. >>>= 6. >>> Shift right zero fill 13. <<= Bitwise AND assignment Bitwise OR assignment Bitwise exclusive OR assignment Shift right assignment Shift right zero fill assignment Shift left assignment 7. << Shift left

Bitwise Logical Operators (&,, ^, and ~.) - The following table shows the outcome of each operation. A B A B A & B A ^ B ~A 0 0 0 0 0 1 1 0 1 0 1 0 0 1 1 0 1 1 1 1 1 1 0 0

The Left Shift (<<) shifts all of the bits in a value to the left a specified number of times. It has the general form: value << num num specifies the number of positions to left-shift the value in value. For each shift left, the high-order bit is shifted out (and lost), and a zero is brought in on the right. Each left shift has the effect of doubling the original value.

The Right Shift (>>) shifts all of the bits in a value to the right a specified number of times. It has the general form: value >> num num specifies the number of positions to right-shift the value in value. When shifting right, the top (leftmost) bits exposed by the right shift are filled in with the previous contents of the top bit. Each time you shift a value to the right, it divides that value by two and discards any remainder.

3. Relational Operators - The relational operators determine the relationship that one operand has to the other. S.N. Operator Result S.N. Operator Result 1. == Equal to 4. < Less than 2.!= Not equal to 5. >= 3. > Greater than 6. <= Greater than or equal to Less than or equal to

3. Relational Operators The outcome of these operations is a boolean value. Most frequently used in the expressions that control the if statement and the various loop statements. Example: int a = 4; int b = 1; boolean c = a < b; In this case, the result of a<b (which is false) is stored in c.

4. Boolean Logical Operators operate only on boolean operands. All of the binary logical operators combine two boolean values to form a resultant boolean value.

4. Boolean Logical Operators S.N. Operator Result S.N. Operator 1. & Logical AND 7. &= 2. Logical OR 8. = 3. ^ Logical XOR (exclusive OR) 9. ^= Result AND assignment OR assignment XOR assignment 4. Short-circuit OR 10. == Equal to 5. && Short-circuit AND 11.!= Not equal to 6.! Logical unary NOT 12.?: Ternary if-thenelse

4. Boolean Logical Operators The following table shows the effect of each logical operation: A B A B A & B A ^ B!A False False False False False True True False True False True False False True True False True True True True True True False False

Short-Circuit Logical Operators (, &&) If you use the and && forms, rather than the and & forms of these operators, Java will not bother to evaluate the righthand operand when the outcome of the expression can be determined by the left operand alone. Example: if (denom!= 0 && num / denom > 10) *there is no risk of causing a run-time exception when denom is zero.

The? Operator (three-way) operator that can replace certain types of if-then-else statements. The? has this general form: expression1? expression2 : expression3 - If expression1 is true, then expression2 is evaluated; otherwise, expression3 is evaluated. - Both expression2 and expression3 are required to. return the same type, which can t be void.

Operator Precedence This part of the presentation shows the order of precedence for Java operators, from highest to lowest. Parentheses are used to alter the precedence of an operation. Parentheses raise the precedence of the operations that are inside them.

Operator Precedence Highest Lowest ( ) [ ]. ++ ~! * / % + >> >>> << > >= < <= ==!= & ^ &&?: = op= Table. The Precedence of the Java Operators

References Herbert Schildt; "Java The Complete Reference, Seventh Edition (Osborne Complete Reference Series)", McGraw- Hill, 2007 http://www.java2s.com/tutorial/java/catal ogjava.htm http://www.freejavaguide.com/corejava.ht m

Thank You