SlideShare a Scribd company logo
1 of 30
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: ,[object Object]
Any primitive type variable - numeric and boolean
Reference variable to an object
A literal - numeric value, boolean value, or string.
An array element, "a[2]“
char primitive, which in numeric operations is treated as an unsigned two byte integer,[object Object]
Assignment Operators The assignment statements has the following syntax: <variable> = <expression>
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;
Example of ++ and -- operators public class Example  { 	public static void main(String[] args)    { 	 } } 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); > java example p = 6 q = 6 j = 7 r = 6 s = 6 >
Arithmetic Operators The arithmetic operators are used to construct mathematical expressions as in algebra.  Their operands are of numeric type.
Arithmetic Operators
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  >
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
Example of Bitwise Operators class Test {  public static void main(String args[]) {  int a = 60; /* 60 = 0011 1100 */  int b = 13; /* 13 = 0000 1101 */  int c = 0;  c = a & b; /* 12 = 0000 1100 */  System.out.println("a & b = " + c );  c = a | b; /* 61 = 0011 1101 */  System.out.println("a | b = " + c );
Example Cont., 	c = a ^ b; /* 49 = 0011 0001 */  	System.out.println("a ^ b = " + c );  	c = ~a; /*-61 = 1100 0011 */  	System.out.println("~a = " + c );  c = a << 2; /* 240 = 1111 0000 */ 	 System.out.println("a << 2 = " + c );  c = a >> 2; /* 215 = 1111 */  System.out.println("a >> 2 = " + c );  c = a >>> 2; /* 215 = 0000 1111 */  System.out.println("a >>> 2 = " + c );  } }
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
Relational Operators
Example of Relational Operators public LessThanExample  { publicstatic 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
Example of Logical Operators publicclassANDOperatorExample{ 	publicstatic 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

More Related Content

What's hot

Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
Abhilash Nair
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
Tech_MX
 

What's hot (20)

Java keywords
Java keywordsJava keywords
Java keywords
 
Java conditional statements
Java conditional statementsJava conditional statements
Java conditional statements
 
Java Data Types
Java Data TypesJava Data Types
Java Data Types
 
java programming- control statements
 java programming- control statements java programming- control statements
java programming- control statements
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
 
Java threads
Java threadsJava threads
Java threads
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
 
Control structures in java
Control structures in javaControl structures in java
Control structures in java
 
Java operators
Java operatorsJava operators
Java operators
 
Java package
Java packageJava package
Java package
 
oops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaoops concept in java | object oriented programming in java
oops concept in java | object oriented programming in java
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
 
Data Types & Variables in JAVA
Data Types & Variables in JAVAData Types & Variables in JAVA
Data Types & Variables in JAVA
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Java Collections
Java  Collections Java  Collections
Java Collections
 
Method Overloading In Java
Method Overloading In JavaMethod Overloading In Java
Method Overloading In Java
 
Applets in java
Applets in javaApplets in java
Applets in java
 
constructors in java ppt
constructors in java pptconstructors in java ppt
constructors in java ppt
 
Java Tokens
Java  TokensJava  Tokens
Java Tokens
 
Interface in java
Interface in javaInterface in java
Interface in java
 

Viewers also liked

Operators and Expressions in Java
Operators and Expressions in JavaOperators and Expressions in Java
Operators and Expressions in Java
Abhilash Nair
 
Control structures in Java
Control structures in JavaControl structures in Java
Control structures in Java
Ravi_Kant_Sahu
 
Control statements in Java
Control statements  in JavaControl statements  in Java
Control statements in Java
Jin Castor
 
Constants, Variables and Data Types in Java
Constants, Variables and Data Types in JavaConstants, Variables and Data Types in Java
Constants, Variables and Data Types in Java
Abhilash Nair
 
Operators and expressions
Operators and expressionsOperators and expressions
Operators and expressions
vishaljot_kaur
 
Control Structures
Control StructuresControl Structures
Control Structures
Ghaffar Khan
 

Viewers also liked (20)

Operators and Expressions in Java
Operators and Expressions in JavaOperators and Expressions in Java
Operators and Expressions in Java
 
Data types, Variables, Expressions & Arithmetic Operators in java
Data types, Variables, Expressions & Arithmetic Operators in javaData types, Variables, Expressions & Arithmetic Operators in java
Data types, Variables, Expressions & Arithmetic Operators in java
 
Operators in java
Operators in javaOperators in java
Operators in java
 
Control structures in Java
Control structures in JavaControl structures in Java
Control structures in Java
 
Control statements in Java
Control statements  in JavaControl statements  in Java
Control statements in Java
 
Constants, Variables and Data Types in Java
Constants, Variables and Data Types in JavaConstants, Variables and Data Types in Java
Constants, Variables and Data Types in Java
 
Operators and expressions
Operators and expressionsOperators and expressions
Operators and expressions
 
Java basic
Java basicJava basic
Java basic
 
Operators in java
Operators in javaOperators in java
Operators in java
 
Introduction to java
Introduction to javaIntroduction to java
Introduction to java
 
Control structures i
Control structures i Control structures i
Control structures i
 
15 bitwise operators
15 bitwise operators15 bitwise operators
15 bitwise operators
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Control Structures
Control StructuresControl Structures
Control Structures
 
Operators in java
Operators in javaOperators in java
Operators in java
 
Inheritance in JAVA PPT
Inheritance  in JAVA PPTInheritance  in JAVA PPT
Inheritance in JAVA PPT
 
Inheritance
InheritanceInheritance
Inheritance
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
 
Java packages
Java packagesJava packages
Java packages
 
Operator in c programming
Operator in c programmingOperator in c programming
Operator in c programming
 

Similar to Operators in java

Operator & Expression in c++
Operator & Expression in c++Operator & Expression in c++
Operator & Expression in c++
bajiajugal
 

Similar to Operators in java (20)

object oriented programming java lectures
object oriented programming java lecturesobject oriented programming java lectures
object oriented programming java lectures
 
Pj01 4-operators and control flow
Pj01 4-operators and control flowPj01 4-operators and control flow
Pj01 4-operators and control flow
 
Operators
OperatorsOperators
Operators
 
05 operators
05   operators05   operators
05 operators
 
Lecture 3
Lecture 3Lecture 3
Lecture 3
 
Effective Java - Still Effective After All These Years
Effective Java - Still Effective After All These YearsEffective Java - Still Effective After All These Years
Effective Java - Still Effective After All These Years
 
Java chapter 3
Java chapter 3Java chapter 3
Java chapter 3
 
Java unit 3
Java unit 3Java unit 3
Java unit 3
 
ppt on logical/arthimatical/conditional operators
ppt on logical/arthimatical/conditional operatorsppt on logical/arthimatical/conditional operators
ppt on logical/arthimatical/conditional operators
 
Fundamentals of computer programming by Dr. A. Charan Kumari
Fundamentals of computer programming by Dr. A. Charan KumariFundamentals of computer programming by Dr. A. Charan Kumari
Fundamentals of computer programming by Dr. A. Charan Kumari
 
02basics
02basics02basics
02basics
 
C++.pptx
C++.pptxC++.pptx
C++.pptx
 
Chapter 2&3 (java fundamentals and Control Structures).ppt
Chapter 2&3 (java fundamentals and Control Structures).pptChapter 2&3 (java fundamentals and Control Structures).ppt
Chapter 2&3 (java fundamentals and Control Structures).ppt
 
operators.pptx
operators.pptxoperators.pptx
operators.pptx
 
Operators
OperatorsOperators
Operators
 
Strings v.1.1
Strings v.1.1Strings v.1.1
Strings v.1.1
 
C++
C++C++
C++
 
Operator & Expression in c++
Operator & Expression in c++Operator & Expression in c++
Operator & Expression in c++
 
Ch5(loops)
Ch5(loops)Ch5(loops)
Ch5(loops)
 
C operators
C operatorsC operators
C operators
 

More from Then Murugeshwari

More from Then Murugeshwari (20)

Traffic safety
Traffic safetyTraffic safety
Traffic safety
 
P h indicators
P h indicatorsP h indicators
P h indicators
 
Avogadro's law
Avogadro's lawAvogadro's law
Avogadro's law
 
Resonance
ResonanceResonance
Resonance
 
Microwave remote sensing
Microwave remote sensingMicrowave remote sensing
Microwave remote sensing
 
Newton's law
Newton's lawNewton's law
Newton's law
 
Surface tension
Surface tensionSurface tension
Surface tension
 
Hook's law
Hook's lawHook's law
Hook's law
 
Hook's law
Hook's lawHook's law
Hook's law
 
ERP components
ERP componentsERP components
ERP components
 
Database fundamentals
Database fundamentalsDatabase fundamentals
Database fundamentals
 
Mosfet
MosfetMosfet
Mosfet
 
Operators
OperatorsOperators
Operators
 
Hiperlan
HiperlanHiperlan
Hiperlan
 
Bluetooth profile
Bluetooth profileBluetooth profile
Bluetooth profile
 
Router
RouterRouter
Router
 
Thread priorities
Thread prioritiesThread priorities
Thread priorities
 
Threads
ThreadsThreads
Threads
 
Identifiers
Identifiers Identifiers
Identifiers
 
Virtual ground
Virtual groundVirtual ground
Virtual ground
 

Recently uploaded

Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Recently uploaded (20)

Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Modernizing Legacy Systems Using Ballerina
Modernizing Legacy Systems Using BallerinaModernizing Legacy Systems Using Ballerina
Modernizing Legacy Systems Using Ballerina
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Decarbonising Commercial Real Estate: The Role of Operational Performance
Decarbonising Commercial Real Estate: The Role of Operational PerformanceDecarbonising Commercial Real Estate: The Role of Operational Performance
Decarbonising Commercial Real Estate: The Role of Operational Performance
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
Simplifying Mobile A11y Presentation.pptx
Simplifying Mobile A11y Presentation.pptxSimplifying Mobile A11y Presentation.pptx
Simplifying Mobile A11y Presentation.pptx
 
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
JohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptxJohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptx
 
API Governance and Monetization - The evolution of API governance
API Governance and Monetization -  The evolution of API governanceAPI Governance and Monetization -  The evolution of API governance
API Governance and Monetization - The evolution of API governance
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 

Operators in java

  • 2. 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.
  • 3.
  • 4. Any primitive type variable - numeric and boolean
  • 6. A literal - numeric value, boolean value, or string.
  • 8.
  • 9. Assignment Operators The assignment statements has the following syntax: <variable> = <expression>
  • 11. Increment and Decrement operators++ and -- The increment and decrement operators add an integer variable by one. increment operator: two successive plus signs, ++ decrement operator: --
  • 12. Increment and Decrement operators++ and -- Common Shorthand a = a + 1; a++; or ++a; a = a - 1; a--; or --a;
  • 13. Example of ++ and -- operators public class Example { public static void main(String[] args) { } } 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); > java example p = 6 q = 6 j = 7 r = 6 s = 6 >
  • 14. Arithmetic Operators The arithmetic operators are used to construct mathematical expressions as in algebra. Their operands are of numeric type.
  • 16. 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 >
  • 17. 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.
  • 19. Example of Bitwise Operators class Test { public static void main(String args[]) { int a = 60; /* 60 = 0011 1100 */ int b = 13; /* 13 = 0000 1101 */ int c = 0; c = a & b; /* 12 = 0000 1100 */ System.out.println("a & b = " + c ); c = a | b; /* 61 = 0011 1101 */ System.out.println("a | b = " + c );
  • 20. Example Cont., c = a ^ b; /* 49 = 0011 0001 */ System.out.println("a ^ b = " + c ); c = ~a; /*-61 = 1100 0011 */ System.out.println("~a = " + c ); c = a << 2; /* 240 = 1111 0000 */ System.out.println("a << 2 = " + c ); c = a >> 2; /* 215 = 1111 */ System.out.println("a >> 2 = " + c ); c = a >>> 2; /* 215 = 0000 1111 */ System.out.println("a >>> 2 = " + c ); } }
  • 21. 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. 
  • 24. Example of Relational Operators public LessThanExample { publicstatic void main(String args[]) { int a = 5; int b = 10;   if(a < b) { System.out.println("a is less than b"); } } }
  • 25. Logical Operators These logical operators work only on boolean operands. Their return values are always boolean.
  • 27. Example of Logical Operators publicclassANDOperatorExample{ publicstatic 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."); } } }
  • 28. 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
  • 29. 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;
  • 30. 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
  • 31. Example of Comma Operator //: c03:CommaOperator.java// From 'Thinking in Java, 3rd ed.' (c) Bruce Eckel 2002// www.BruceEckel.com. 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);    }  }} ///:~                    
  • 32. 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:
  • 34. The End ….. Thank You …..