Difference between revisions of "Operators"
From Suhrid.net Wiki
Jump to navigationJump to searchLine 17: | Line 17: | ||
== Relational Operators == | == Relational Operators == | ||
+ | * Can be applied to '''ANY''' combination of integers, floats or '''chars'''. | ||
+ | * int's can be compared to doubles | ||
+ | * char's can be compared to int's | ||
+ | <syntaxhighlight lang="java5"> | ||
+ | |||
+ | int i = 4; | ||
+ | double pi = 3.14; | ||
+ | |||
+ | if( i > pi) { | ||
+ | System.out.println("Greater than pi"); | ||
+ | } | ||
+ | |||
+ | char c = 'C'; | ||
+ | |||
+ | if( c > 66) { | ||
+ | System.out.println("C > B"); | ||
+ | } | ||
+ | |||
+ | |||
+ | </syntaxhighlight> | ||
+ | |||
[[Category:OCPJP]] | [[Category:OCPJP]] |
Revision as of 03:26, 31 August 2011
Compound Operators
- +=, -=, *= and /=
- Makes code compact
- Operator precedence for compound assignments : The expression on the right side of the = will always be evaluated first!
int x = 2;
x *= 3 + 5;
//This is equivalent to x = x * (3 + 5) and NOT x = (x*2) + 5.
Relational Operators
- Can be applied to ANY combination of integers, floats or chars.
- int's can be compared to doubles
- char's can be compared to int's
int i = 4;
double pi = 3.14;
if( i > pi) {
System.out.println("Greater than pi");
}
char c = 'C';
if( c > 66) {
System.out.println("C > B");
}