- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjava_operators.java
More file actions
Latest commit
90 lines (71 loc) · 3.02 KB
/
Copy pathjava_operators.java
File metadata and controls
90 lines (71 loc) · 3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
JavaOperators
JavaOperators
Operatorsareusedtoperformoperationsonvariablesandvalues.
Intheexamplebelow, weusethe + operatortoaddtogethertwovalues:
ExampleGetyourownJavaServer
intx = 100 + 50;
Althoughthe + operatorisoftenusedtoaddtogethertwovalues, likeintheexampleabove, itcanalsobeusedtoaddtogetheravariableandavalue, oravariableandanothervariable:
Example
intsum1 = 100 + 50; // 150 (100 + 50)
intsum2 = sum1 + 250; // 400 (150 + 250)
intsum3 = sum2 + sum2; // 800 (400 + 400)
Javadividestheoperatorsintothefollowinggroups:
Arithmeticoperators
Assignmentoperators
Comparisonoperators
Logicaloperators
Bitwiseoperators
ArithmeticOperators
Arithmeticoperatorsareusedtoperformcommonmathematicaloperations.
OperatorNameDescriptionExampleTryit
+ AdditionAddstogethertwovaluesx + y
- SubtractionSubtractsonevaluefromanotherx - y
* MultiplicationMultipliestwovaluesx * y
/ DivisionDividesonevaluebyanotherx / y
% ModulusReturnsthedivisionremainderx % y
++ IncrementIncreasesthevalueofavariableby1 ++x
-- DecrementDecreasesthevalueofavariableby1 --x
JavaAssignmentOperators
Assignmentoperatorsareusedtoassignvaluestovariables.
Intheexamplebelow, weusetheassignmentoperator (=) toassignthevalue10toavariablecalledx:
Example
intx = 10;
Theadditionassignmentoperator (+=) addsavaluetoavariable:
Example
intx = 10;
x += 5;
Alistofallassignmentoperators:
OperatorExampleSameAsTryit
= x = 5x = 5
+= x += 3x = x + 3
-= x -= 3x = x - 3
*= x *= 3x = x * 3
/= x /= 3x = x / 3
%= x %= 3x = x % 3
&= x &= 3x = x & 3
|= x |= 3x = x | 3
^= x ^= 3x = x ^ 3
>>= x >>= 3x = x >> 3
<<= x <<= 3x = x << 3
JavaComparisonOperators
Comparisonoperatorsareusedtocomparetwovalues (orvariables). Thisisimportantinprogramming, becauseithelpsustofindanswersandmakedecisions.
Thereturnvalueofacomparisoniseithertrueorfalse. ThesevaluesareknownasBooleanvalues, andyouwilllearnmoreaboutthemintheBooleansandIf..Elsechapter.
Inthefollowingexample, weusethegreaterthanoperator (>) tofindoutif5isgreaterthan3:
Example
intx = 5;
inty = 3;
System.out.println(x > y); // returns true, because 5 is higher than 3
OperatorNameExampleTryit
== Equaltox == y
!= Notequalx != y
> Greaterthanx > y
< Lessthanx < y
>= Greaterthanorequaltox >= y
<= Lessthanorequaltox <= y
JavaLogicalOperators
Youcanalsotestfortrueorfalsevalueswithlogicaloperators.
Logicaloperatorsareusedtodeterminethelogicbetweenvariablesorvalues:
OperatorNameDescriptionExampleTryit
&& LogicalandReturnstrueifbothstatementsaretruex < 5 && x < 10
|| LogicalorReturnstrueifoneofthestatementsistruex < 5 || x < 4
! LogicalnotReversetheresult, returnsfalseiftheresultistrue !(x < 5 && x < 10)