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

Looping statements in Java
Looping statements in JavaLooping statements in Java
Looping statements in Java
Jin Castor
 

What's hot (20)

Java Data Types
Java Data TypesJava Data Types
Java Data Types
 
JVM
JVMJVM
JVM
 
Looping statements in Java
Looping statements in JavaLooping statements in Java
Looping statements in Java
 
Java threads
Java threadsJava threads
Java threads
 
Java(Polymorphism)
Java(Polymorphism)Java(Polymorphism)
Java(Polymorphism)
 
Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)
 
Control statements in java
Control statements in javaControl statements in java
Control statements in java
 
Control flow statements in java
Control flow statements in javaControl flow statements in java
Control flow statements in java
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
 
Applets
AppletsApplets
Applets
 
Methods in java
Methods in javaMethods in java
Methods in java
 
Array in c++
Array in c++Array in c++
Array in c++
 
java token
java tokenjava token
java token
 
Operators in java presentation
Operators in java presentationOperators in java presentation
Operators in java presentation
 
Control statements in c
Control statements in cControl statements in c
Control statements in c
 
Class and Objects in Java
Class and Objects in JavaClass and Objects in Java
Class and Objects in Java
 
MULTI THREADING IN JAVA
MULTI THREADING IN JAVAMULTI THREADING IN JAVA
MULTI THREADING IN JAVA
 
Introduction to Java Programming
Introduction to Java ProgrammingIntroduction to Java Programming
Introduction to Java Programming
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
Java conditional statements
Java conditional statementsJava conditional statements
Java conditional statements
 

Viewers also liked

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)

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
 
Java tutorial PPT
Java tutorial PPTJava tutorial PPT
Java tutorial PPT
 

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
 
Java operators
Java operatorsJava operators
Java operators
 
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)
 

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

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 

Recently uploaded (20)

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdf
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 

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 …..