1 / 30

Java Operators

Java Operators. Being a Smooth Operator. CS 102-02 Lecture 2-1. Operating with Java. Most programming languages have operators Operators are short-hand symbols for actions = Assign right to left + Add two numbers (or concatenate two strings) Operators in Java have fixed meaning

phyllislew
Download Presentation

Java Operators

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. Java Operators Being a Smooth Operator CS 102-02 Lecture 2-1

  2. Operating with Java • Most programming languages have operators • Operators are short-hand symbols for actions = Assign right to left + Add two numbers (or concatenate two strings) • Operators in Java have fixed meaning • No operator overloading • Can’t say: List = List + Item; // Add item to list

  3. Kinds of Operators

  4. Operator Precedence • Usually things go left-to-right, but there are precedence rules • Nutshell reading lists operators by precedence • Override precedence with ()’s

  5. Arithmetic Operators • The usual suspects: plus, minus, blah, blah, blah • Modulo/remainder operator

  6. Modulo Operator • Modulo (or remainder) operator: what’s left over after division 7%3 = 1 198%3 = ?? 6.0%4.0 = 2 • Is it odd or even? • Looping with clock arithmetic • Appointment at 5pm everyday • Baking 217 cakes: step 3 of 7 same as 24 of 28

  7. Short-Hand Operators • Increment and decrement: ++ and -- • Often need to add or subtract 1 • Pre: Add (subtract) first • Post: Add (subtract) afterwards • Compiler can sometimes optimize

  8. Testing Out Short-Hand • Suppose we start with: • X = 7; • Y = 9; What’s the difference between: X++; ++X;

  9. Are You My Type? • What’s the type of a result? Expression Result type int * int int float * float ?? int * float ?? int / int ?? • Conversion & promotion

  10. Assignment Operators • Change the value on the left to the value of the expression on the right If you want to: Try: Assign 8 to Y Y = 8; Add 1 to Y Y++; Assign Y+10 to Y X += 10;

  11. Works for Strings Too • Strings are “added” (concatenated) with + What is Name after the third line? Name = “Simpson”; First = “Lisa”; Name += First; What’s the result here? Age = 11; Message = “He’s “ + Age + “ years old.”;

  12. Conditional Operator • Instead of If..Then..Else, use ?: • Takes three arguments in the form: Boolean condition? If-true : If-false If (Simpson == “Lisa”) { Message = “She’s our favorite!”; } else { Message= “Doh!”; } System.out.println(Message); is the same as…

  13. Using the Conditional Operator System.out.println(Simpson==“Lisa” ? ”She’s our favorite” :“Doh!”); (The above should be on one line in a real program)

  14. And, But and Or will get you pretty far.. • Logical operators combine simple expressions to form complex ones • Boolean logic

  15. Boolean Types • True or false are real values in Java • Some languages just use 0 and not 0 if (y = 7) then … • In Java result of a comparison is Boolean 8 != 9 ?? 8 != 8 ??

  16. Logical Operators in Java • Translating logic into Java AND && OR || XOR ^ NOT !

  17. Boolean Expressions • De Morgan’s Laws with Expressions One & Two One OR Two == One AND Two One AND Two == One OR Two • Some handy relations One XOR One == False One OR One == True

  18. Short-Circuit • Remember: False AND Anything == False True OR Anything == True • Sometimes compiler can short-circuit and skip evaluation of second expression • What if there are side effects?

  19. Sideline on Side Effects • Side effects are results of expression evaluation other than the expression’s value • Examples X++; • Output: System.out.println(“Howdy!”);

  20. Short-Circuiting Side Effects • Short-circuiting could prevent a side effect • How do you force the compiler to evaluate a second expression?

  21. No Short-Circuit Here • Guarantee that the second expression is evaluated AND & OR | XOR ^ (Why is ^ listed here?)

  22. Relational Operators • Determine the relationship between values • Equality & inequality • Less than, greater than

  23. (In)Equality • Equality is different from assignment == != = • Most keyboards just have = • Use == for equality • And != for inequality

  24. Bitwise Operators • Computers are binary creatures: everything’s on or off • For example, computers can’t store decimal numbers so

  25. Binary Arithmetic • Everything’s in powers of two • Turn 78 into:

  26. Accentuate the positive • Computers don’t know about negative numbers • Use the first (leftmost) bit as a sign bit: 1 if negative: -5 is 11111101 0 if positive: +5 is 00000011

  27. Bitwise is Binary • Work with the bits inside the values • Only good for integral values (integer numbers, bytes and characters)

  28. And Shift Your Bits ‘Round and ‘Round • Bitwise AND of 78 and 34

  29. Why Bother with Bitwise? • Use numbers not for themselves but for their internal representations • Example: A tic-tac-toe grid might have 0’s for O’s and 1’s for X’s • Just need 9 bits to do the whole table and only 27 bits for 3-D tic-tac-toe

  30. That’s It for Operators • Operators are key to building large expressions in Java • Know operator precedence (or at least where to look it up) • Next time: Use operators to build expressions for control structures

More Related