java:operators
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
java:operators [2017/02/14 09:13] – [Τελεστής instanceof] gthanos | java:operators [2021/02/28 16:38] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 56: | Line 56: | ||
| %%--%% | | %%--%% | ||
- | Οι τελεστές αύξησης ή μείωσης της τιμής κατά 1 (++, | + | Οι τελεστές αύξησης ή μείωσης της τιμής κατά 1 (++, |
<code java PrePostDemo.java> | <code java PrePostDemo.java> | ||
- | class PrePostDemo | + | class PreAndPostIncrementDemo |
public static void main(String[] args){ | public static void main(String[] args){ | ||
int i = 3; | int i = 3; | ||
Line 68: | Line 68: | ||
// prints 5 | // prints 5 | ||
System.out.println(i); | System.out.println(i); | ||
- | // prints 6 | + | // prints |
System.out.println(++i); | System.out.println(++i); | ||
- | // prints 6 | + | // prints 6, i=7 |
System.out.println(i++); | System.out.println(i++); | ||
// prints 7 | // prints 7 | ||
System.out.println(i); | System.out.println(i); | ||
+ | // prints 7, i=6 | ||
+ | System.out.println(i--); | ||
+ | // prints 5, i=5 | ||
+ | System.out.println(--i); | ||
} | } | ||
} | } | ||
Line 88: | Line 92: | ||
| %%< | | %%< | ||
+ | <code java ComparisonOperators.java> | ||
+ | |||
+ | public class ComparisonOperators { | ||
+ | public static void main(String []args) { | ||
+ | int a = 5; | ||
+ | int b = 8; | ||
+ | if(a>=5) { | ||
+ | System.out.println(" | ||
+ | } | ||
+ | if(b<9) { | ||
+ | System.out.println(" | ||
+ | } | ||
+ | if(b==8) { | ||
+ | System.out.println(" | ||
+ | } | ||
+ | if(a!=6) { | ||
+ | System.out.println(" | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </ | ||
===== Λογικοί τελεστές ===== | ===== Λογικοί τελεστές ===== | ||
Line 98: | Line 123: | ||
Αξίζει να προσέξετε τον **ternary operator** που αποτελεί σύντμηση μίας δήλωσης '' | Αξίζει να προσέξετε τον **ternary operator** που αποτελεί σύντμηση μίας δήλωσης '' | ||
- | <code java ConditionalDemo1.java> | + | <code java LogicalOperatorsDemo.java> |
- | class ConditionalDemo1 | + | class LogicalOperatorsDemo |
- | | + | |
- | int value1 | + | |
- | int value2 | + | |
- | if((value1 | + | int b = 2; |
- | System.out.println(" | + | int c = 3; |
- | if((value1 | + | |
- | System.out.println(" | + | System.out.println(" |
+ | } | ||
+ | | ||
+ | System.out.println("a > 0 OR b == 1"); | ||
+ | } | ||
+ | if(!(a != 1) && !(b != 1)) { | ||
+ | System.out.println(" | ||
+ | } | ||
+ | if( a>0 && b>0 && c>0) { | ||
+ | System.out.println(" | ||
+ | } | ||
+ | if( a<0 || b<0 || c>0) { | ||
+ | System.out.println(" | ||
} | } | ||
+ | | ||
+ | } | ||
} | } | ||
+ | |||
</ | </ | ||
- | <code java ConditionalDemo2.java> | + | <code java TernaryOperatorDemo.java> |
- | class ConditionalDemo2 | + | class TernaryOperatorDemo |
- | | + | |
- | int value1 | + | int a = 1; |
- | int value2 | + | int b = 2; |
- | int result; | + | int result; |
- | | + | |
- | result = someCondition | + | result = a >= 1 ? ++a : ++b; |
- | | + | |
- | } | + | } |
} | } | ||
</ | </ | ||
Line 138: | Line 178: | ||
| %%^%% | δυαδικό αποκλειστικό OR (exclusive OR) | | | %%^%% | δυαδικό αποκλειστικό OR (exclusive OR) | | ||
- | <code java BitDemo.java> | + | <code java BitOperatorsDemo.java> |
- | class BitDemo | + | class BitOperatorsDemo |
- | public static void main(String[] args) { | + | public static void main(String[] args) { |
- | int bitmask = 0x000F; | + | int bitmask = 0x000F; |
- | int val = 0x2222; | + | int val = 0x4444; |
- | // prints | + | |
- | System.out.println(" | + | |
- | System.out.println(" | + | System.out.println(" |
- | System.out.println(" | + | val = val & bitmask; |
- | System.out.println(" | + | // prints 16 |
- | } | + | |
+ | // prints 2 | ||
+ | | ||
+ | // prints 24 | ||
+ | | ||
+ | } | ||
} | } | ||
</ | </ | ||
+ | ===== Τελεστές Ανάθεσης ===== | ||
- | ===== Τελεστές ανάθεσης ===== | + | ^ τελεστής |
- | + | ||
- | + | ||
- | ^ τελεστής ^ επεξήγηση ^ παράδειγμα ^ | + | |
| = | απλός τελεστής ανάθεσης | | = | απλός τελεστής ανάθεσης | ||
| += | πρόσθεση και ανάθεση | | += | πρόσθεση και ανάθεση | ||
Line 168: | Line 211: | ||
| %%^%%= | | %%^%%= | ||
+ | <code java AssignmentOperators.java> | ||
+ | class AssignmentOperatorDemo { | ||
+ | public static void main(String []args) { | ||
+ | int a = 16; | ||
+ | a += 16; | ||
+ | System.out.println(" | ||
+ | a *= 2; | ||
+ | System.out.println(" | ||
+ | a /= 4; | ||
+ | System.out.println(" | ||
+ | a <<= 1; | ||
+ | System.out.println(" | ||
+ | a >>= 2; | ||
+ | System.out.println(" | ||
+ | a %= 7; | ||
+ | System.out.println(" | ||
+ | } | ||
+ | } | ||
+ | </ | ||
- | + | | Προηγούμενο: | |
- | | Προηγούμενο: | + | |
java/operators.1487063582.txt.gz · Last modified: 2017/02/14 09:13 by gthanos