1 / 17

Java operatoriai

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

hadlock
Download Presentation

Java operatoriai

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Operators precedence

  3. Simple AssignmentOperator • 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. ArithmeticOperators + additive operator (also used for String concatenation) - subtraction operator * multiplication operator / division operator % remainder operator

  5. ArithmeticOperators class ArithmeticDemo { public static void main (String[] args){ int result = 1 + 2; // result is now 3System.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. ArithmeticOperators 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 System.out.println(result); result++; // result is now 1 result+=1; // System.out.println(result); result = -result; // result is now -1 System.out.println(result); boolean success = false; System.out.println(success); // false System.out.println(!success); // true } }

  9. Unary Operators class PrePostDemo { public static void main(String[] args){ inti = 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 anif-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; booleansomeCondition = 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 instanceofMyInterface: " + (obj1 instanceofMyInterface)); System.out.println("obj2 instanceof Parent: " + (obj2 instanceof Parent)); System.out.println("obj2 instanceof Child: " + (obj2 instanceof Child)); System.out.println("obj2 instanceofMyInterface: " + (obj2 instanceofMyInterface)); } } class Parent{} class Child extends Parent implements MyInterface{} interface MyInterface{} Output: obj1 instanceof Parent: true obj1 instanceof Child: false obj1 instanceofMyInterface: false obj2 instanceof Parent: true obj2 instanceof Child: true obj2 instanceofMyInterface: 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; intval = 0x2222; System.out.println(val & bitmask); // prints "2" } }

  17. Summary of Operators • Simple Assignment Operator = 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 1 -- 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 instanceofCompares 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

More Related