Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java operatoriai www.mif.vu.lt/~tomasa/operatoriai.ppt http://java.sun.com/docs/books/tutorial/java/nutsandbolts/operators.html http://www.mif.vu.lt/~bastys/java/paskaitos/4/4.htm#Operators.

Similar presentations


Presentation on theme: "Java operatoriai www.mif.vu.lt/~tomasa/operatoriai.ppt http://java.sun.com/docs/books/tutorial/java/nutsandbolts/operators.html http://www.mif.vu.lt/~bastys/java/paskaitos/4/4.htm#Operators."— Presentation transcript:

1 Java operatoriai www.mif.vu.lt/~tomasa/operatoriai.ppt

2 Operators precedence Operatoriai postfix expr++ expr-- unary
++expr  --expr  +expr  -expr  ~ ! multiplicative * / % additive + - shift << >> >>> relational < > <= >= instanceof equality == != bitwise AND & bitwise exclusive OR ^ bitwise inclusive OR | logical AND && logical OR || ternary ? : assignment = += -= *= /= %= &= ^= |= <<= >>= >>>=

3 Simple Assignment Operator
int cadence = 0; int speed = 0; int gear = 1; int masyvas []; masyvas = new int[5]; int j = 10; int z = j++%5; // z = 0 int c = 10; int x = ++c%5; // x = 1

4 Arithmetic Operators + additive operator (also used for String concatenation) - subtraction operator * multiplication operator / division operator % remainder operator

5 Arithmetic Operators class ArithmeticDemo { public static void main (String[] args){ int result = 1 + 2; // result is now 3 System.out.println(result); result = result - 1; // result is now 2 // same as: result-=1; // same as: result--; // same as: --result; System.out.println(result); result = result * 2; // result is now 4 // same as: result*=2; System.out.println(result); result = result / 2; // result is now 2 // same as: result/=2; System.out.println(result); result = result + 8; // result is now 10 // same as: result+=8; result = result % 7; // result is now 3 // same as: result%=7; System.out.println(result); } }

6 Arithmetic Operators class ConcatDemo { public static void main(String[] args){ String firstString = "This is"; String secondString = " a concatenated string."; String thirdString = firstString+secondString; System.out.println(thirdString); } Output: "This is a concatenated string.“

7 Unary Operators + Unary plus operator; indicates positive value (numbers are positive without this, however) - Unary minus operator; negates an expression ++ Increment operator; increments a value by 1 -- Decrement operator; decrements a value by 1 ! Logical complement operator; inverts the value of a boolean

8 Unary Operators class UnaryDemo { public static void main(String[] args){ int result = +1; // result is now 1 System.out.println(result); result--; // result is now 0 result++; // result is now 1 result+=1; // result = -result; // result is now -1 boolean success = false; System.out.println(success); // false System.out.println(!success); // true }

9 Unary Operators class PrePostDemo { public static void main(String[] args){ int i = 3; i++; System.out.println(i); // "4" ++i; System.out.println(i); // "5" System.out.println(++i); // "6" System.out.println(i++); // "6" System.out.println(i); // "7" }

10 Equality and Relational Operators
== equal to != not equal to > greater than >= greater than or equal to < less than <= less than or equal to

11 Equality and Relational Operators
class ComparisonDemo { public static void main(String[] args){ int value1 = 1; int value2 = 2; if(value1 == value2) System.out.println("value1 == value2"); if(value1 != value2) System.out.println("value1 != value2"); if(value1 > value2) System.out.println("value1 > value2"); if(value1 < value2) System.out.println("value1 < value2"); if(value1 <= value2) System.out.println("value1 <= value2"); } Output: value1 != value2 value1 < value2 value1 <= value2

12 Conditional Operators
&& Conditional-AND || Conditional-OR Another conditional operator is ?:, which can be thought of as shorthand for an if-then-else statement

13 Conditional Operators
class ConditionalDemo1 { public static void main(String[] args){ int value1 = 1; int value2 = 2; if((value1 == 1) && (value2 == 2)) System.out.println("value1 is 1 AND value2 is 2"); if((value1 == 1) || (value2 == 1)) System.out.println("value1 is 1 OR value2 is 1"); }

14 Conditional Operators
class ConditionalDemo2 { public static void main(String[] args){ int value1 = 1; int value2 = 2; int result; boolean someCondition = true; result = someCondition ? value1 : value2; System.out.println(result); }

15 The Type Comparison Operator instanceof
class InstanceofDemo { public static void main(String[] args) { Parent obj1 = new Parent(); Child obj2 = new Child(); System.out.println("obj1 instanceof Parent: " + (obj1 instanceof Parent)); System.out.println("obj1 instanceof Child: " + (obj1 instanceof Child)); System.out.println("obj1 instanceof MyInterface: " + (obj1 instanceof MyInterface)); System.out.println("obj2 instanceof Parent: " + (obj2 instanceof Parent)); System.out.println("obj2 instanceof Child: " + (obj2 instanceof Child)); System.out.println("obj2 instanceof MyInterface: " + (obj2 instanceof MyInterface)); } class Parent{} class Child extends Parent implements MyInterface{} interface MyInterface{} Output: obj1 instanceof Parent: true obj1 instanceof Child: false obj1 instanceof MyInterface: false obj2 instanceof Parent: true obj2 instanceof Child: true obj2 instanceof MyInterface: true

16 Bitwise and Bit Shift Operators
~ Unary bitwise complement << Signed left shift >> Signed right shift >>> Unsigned right shift & Bitwise AND ^ Bitwise exclusive OR | Bitwise inclusive OR class BitDemo { public static void main(String[] args) { int bitmask = 0x000F; int val = 0x2222; System.out.println(val & bitmask); // prints "2" }

17 Summary of Operators Simple Assignment Operator
Arithmetic Operators + Additive operator (also used for String concatenation) - Subtraction operator * Multiplication operator / Division operator % Remainder operator Unary Operators + Unary plus operator; indicates positive value (numbers are positive without this, however) - Unary minus operator; negates an expression ++ Increment operator; increments a value by Decrement operator; decrements a value by 1 ! Logical compliment operator; inverts the value of a boolean Equality and Relational Operators == Equal to != Not equal to > Greater than >= Greater than or equal to < Less than <= Less than or equal to Conditional Operators && Conditional-AND || Conditional-OR ?: Ternary (shorthand for if-then-else statement) Type Comparison Operator instanceof Compares an object to a specified type Bitwise and Bit Shift Operators ~ Unary bitwise complement << Signed left shift >> Signed right shift >>> Unsigned right shift & Bitwise AND ^ Bitwise exclusive OR | Bitwise inclusive OR


Download ppt "Java operatoriai www.mif.vu.lt/~tomasa/operatoriai.ppt http://java.sun.com/docs/books/tutorial/java/nutsandbolts/operators.html http://www.mif.vu.lt/~bastys/java/paskaitos/4/4.htm#Operators."

Similar presentations


Ads by Google