Operators in JAVA. Operator An operator is a symbol that operates on one or more arguments to produce a result. Java provides a rich set of operators.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Department of Computer Science. Definition “An operator is a symbol (+,-,*,/) that directs the computer to perform certain mathematical or logical manipulations.
Types, Variables and Operators Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2013.
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
1 Fundamental Data types Overview l Primitive Data Types l Variable declaration l Arithmetical Operations l Expressions l Assignment statement l Increment.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
All the Operators. Precedence An operator with higher precedence is done earlier (prededes) one with lower precedence –A higher precedence is indicated.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
All the Operators. Precedence An operator with higher precedence is done earlier (prededes) one with lower precedence –A higher precedence is indicated.
1 Data types, operations, and expressions Overview l Primitive Data Types l Variable declaration l Arithmetical Operations l Expressions.
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
Program Elements We can now examine the core elements of programming (as implemented in Java) We focuse on: data types variable declaration and use, constants.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
More about Numerical Computation CS-2301, B-Term More about Numerical Computation CS-2301, System Programming for Non-Majors (Slides include materials.
JavaScript, Third Edition
2015/8/221 Data Types & Operators Lecture from (Chapter 3,4)
Chapter 2: Introducing Data Types and Operators.  Know Java’s primitive types  Use literals  Initialize variables  Know the scope rules of variables.
Chapter 4: Operators Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills /1436.
© 2001 by Ashby M. Woolf Revision 3 Using Java Operators The Basic Toolkit Pliers, Screwdriver, Hammer and Drill.
CHAPTER:8 OPERATORS AND EXPRESSION IN C++ Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
General Features of Java Programming Language Variables and Data Types Operators Expressions Control Flow Statements.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Chapter 3: Data Types and Operators JavaScript - Introductory.
Operators Using Java operators An operator takes one or more arguments and produces a new value. All operators produce a value from their.
Simple Data Types and Statements. Namespaces namespace MyNamespace { // …. { MyNamespace::func1() using namespace OtherNamespace; Comments: // /* xxxx.
JAVA Tokens. Introduction A token is an individual element in a program. More than one token can appear in a single line separated by white spaces.
Java Software Solutions Lewis and Loftus Chapter 5 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. More Programming Constructs.
Basic Operators. What is an operator? using expression is equal to 9. Here, 4 and 5 are called operands and + is the operator Python language supports.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Universidad Nacional de Colombia Facultad de Ingeniería Departamento de Sistemas ertificación en AVA.
C Operators. CONTENTS C OPERATORS TYPES OF OPERATOR UNARY BINARY TERNARY ARITHMATIC RELATIONAL LOGICAL.
Java operatoriai sandbolts/operators.html
Controlling Program Flow. Data Types and Variable Declarations Controlling Program Flow.
15-Nov-15 All the Operators. operators.ppt 2 Precedence An operator with higher precedence is done earlier (precedes) one with lower precedence A higher.
Java Language Basics By Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.
COMP Primitive and Class Types Yi Hong May 14, 2015.
Sahar Mosleh California State University San MarcosPage 1 Data Types and Operators.
1 Lecture 5 More Programming Constructs Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.
Software Technology - I Tools to do operations.....
Operators & Expressions
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
Boolean expressions, part 1: Compare operators. Compare operators Compare operators compare 2 numerical values and return a Boolean (logical) value A.
0 Chap.2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations 2.5Arithmetic Operators 2.6Relational.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material developed.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
Programming Principles Operators and Expressions.
Chad’s C++ Tutorial Demo Outline. 1. What is C++? C++ is an object-oriented programming (OOP) language that is viewed by many as the best language for.
 Array ◦ Single & Multi-dimensional  Java Operators ◦ Assignment ◦ Arithmetic ◦ Relational ◦ Logical ◦ Bitwise & other.
August 6, Operators. August 6, Arithmetic Operators.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
CompSci 230 S Programming Techniques
Lecture 3 Java Operators.
University of Central Florida COP 3330 Object Oriented Programming
Programming in Java Sachin Malhotra, Chairperson, PGDM-IT, IMS Ghaziabad Saurabh Chaudhary, Dean, Academics, IMS Ghaziabad.
2.0 FUNDAMENTALS OF JAVA PROGRAMMING LANGUAGE
Lecture 2: Data Types, Variables, Operators, and Expressions
University of Central Florida COP 3330 Object Oriented Programming
Multiple variables can be created in one declaration
Primitive Data, Variables, Loops (Maybe)
Java operatoriai
Operators and Expressions
Escape Sequences What if we wanted to print the quote character?
Starting JavaProgramming
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
C Operators, Operands, Expressions & Statements
Chapter-3 Operators.
OPERATORS in C Programming
OPERATORS in C Programming
Presentation transcript:

Operators in JAVA

Operator An operator is a symbol that operates on one or more arguments to produce a result. Java provides a rich set of operators to manipulate variables.

Operands An operands are the values on which the operators act upon. An operand can be: o A numeric variable - integer, floating point or character o Any primitive type variable - numeric and boolean o Reference variable to an object o A literal - numeric value, boolean value, or string. o An array element, "a[2]“ o char primitive, which in numeric operations is treated as an unsigned two byte integer

Types of Operators Assignment Operators Increment Decrement Operators Arithmetic Operators Bitwise Operators Relational Operators Logical Operators Ternary Operators Comma Operators Instanceof Operators

Assignment Operators The assignment statements has the following syntax: =

Assigning values Example

Increment and Decrement operators ++ and -- The increment and decrement operators add an integer variable by one. increment operator: two successive plus signs, ++ decrement operator: --

Increment and Decrement operators ++ and -- Common Shorthand a = a + 1;a++; or ++a; a = a - 1;a--; or --a;

int j, p, q, r, s; j = 5; p = ++j; // j = j + 1; p = j; System.out.println("p = " + p); q = j++; // q = j; j = j + 1; System.out.println("q = " + q); System.out.println("j = " + j); r = --j; // j = j -1; r = j; System.out.println("r = " + r); s = j--; // s = j; j = j - 1; System.out.println("s = " + s); Example of ++ and -- operators > java example p = 6 q = 6 j = 7 r = 6 s = 6 > > java example p = 6 q = 6 j = 7 r = 6 s = 6 > public class Example { public static void main(String[] args) { }

Arithmetic Operators The arithmetic operators are used to construct mathematical expressions as in algebra. Their operands are of numeric type.

Arithmetic Operators OperatorResult +Addition -Subtraction (also unary minus) *Multiplication /Division %Modulus ++Increment +=Addition assignment -=Subtraction assignment *=Multiplication assignment /=Division assignment %=Modulus assignment --Decrement

Simple Arithmetic public class Example { public static void main(String[] args) { int j, k, p, q, r, s, t; j = 5; k = 2; p = j + k; q = j - k; r = j * k; s = j / k; t = j % k; System.out.println("p = " + p); System.out.println("q = " + q); System.out.println("r = " + r); System.out.println("s = " + s); System.out.println("t = " + t); } > java Example p = 7 q = 3 r = 10 s = 2 t = 1 > > java Example p = 7 q = 3 r = 10 s = 2 t = 1 >

Bitwise Operators Java's bitwise operators operate on individual bits of integer (int and long) values. If an operand is shorter than an int, it is promoted to int before doing the operations.

Bitwise Operators OperatorNameDescription a & band 1 if both bits are 1. a | bor 1 if either bit is 1. a ^ bxor 1 if both bits are different. ~anot Inverts the bits. n << pleft shift Shifts the bits of n left p positions. Zero bits are shifted into the low-order positions. n >> pright shift Shifts the bits of n right p positions. If n is a 2's complement signed number, the sign bit is shifted into the high-order positions. n >>> pright shift Shifts the bits of n right p positions. Zeros are shifted into the high-order positions.

Example of Bitwise Operators class Test { public static void main(String args[]) { int a = 60; /* 60 = */ int b = 13; /* 13 = */ int c = 0; c = a & b; /* 12 = */ System.out.println("a & b = " + c ); c = a | b; /* 61 = */ System.out.println("a | b = " + c );

Example Cont., c = a ^ b; /* 49 = */ System.out.println("a ^ b = " + c ); c = ~a; /*-61 = */ System.out.println("~a = " + c ); c = a << 2; /* 240 = */ System.out.println("a << 2 = " + c ); c = a >> 2; /* 215 = 1111 */ System.out.println("a >> 2 = " + c ); c = a >>> 2; /* 215 = */ System.out.println("a >>> 2 = " + c ); } Output: a & b = 12 a | b = 61 a ^ b = 49 ~a = -61 a << 2 = 240 a >> 15 a >>> 15

Relational Operators A relational operator compares two values and determines the relationship between them. For example, != returns true if its two operands are unequal. Relational operators are used to test whether two values are equal, whether one value is greater than another, and so forth.

Relational Operators OperatorDescription ==Checks if the value of two operands are equal or not, if yes then condition becomes true. !=Checks if the value of two operands are equal or not, if values are not equal then condition becomes true. >Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true.

Relational Operators OperatorDescription <Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true. >=Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true. <=Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true.

Example of Relational Operators public LessThanExample { public static void main(String args[]) { int a = 5; int b = 10; if(a < b) { System.out.println("a is less than b"); }

Logical Operators These logical operators work only on boolean operands. Their return values are always boolean.

Logical Operators OperatorDescription &&Called Logical AND operator. If both the operands are non zero then then condition becomes true. ||Called Logical OR Operator. If any of the two operands are non zero then then condition becomes true. !Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false.

Example of Logical Operators public class ANDOperatorExample{ public static void main(String[] args){ char ans = 'y'; int count = 1; if(ans == 'y' & count == 0){ System.out.println("Count is Zero.");} if(ans == 'y' & count == 1) { System.out.println("Count is One."); } if(ans == 'y' & count == 2) { System.out.println("Count is Two."); } } }

Ternary Operators Java has a short hand way by using ?: the ternary aka conditional operator for doing ifs that compute a value. Unlike the if statement, the conditional operator is an expression which can be used for

Example of Ternary Operator // longhand with if: int answer; if ( a > b ) { answer = 1; } else { answer = -1; } // can be written more tersely with the ternary operator as: int answer = a > b ? 1 : -1;

Comma Operators Java has an often look past feature within it’s for loop and this is the comma operator. Usually when people think about commas in the java language they think of a way to split up arguments within a functions parameters

Example of Comma Operator //: c03:CommaOperator.java // From 'Thinking in Java, 3rd ed.' (c) Bruce Eckel 2002 // See copyright notice in CopyRight.txt. public class CommaOperator { public static void main(String[] args) { for(int i = 1, j = i + 10; i < 5; i++, j = i * 2) { System.out.println("i= " + i + " j= " + j); } } } ///:~

Instanceof Operators This operator is used only for object reference variables. The operator checks whether the object is of a particular type(class type or interface type). InstanceOf operator is wriiten as: ( Object reference variable ) instanceOf (class/interface type)

Example of instanceof Operator class Vehicle {} public class Car extends Vehicle { public static void main(String args[]) { Vehicle a = new Car(); boolean result = a instanceof Car; System.out.println( result); }

The End ….. Thank You …..