SlideShare a Scribd company logo
1 of 7
Download to read offline
http://www.tutorialspoint.com/java/java_basic_operators.htm Copyright © tutorialspoint.com
JAVA - BASIC OPERATORSJAVA - BASIC OPERATORS
Java provides a rich set of operators to manipulate variables. We can divide all the Java operators
into the following groups:
Arithmetic Operators
Relational Operators
Bitwise Operators
Logical Operators
Assignment Operators
Misc Operators
The Arithmetic Operators:
Arithmetic operators are used in mathematical expressions in the same way that they are used in
algebra. The following table lists the arithmetic operators:
Assume integer variable A holds 10 and variable B holds 20, then:
Show Examples
SR.NO Operator and Example
1 + Addition
Adds values on either side of the operator
Example: A + B will give 30
2 - Subtraction
Subtracts right hand operand from left hand operand
Example: A - B will give -10
3 * Multiplication
Multiplies values on either side of the operator
Example: A * B will give 200
4 / Division
Divides left hand operand by right hand operand
Example: B / A will give 2
5 % Modulus
Divides left hand operand by right hand operand and returns remainder
Example: B % A will give 0
6 ++ Increment
Increases the value of operand by 1
Example: B++ gives 21
7 -- Decrement
Decreases the value of operand by 1
Example: B-- gives 19
The Relational Operators:
There are following relational operators supported by Java language
Assume variable A holds 10 and variable B holds 20, then:
Show Examples
SR.NO Operator and Description
1 == equalto
Checks if the values of two operands are equal or not, if yes then condition becomes
true.
Example: A == B is not true.
2 != notequalto
Checks if the values of two operands are equal or not, if values are not equal then
condition becomes true.
Example: A! = B is true.
3 > greaterthan
Checks if the value of left operand is greater than the value of right operand, if yes
then condition becomes true.
Example: A > B is not true.
4 < lessthan
Checks if the value of left operand is less than the value of right operand, if yes then
condition becomes true.
Example: A < B is true.
5 >= greaterthanorequalto
Checks if the value of left operand is greater than or equal to the value of right
operand, if yes then condition becomes true.
Example A >= B is not true.
6 <= lessthanorequalto
Checks if the value of left operand is less than or equal to the value of right operand, if
yes then condition becomes true.
exampleA <= B is true.
The Bitwise Operators:
Java defines several bitwise operators, which can be applied to the integer types, long, int, short,
char, and byte.
Bitwise operator works on bits and performs bit-by-bit operation. Assume if a = 60; and b = 13;
now in binary format they will be as follows:
a = 0011 1100
b = 0000 1101
-----------------
a&b = 0000 1100
a|b = 0011 1101
a^b = 0011 0001
~a = 1100 0011
The following table lists the bitwise operators:
Assume integer variable A holds 60 and variable B holds 13 then:
Show Examples
SR.NO Operator and Description
1 & bitwiseand
Binary AND Operator copies a bit to the result if it exists in both operands.
Example: A & B will give 12 which is 0000 1100
2 | bitwiseor
Binary OR Operator copies a bit if it exists in either operand.
Example: A | B will give 61 which is 0011 1101
3 ^ bitwiseXOR
Binary XOR Operator copies the bit if it is set in one operand but not both.
Example: AB will give 49 which is 0011 0001
4 ~ bitwisecompliment
Binary Ones Complement Operator is unary and has the effect of 'flipping' bits.
Example: A will give -61 which is 1100 0011 in 2's complement form due to a signed
binary number.
5 << leftshift
Binary Left Shift Operator. The left operands value is moved left by the number of bits
specified by the right operand
Example: A << 2 will give 240 which is 1111 0000
6 >> rightshift
Binary Right Shift Operator. The left operands value is moved right by the number of
bits specified by the right operand.
Example: A >> 2 will give 15 which is 1111
7 >>> zerofillrightshift
Shift right zero fill operator. The left operands value is moved right by the number of
bits specified by the right operand and shifted values are filled up with zeros.
Example: A >>>2 will give 15 which is 0000 1111
The Logical Operators:
The following table lists the logical operators:
Assume Boolean variables A holds true and variable B holds false, then:
Show Examples
Operator Description
1 && logicaland
Called Logical AND operator. If both the operands are non-zero, then the condition
becomes true.
Example A && B is false.
2 || logicalor
Called Logical OR Operator. If any of the two operands are non-zero, then the
condition becomes true.
Example A | | B is true.
3 ! logicalnot
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 ! A && B is true.
The Assignment Operators:
There are following assignment operators supported by Java language:
Show Examples
SR.NO Operator and Description
1 =
Simple assignment operator, Assigns values from right side operands to left side
operand.
Example: C = A + B will assign value of A + B into C
2 +=
Add AND assignment operator, It adds right operand to the left operand and assign the
result to left operand.
Example: C += A is equivalent to C = C + A
3 -=
Subtract AND assignment operator, It subtracts right operand from the left operand
and assign the result to left operand.
Example:C -= A is equivalent to C = C - A
4 *=
Multiply AND assignment operator, It multiplies right operand with the left operand and
assign the result to left operand.
Example: C *= A is equivalent to C = C * A
5 /=
Divide AND assignment operator, It divides left operand with the right operand and
assign the result to left operand
ExampleC /= A is equivalent to C = C / A
6 %=
Modulus AND assignment operator, It takes modulus using two operands and assign
the result to left operand.
Example: C %= A is equivalent to C = C % A
7 <<=
Left shift AND assignment operator.
ExampleC <<= 2 is same as C = C << 2
8 >>=
Right shift AND assignment operator
Example C >>= 2 is same as C = C >> 2
9 &=
Bitwise AND assignment operator.
Example: C &= 2 is same as C = C & 2
10 ^=
bitwise exclusive OR and assignment operator.
Example: C ^= 2 is same as C = C ^ 2
11 |=
bitwise inclusive OR and assignment operator.
Example: C |= 2 is same as C = C | 2
Miscellaneous Operators
There are few other operators supported by Java Language.
Conditional Operator ? :
Conditional operator is also known as the ternary operator. This operator consists of three
operands and is used to evaluate Boolean expressions. The goal of the operator is to decide which
value should be assigned to the variable. The operator is written as:
variable x = (expression) ? value if true : value if false
Following is the example:
public class Test {
public static void main(String args[]){
int a, b;
a = 10;
b = (a == 1) ? 20: 30;
System.out.println( "Value of b is : " + b );
b = (a == 10) ? 20: 30;
System.out.println( "Value of b is : " + b );
}
}
This would produce the following result −
Value of b is : 30
Value of b is : 20
instance of Operator:
This operator is used only for object reference variables. The operator checks whether the object
is of a particular type classtypeorinterfacetype. instanceof operator is wriiten as:
( Object reference variable ) instanceof (class/interface type)
If the object referred by the variable on the left side of the operator passes the IS-A check for the
class/interface type on the right side, then the result will be true. Following is the example:
public class Test {
public static void main(String args[]){
String name = "James";
// following will return true since name is type of String
boolean result = name instanceof String;
System.out.println( result );
}
}
This would produce the following result:
true
This operator will still return true if the object being compared is the assignment compatible with
the type on the right. Following is one more example:
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 );
}
}
This would produce the following result:
true
Precedence of Java Operators:
Operator precedence determines the grouping of terms in an expression. This affects how an
expression is evaluated. Certain operators have higher precedence than others; for example, the
multiplication operator has higher precedence than the addition operator:
For example, x = 7 + 3 * 2; here x is assigned 13, not 20 because operator * has higher
precedence than +, so it first gets multiplied with 3*2 and then adds into 7.
Here, operators with the highest precedence appear at the top of the table, those with the lowest
appear at the bottom. Within an expression, higher precedence operators will be evaluated first.
Category Operator Associativity
Postfix [] . dotoperator Left toright
Unary ++ - - ! ~ Right to left
Multiplicative * / % Left to right
Additive + - Left to right
Shift >> >>> << Left to right
Relational > >= < <= Left to right
Equality == != Left to right
Bitwise AND & Left to right
Bitwise XOR ^ Left to right
Bitwise OR | Left to right
Logical AND && Left to right
Logical OR || Left to right
Conditional ?: Right to left
Assignment = += -= *= /= %= >>= <<= &= ^= |= Right to left
Comma , Left to right
What is Next?
Next chapter would explain about loop control in Java programming. The chapter will describe
various types of loops and how these loops can be used in Java program development and for what
purposes they are being used.
Loading [MathJax]/jax/output/HTML-CSS/jax.js

More Related Content

What's hot

CBSE Class XI :- Operators in C++
CBSE Class XI :- Operators in C++CBSE Class XI :- Operators in C++
CBSE Class XI :- Operators in C++Pranav Ghildiyal
 
Constructor and destructors
Constructor and destructorsConstructor and destructors
Constructor and destructorsdivyalakshmi77
 
C programming operators
C programming operatorsC programming operators
C programming operatorsSuneel Dogra
 
Lecture 2 C++ | Variable Scope, Operators in c++
Lecture 2 C++ | Variable Scope, Operators in c++Lecture 2 C++ | Variable Scope, Operators in c++
Lecture 2 C++ | Variable Scope, Operators in c++Himanshu Kaushik
 
Operators in Python
Operators in PythonOperators in Python
Operators in PythonAnusuya123
 
Operation and expression in c++
Operation and expression in c++Operation and expression in c++
Operation and expression in c++Online
 
Python Training in Bangalore | Python Operators | Learnbay.in
Python Training in Bangalore | Python Operators | Learnbay.inPython Training in Bangalore | Python Operators | Learnbay.in
Python Training in Bangalore | Python Operators | Learnbay.inLearnbayin
 
Chapter 5 - Operators in C++
Chapter 5 - Operators in C++Chapter 5 - Operators in C++
Chapter 5 - Operators in C++Deepak Singh
 
Operators in c programming
Operators in c programmingOperators in c programming
Operators in c programmingsavitamhaske
 

What's hot (19)

CBSE Class XI :- Operators in C++
CBSE Class XI :- Operators in C++CBSE Class XI :- Operators in C++
CBSE Class XI :- Operators in C++
 
Constructor and destructors
Constructor and destructorsConstructor and destructors
Constructor and destructors
 
C programming operators
C programming operatorsC programming operators
C programming operators
 
Operators in c++
Operators in c++Operators in c++
Operators in c++
 
Lecture 2 C++ | Variable Scope, Operators in c++
Lecture 2 C++ | Variable Scope, Operators in c++Lecture 2 C++ | Variable Scope, Operators in c++
Lecture 2 C++ | Variable Scope, Operators in c++
 
Operators in Python
Operators in PythonOperators in Python
Operators in Python
 
Session03 operators
Session03 operatorsSession03 operators
Session03 operators
 
C – operators and expressions
C – operators and expressionsC – operators and expressions
C – operators and expressions
 
Operators
OperatorsOperators
Operators
 
Python : basic operators
Python : basic operatorsPython : basic operators
Python : basic operators
 
Operation and expression in c++
Operation and expression in c++Operation and expression in c++
Operation and expression in c++
 
Python operators
Python operatorsPython operators
Python operators
 
07 ruby operators
07 ruby operators07 ruby operators
07 ruby operators
 
Java script session 4
Java script session 4Java script session 4
Java script session 4
 
SPL 6 | Operators in C
SPL 6 | Operators in CSPL 6 | Operators in C
SPL 6 | Operators in C
 
Python Training in Bangalore | Python Operators | Learnbay.in
Python Training in Bangalore | Python Operators | Learnbay.inPython Training in Bangalore | Python Operators | Learnbay.in
Python Training in Bangalore | Python Operators | Learnbay.in
 
Chapter 5 - Operators in C++
Chapter 5 - Operators in C++Chapter 5 - Operators in C++
Chapter 5 - Operators in C++
 
Operators
OperatorsOperators
Operators
 
Operators in c programming
Operators in c programmingOperators in c programming
Operators in c programming
 

Viewers also liked

האקרמן קידום אתרים
האקרמן קידום אתריםהאקרמן קידום אתרים
האקרמן קידום אתריםroi-boharov
 
k10798 aaftab alam opc me 6th sem
k10798 aaftab alam opc me 6th semk10798 aaftab alam opc me 6th sem
k10798 aaftab alam opc me 6th semGuddu Ali
 
3 1 forma, espacio y orden bioarmónico
3 1 forma, espacio y orden bioarmónico 3 1 forma, espacio y orden bioarmónico
3 1 forma, espacio y orden bioarmónico planarqubvbolivar
 
Carga y descarga del condensador..
Carga y descarga del condensador..Carga y descarga del condensador..
Carga y descarga del condensador..TecnoEstudio
 
2 12 el espacio comunitario y la construcción participativa – diseño ii
2 12 el espacio comunitario y la construcción participativa – diseño ii2 12 el espacio comunitario y la construcción participativa – diseño ii
2 12 el espacio comunitario y la construcción participativa – diseño iiplanarqubvbolivar
 
K10870 aaftab alam ic engine me 6 th sem
K10870 aaftab alam ic engine me 6 th semK10870 aaftab alam ic engine me 6 th sem
K10870 aaftab alam ic engine me 6 th semGuddu Ali
 
Marketinški pospešek v leto 2017, prof. dr. Vesna Žabkar, EF
Marketinški pospešek v leto 2017, prof. dr. Vesna Žabkar, EFMarketinški pospešek v leto 2017, prof. dr. Vesna Žabkar, EF
Marketinški pospešek v leto 2017, prof. dr. Vesna Žabkar, EFDruštvo za marketing Slovenije
 
Statistical signal processing(1)
Statistical signal processing(1)Statistical signal processing(1)
Statistical signal processing(1)Gopi Saiteja
 
Sesi suai kenal perjumpaan pertama ABM TP
Sesi suai kenal perjumpaan pertama ABM TPSesi suai kenal perjumpaan pertama ABM TP
Sesi suai kenal perjumpaan pertama ABM TPWhyin Chong
 
Manajemen Nyeri Nonfarmakologi
Manajemen Nyeri NonfarmakologiManajemen Nyeri Nonfarmakologi
Manajemen Nyeri NonfarmakologiRizqah Auliya
 
K10990 GUDDU ALI IC ENGINE ME 6TH SEM
K10990 GUDDU ALI IC ENGINE ME 6TH SEMK10990 GUDDU ALI IC ENGINE ME 6TH SEM
K10990 GUDDU ALI IC ENGINE ME 6TH SEMGuddu Ali
 
Permainan tradisional melayu
Permainan tradisional melayuPermainan tradisional melayu
Permainan tradisional melayu@f!Q@H @F!N@
 
Divider dan cuti negeri kumpulan b
Divider dan cuti negeri kumpulan bDivider dan cuti negeri kumpulan b
Divider dan cuti negeri kumpulan bammarwajdi
 
flying Spy robot by sanjeev
flying Spy robot by sanjeevflying Spy robot by sanjeev
flying Spy robot by sanjeevSanjeev Pandey
 
NOTA RINGKAS BAB 2
NOTA RINGKAS BAB 2NOTA RINGKAS BAB 2
NOTA RINGKAS BAB 2Meera Zaheed
 

Viewers also liked (20)

Matias molina
Matias molinaMatias molina
Matias molina
 
האקרמן קידום אתרים
האקרמן קידום אתריםהאקרמן קידום אתרים
האקרמן קידום אתרים
 
k10798 aaftab alam opc me 6th sem
k10798 aaftab alam opc me 6th semk10798 aaftab alam opc me 6th sem
k10798 aaftab alam opc me 6th sem
 
3 1 forma, espacio y orden bioarmónico
3 1 forma, espacio y orden bioarmónico 3 1 forma, espacio y orden bioarmónico
3 1 forma, espacio y orden bioarmónico
 
Ppt
PptPpt
Ppt
 
Carga y descarga del condensador..
Carga y descarga del condensador..Carga y descarga del condensador..
Carga y descarga del condensador..
 
2 12 el espacio comunitario y la construcción participativa – diseño ii
2 12 el espacio comunitario y la construcción participativa – diseño ii2 12 el espacio comunitario y la construcción participativa – diseño ii
2 12 el espacio comunitario y la construcción participativa – diseño ii
 
Boiler
BoilerBoiler
Boiler
 
K10870 aaftab alam ic engine me 6 th sem
K10870 aaftab alam ic engine me 6 th semK10870 aaftab alam ic engine me 6 th sem
K10870 aaftab alam ic engine me 6 th sem
 
Marketinški pospešek v leto 2017, prof. dr. Vesna Žabkar, EF
Marketinški pospešek v leto 2017, prof. dr. Vesna Žabkar, EFMarketinški pospešek v leto 2017, prof. dr. Vesna Žabkar, EF
Marketinški pospešek v leto 2017, prof. dr. Vesna Žabkar, EF
 
Statistical signal processing(1)
Statistical signal processing(1)Statistical signal processing(1)
Statistical signal processing(1)
 
Sesi suai kenal perjumpaan pertama ABM TP
Sesi suai kenal perjumpaan pertama ABM TPSesi suai kenal perjumpaan pertama ABM TP
Sesi suai kenal perjumpaan pertama ABM TP
 
Manajemen Nyeri Nonfarmakologi
Manajemen Nyeri NonfarmakologiManajemen Nyeri Nonfarmakologi
Manajemen Nyeri Nonfarmakologi
 
Takziah
TakziahTakziah
Takziah
 
K10990 GUDDU ALI IC ENGINE ME 6TH SEM
K10990 GUDDU ALI IC ENGINE ME 6TH SEMK10990 GUDDU ALI IC ENGINE ME 6TH SEM
K10990 GUDDU ALI IC ENGINE ME 6TH SEM
 
Jetstar marketing report
Jetstar marketing reportJetstar marketing report
Jetstar marketing report
 
Permainan tradisional melayu
Permainan tradisional melayuPermainan tradisional melayu
Permainan tradisional melayu
 
Divider dan cuti negeri kumpulan b
Divider dan cuti negeri kumpulan bDivider dan cuti negeri kumpulan b
Divider dan cuti negeri kumpulan b
 
flying Spy robot by sanjeev
flying Spy robot by sanjeevflying Spy robot by sanjeev
flying Spy robot by sanjeev
 
NOTA RINGKAS BAB 2
NOTA RINGKAS BAB 2NOTA RINGKAS BAB 2
NOTA RINGKAS BAB 2
 

Similar to Java basic operators

Similar to Java basic operators (20)

itft-Operators in java
itft-Operators in javaitft-Operators in java
itft-Operators in java
 
java operators
 java operators java operators
java operators
 
Operator 04 (js)
Operator 04 (js)Operator 04 (js)
Operator 04 (js)
 
Opeartor &amp; expression
Opeartor &amp; expressionOpeartor &amp; expression
Opeartor &amp; expression
 
Java unit1 b- Java Operators to Methods
Java  unit1 b- Java Operators to MethodsJava  unit1 b- Java Operators to Methods
Java unit1 b- Java Operators to Methods
 
Bit shift operators
Bit shift operatorsBit shift operators
Bit shift operators
 
python operators.ppt
python operators.pptpython operators.ppt
python operators.ppt
 
Python operators part2
Python operators part2Python operators part2
Python operators part2
 
Py-Slides-2 (1).ppt
Py-Slides-2 (1).pptPy-Slides-2 (1).ppt
Py-Slides-2 (1).ppt
 
Py-Slides-2.ppt
Py-Slides-2.pptPy-Slides-2.ppt
Py-Slides-2.ppt
 
Py-Slides-2.ppt
Py-Slides-2.pptPy-Slides-2.ppt
Py-Slides-2.ppt
 
Operators and it's type
Operators and it's type Operators and it's type
Operators and it's type
 
Conditional and special operators
Conditional and special operatorsConditional and special operators
Conditional and special operators
 
Operators In Java Part - 8
Operators In Java Part - 8Operators In Java Part - 8
Operators In Java Part - 8
 
Computer programming 2 Lesson 7
Computer programming 2  Lesson 7Computer programming 2  Lesson 7
Computer programming 2 Lesson 7
 
Operators used in vb.net
Operators used in vb.netOperators used in vb.net
Operators used in vb.net
 
Programming C Part 02
Programming C Part 02Programming C Part 02
Programming C Part 02
 
Types of Operators in C
Types of Operators in CTypes of Operators in C
Types of Operators in C
 
Operators in C & C++ Language
Operators in C & C++ LanguageOperators in C & C++ Language
Operators in C & C++ Language
 
Operators in C/C++
Operators in C/C++Operators in C/C++
Operators in C/C++
 

More from Emmanuel Alimpolos

Presentationonmemo 131008143853-phpapp02
Presentationonmemo 131008143853-phpapp02Presentationonmemo 131008143853-phpapp02
Presentationonmemo 131008143853-phpapp02Emmanuel Alimpolos
 
Recruitment selection-1230614550740619-2
Recruitment selection-1230614550740619-2Recruitment selection-1230614550740619-2
Recruitment selection-1230614550740619-2Emmanuel Alimpolos
 
Pagbabagongmorpoponemiko 160907063021
Pagbabagongmorpoponemiko 160907063021Pagbabagongmorpoponemiko 160907063021
Pagbabagongmorpoponemiko 160907063021Emmanuel Alimpolos
 
Karlvinreportpresentationsafilipino 161108134808
Karlvinreportpresentationsafilipino 161108134808Karlvinreportpresentationsafilipino 161108134808
Karlvinreportpresentationsafilipino 161108134808Emmanuel Alimpolos
 
Howtowriteamemo 090920105907-phpapp02
Howtowriteamemo 090920105907-phpapp02Howtowriteamemo 090920105907-phpapp02
Howtowriteamemo 090920105907-phpapp02Emmanuel Alimpolos
 
Applyingforajob 120613221830-phpapp01
Applyingforajob 120613221830-phpapp01Applyingforajob 120613221830-phpapp01
Applyingforajob 120613221830-phpapp01Emmanuel Alimpolos
 
01microsoftofficeword2007introductionandparts 130906003510-
01microsoftofficeword2007introductionandparts 130906003510-01microsoftofficeword2007introductionandparts 130906003510-
01microsoftofficeword2007introductionandparts 130906003510-Emmanuel Alimpolos
 
2 2amortization-110921085439-phpapp01
2 2amortization-110921085439-phpapp012 2amortization-110921085439-phpapp01
2 2amortization-110921085439-phpapp01Emmanuel Alimpolos
 
2 3sinkingfunds-110921085502-phpapp02
2 3sinkingfunds-110921085502-phpapp022 3sinkingfunds-110921085502-phpapp02
2 3sinkingfunds-110921085502-phpapp02Emmanuel Alimpolos
 
Coherentwriting 121002082424-phpapp01
Coherentwriting 121002082424-phpapp01Coherentwriting 121002082424-phpapp01
Coherentwriting 121002082424-phpapp01Emmanuel Alimpolos
 
Batayangkaalamansapagsulat 140811070400-phpapp01
Batayangkaalamansapagsulat 140811070400-phpapp01Batayangkaalamansapagsulat 140811070400-phpapp01
Batayangkaalamansapagsulat 140811070400-phpapp01Emmanuel Alimpolos
 

More from Emmanuel Alimpolos (20)

Amortization
AmortizationAmortization
Amortization
 
Presentationonmemo 131008143853-phpapp02
Presentationonmemo 131008143853-phpapp02Presentationonmemo 131008143853-phpapp02
Presentationonmemo 131008143853-phpapp02
 
Unit 1-120510090718-phpapp01
Unit 1-120510090718-phpapp01Unit 1-120510090718-phpapp01
Unit 1-120510090718-phpapp01
 
Recruitment selection-1230614550740619-2
Recruitment selection-1230614550740619-2Recruitment selection-1230614550740619-2
Recruitment selection-1230614550740619-2
 
Pagbabagongmorpoponemiko 160907063021
Pagbabagongmorpoponemiko 160907063021Pagbabagongmorpoponemiko 160907063021
Pagbabagongmorpoponemiko 160907063021
 
Karlvinreportpresentationsafilipino 161108134808
Karlvinreportpresentationsafilipino 161108134808Karlvinreportpresentationsafilipino 161108134808
Karlvinreportpresentationsafilipino 161108134808
 
Howtowriteamemo 090920105907-phpapp02
Howtowriteamemo 090920105907-phpapp02Howtowriteamemo 090920105907-phpapp02
Howtowriteamemo 090920105907-phpapp02
 
Applyingforajob 120613221830-phpapp01
Applyingforajob 120613221830-phpapp01Applyingforajob 120613221830-phpapp01
Applyingforajob 120613221830-phpapp01
 
01microsoftofficeword2007introductionandparts 130906003510-
01microsoftofficeword2007introductionandparts 130906003510-01microsoftofficeword2007introductionandparts 130906003510-
01microsoftofficeword2007introductionandparts 130906003510-
 
2 2amortization-110921085439-phpapp01
2 2amortization-110921085439-phpapp012 2amortization-110921085439-phpapp01
2 2amortization-110921085439-phpapp01
 
2 3sinkingfunds-110921085502-phpapp02
2 3sinkingfunds-110921085502-phpapp022 3sinkingfunds-110921085502-phpapp02
2 3sinkingfunds-110921085502-phpapp02
 
Probability
ProbabilityProbability
Probability
 
Midterm
MidtermMidterm
Midterm
 
J introtojava1-pdf
J introtojava1-pdfJ introtojava1-pdf
J introtojava1-pdf
 
Energy, work, power
Energy, work, powerEnergy, work, power
Energy, work, power
 
Java basic operators
Java basic operatorsJava basic operators
Java basic operators
 
statistic midterm
statistic midtermstatistic midterm
statistic midterm
 
Coherentwriting 121002082424-phpapp01
Coherentwriting 121002082424-phpapp01Coherentwriting 121002082424-phpapp01
Coherentwriting 121002082424-phpapp01
 
Batayangkaalamansapagsulat 140811070400-phpapp01
Batayangkaalamansapagsulat 140811070400-phpapp01Batayangkaalamansapagsulat 140811070400-phpapp01
Batayangkaalamansapagsulat 140811070400-phpapp01
 
Pagsulat
PagsulatPagsulat
Pagsulat
 

Recently uploaded

Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptxSherlyMaeNeri
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........LeaCamillePacle
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxDr.Ibrahim Hassaan
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxChelloAnnAsuncion2
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfSpandanaRallapalli
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 

Recently uploaded (20)

Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptx
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........
 
Rapple "Scholarly Communications and the Sustainable Development Goals"
Rapple "Scholarly Communications and the Sustainable Development Goals"Rapple "Scholarly Communications and the Sustainable Development Goals"
Rapple "Scholarly Communications and the Sustainable Development Goals"
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptx
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdf
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 

Java basic operators

  • 1. http://www.tutorialspoint.com/java/java_basic_operators.htm Copyright © tutorialspoint.com JAVA - BASIC OPERATORSJAVA - BASIC OPERATORS Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups: Arithmetic Operators Relational Operators Bitwise Operators Logical Operators Assignment Operators Misc Operators The Arithmetic Operators: Arithmetic operators are used in mathematical expressions in the same way that they are used in algebra. The following table lists the arithmetic operators: Assume integer variable A holds 10 and variable B holds 20, then: Show Examples SR.NO Operator and Example 1 + Addition Adds values on either side of the operator Example: A + B will give 30 2 - Subtraction Subtracts right hand operand from left hand operand Example: A - B will give -10 3 * Multiplication Multiplies values on either side of the operator Example: A * B will give 200 4 / Division Divides left hand operand by right hand operand Example: B / A will give 2 5 % Modulus Divides left hand operand by right hand operand and returns remainder Example: B % A will give 0 6 ++ Increment
  • 2. Increases the value of operand by 1 Example: B++ gives 21 7 -- Decrement Decreases the value of operand by 1 Example: B-- gives 19 The Relational Operators: There are following relational operators supported by Java language Assume variable A holds 10 and variable B holds 20, then: Show Examples SR.NO Operator and Description 1 == equalto Checks if the values of two operands are equal or not, if yes then condition becomes true. Example: A == B is not true. 2 != notequalto Checks if the values of two operands are equal or not, if values are not equal then condition becomes true. Example: A! = B is true. 3 > greaterthan Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true. Example: A > B is not true. 4 < lessthan Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true. Example: A < B is true. 5 >= greaterthanorequalto Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true. Example A >= B is not true. 6 <= lessthanorequalto Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true. exampleA <= B is true. The Bitwise Operators:
  • 3. Java defines several bitwise operators, which can be applied to the integer types, long, int, short, char, and byte. Bitwise operator works on bits and performs bit-by-bit operation. Assume if a = 60; and b = 13; now in binary format they will be as follows: a = 0011 1100 b = 0000 1101 ----------------- a&b = 0000 1100 a|b = 0011 1101 a^b = 0011 0001 ~a = 1100 0011 The following table lists the bitwise operators: Assume integer variable A holds 60 and variable B holds 13 then: Show Examples SR.NO Operator and Description 1 & bitwiseand Binary AND Operator copies a bit to the result if it exists in both operands. Example: A & B will give 12 which is 0000 1100 2 | bitwiseor Binary OR Operator copies a bit if it exists in either operand. Example: A | B will give 61 which is 0011 1101 3 ^ bitwiseXOR Binary XOR Operator copies the bit if it is set in one operand but not both. Example: AB will give 49 which is 0011 0001 4 ~ bitwisecompliment Binary Ones Complement Operator is unary and has the effect of 'flipping' bits. Example: A will give -61 which is 1100 0011 in 2's complement form due to a signed binary number. 5 << leftshift Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand Example: A << 2 will give 240 which is 1111 0000 6 >> rightshift Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand. Example: A >> 2 will give 15 which is 1111
  • 4. 7 >>> zerofillrightshift Shift right zero fill operator. The left operands value is moved right by the number of bits specified by the right operand and shifted values are filled up with zeros. Example: A >>>2 will give 15 which is 0000 1111 The Logical Operators: The following table lists the logical operators: Assume Boolean variables A holds true and variable B holds false, then: Show Examples Operator Description 1 && logicaland Called Logical AND operator. If both the operands are non-zero, then the condition becomes true. Example A && B is false. 2 || logicalor Called Logical OR Operator. If any of the two operands are non-zero, then the condition becomes true. Example A | | B is true. 3 ! logicalnot 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 ! A && B is true. The Assignment Operators: There are following assignment operators supported by Java language: Show Examples SR.NO Operator and Description 1 = Simple assignment operator, Assigns values from right side operands to left side operand. Example: C = A + B will assign value of A + B into C 2 += Add AND assignment operator, It adds right operand to the left operand and assign the result to left operand. Example: C += A is equivalent to C = C + A 3 -=
  • 5. Subtract AND assignment operator, It subtracts right operand from the left operand and assign the result to left operand. Example:C -= A is equivalent to C = C - A 4 *= Multiply AND assignment operator, It multiplies right operand with the left operand and assign the result to left operand. Example: C *= A is equivalent to C = C * A 5 /= Divide AND assignment operator, It divides left operand with the right operand and assign the result to left operand ExampleC /= A is equivalent to C = C / A 6 %= Modulus AND assignment operator, It takes modulus using two operands and assign the result to left operand. Example: C %= A is equivalent to C = C % A 7 <<= Left shift AND assignment operator. ExampleC <<= 2 is same as C = C << 2 8 >>= Right shift AND assignment operator Example C >>= 2 is same as C = C >> 2 9 &= Bitwise AND assignment operator. Example: C &= 2 is same as C = C & 2 10 ^= bitwise exclusive OR and assignment operator. Example: C ^= 2 is same as C = C ^ 2 11 |= bitwise inclusive OR and assignment operator. Example: C |= 2 is same as C = C | 2 Miscellaneous Operators There are few other operators supported by Java Language. Conditional Operator ? : Conditional operator is also known as the ternary operator. This operator consists of three operands and is used to evaluate Boolean expressions. The goal of the operator is to decide which value should be assigned to the variable. The operator is written as: variable x = (expression) ? value if true : value if false
  • 6. Following is the example: public class Test { public static void main(String args[]){ int a, b; a = 10; b = (a == 1) ? 20: 30; System.out.println( "Value of b is : " + b ); b = (a == 10) ? 20: 30; System.out.println( "Value of b is : " + b ); } } This would produce the following result − Value of b is : 30 Value of b is : 20 instance of Operator: This operator is used only for object reference variables. The operator checks whether the object is of a particular type classtypeorinterfacetype. instanceof operator is wriiten as: ( Object reference variable ) instanceof (class/interface type) If the object referred by the variable on the left side of the operator passes the IS-A check for the class/interface type on the right side, then the result will be true. Following is the example: public class Test { public static void main(String args[]){ String name = "James"; // following will return true since name is type of String boolean result = name instanceof String; System.out.println( result ); } } This would produce the following result: true This operator will still return true if the object being compared is the assignment compatible with the type on the right. Following is one more example: 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 ); } } This would produce the following result: true Precedence of Java Operators:
  • 7. Operator precedence determines the grouping of terms in an expression. This affects how an expression is evaluated. Certain operators have higher precedence than others; for example, the multiplication operator has higher precedence than the addition operator: For example, x = 7 + 3 * 2; here x is assigned 13, not 20 because operator * has higher precedence than +, so it first gets multiplied with 3*2 and then adds into 7. Here, operators with the highest precedence appear at the top of the table, those with the lowest appear at the bottom. Within an expression, higher precedence operators will be evaluated first. Category Operator Associativity Postfix [] . dotoperator Left toright Unary ++ - - ! ~ Right to left Multiplicative * / % Left to right Additive + - Left to right Shift >> >>> << Left to right Relational > >= < <= Left to right Equality == != Left to right Bitwise AND & Left to right Bitwise XOR ^ Left to right Bitwise OR | Left to right Logical AND && Left to right Logical OR || Left to right Conditional ?: Right to left Assignment = += -= *= /= %= >>= <<= &= ^= |= Right to left Comma , Left to right What is Next? Next chapter would explain about loop control in Java programming. The chapter will describe various types of loops and how these loops can be used in Java program development and for what purposes they are being used. Loading [MathJax]/jax/output/HTML-CSS/jax.js