Sunteți pe pagina 1din 7

Alistair C.

Rooney 2002 All rights reserved

Java for ABAP programmers Lesson 6


TM

Lesson 6 The Java Operators


These can be broken down into several different groups: Arithmetic Operators. The most important thing to remember about Arithmetic operators is their order of precedence. Let me give you an example: 2 + 3 * 5 + 4 = .? If you said 21 you are correct. If you said 54 you made the mistake of adding the numbers before multiplying them. At school we learnt about BODMAS, which stands for Brackets, Of, Division, Multiplication, addition and subtraction. Notice how add and subtract are the last things you do? Remember Alistairs golden rule and youll be OK. If in doubt, use brackets! So the above equation then, would be better expressed: 2 + (3 * 5) + 4 = 21 So, what are the maths operators in Java? + to Add - to Subtract * to Multiply / to Divide % to find the Modulus. Be careful of the minus sign! This can also be used to denote a negative number. This is known as a unary operator (only has one operand). The next set of operators in Java are usually used when testing the relationship between two variables. These are called:

Alistair C. Rooney 2002 All rights reserved ABAP is the registered trademark of SAP AG. Java is the registered trademark of Sun Microsystems Inc.

Alistair C. Rooney 2002 All rights reserved

Relational Operators Big Point no. 1. The result of these operations will always return a true or false Boolean value. Big Point no. 2. The equals operator is = = (double equals sign) not just = as in ABAP. OK then. Lets have a sail through these. You should be familiar with just about all of them already. Less than .<. Greater than.<. Equal to..= =. Not equal to!=. Less than or equal to..<=. Greater than or equal to..>=. Example code: if (appleCount >= bananaCount) { banana.eat(); } Well discuss these in more detail when we get to Control Flow in Java. Right then any questions? Yes you at the back. No I do not shave my head! Lets move on shall we? Increment operators These are what I call Shorthand operators. Programmers Joke Warning: Not really that funny. Java is the result of the operation C++, since C is only incremented after the operation. No, I havent lost it completely. This is to explain the increment and decrement operators in Java. The code x = x+1 can be shortened to x++. By the same token y = y-1 can be shortened to y--.

Alistair C. Rooney 2002 All rights reserved ABAP is the registered trademark of SAP AG. Java is the registered trademark of Sun Microsystems Inc.

Alistair C. Rooney 2002 All rights reserved

The code for a loop (well cover loops later) could be: while(x<10) { x++; System.out.println(The value of x is +x); } The loop will continue until x=10. Notice how x increments each time. The output of this will be: The The The The The The The The The The value value value value value value value value value value of of of of of of of of of of x x x x x x x x x x is is is is is is is is is is 0 1 2 3 4 5 6 7 8 9

Get it? Now that you have Ill confuse the issue further. ++X has a slightly different meaning to X++. Notice how the plusses come before the variable name. This tells the interpreter to increment the variable before using it. This means if we had said x = 3 * j++ where j = 5, then x would equal 15. However if we had said x = 3* ++j then x would be 18. In other words j had already incremented to 6 before it was used. Summary ++h will increment h BEFORE use. h++ will increment h AFTER use. Now look at the joke at the top again! Its still not funny. Now lets move on to the next set of operators.

Alistair C. Rooney 2002 All rights reserved ABAP is the registered trademark of SAP AG. Java is the registered trademark of Sun Microsystems Inc.

Alistair C. Rooney 2002 All rights reserved

Logical Operators There are three logical operators. As ABAP programmers you will know these as AND, OR and NOT. In Java these are the && operator, the || operator and the ! operator. The first two are the same as ABAP but the third has a slightly different meaning. It can negate any Boolean expression, and in doing so, it can be very handy. Commonly these are used in if statements and in while statements. An example would be: while(x!=10) { x++; System.out.println(The value of x is +x); } Another example could be: if((a==7)&&(x!=10)) { do something. } The first example resolves to while x is NOT equal to 10. The second one says if a equals 7 AND x is not equal to 10. As an ABAP programmer you should be comfortable with nesting Boolean expressions, so I wont bore you with explanations on these basic principles. However the next section will introduce a new concept, namely bit manipulation. Bitwise Operators There are 8 bits in a byte. So lets look at a number we can use for our exercises. The number 15 is 1111 in Binary or diagrammatically: 0 0 0 0 1 1 1 1

The sign in our example is positive. If the sign was negative the leftmost bit would be a 1.

Alistair C. Rooney 2002 All rights reserved ABAP is the registered trademark of SAP AG. Java is the registered trademark of Sun Microsystems Inc.

Alistair C. Rooney 2002 All rights reserved

Important Note Negative figures are held in twos complement on any computer. Explaining twos complement is not for this course, but feel free to research this if you have the time. http://www.duke.edu/~twf/cps104/twoscomp.html will help you with this. Back to our Logical operators. They are: & to AND two binary numbers (typically used for masking) | to OR two binary numbers (typically used to set bits) ^ to XOR a binary number. ~ to convert a binary to ones complement >> to shift bits right. << to shift bits left. >>> to shift bits right (logical). If we were to AND our example (15) and lets say 5 we could express this in java as: byte a = (byte) 0x0e ; byte b = (byte) 0x05 ; byte c = (byte)a&b ; 0 0 0 Bit7 0 0 0 Bit6 0 0 0 Bit5 // 15 in hex // 5 in hex // the 2 are ANDed resulting in 5 0 0 0 Bit4 1 0 0 Bit3 1 1 1 Bit2 1 0 0 Bit1 1 1 1 Bit0

Remember youre Boolean Logic? Both bits must be 1 before the result can be 1. So we have effectively masked bits 0 and 3. If we were to OR the same values we would have: byte a = (byte) 0x0e ; byte b = (byte) 0x05 ; byte c = (byte)a|b ; 0 0 0 Bit7 0 0 0 Bit6 0 0 0 Bit5 // 15 in hex // 5 in hex // the 2 are ANDed resulting in 15 0 0 0 Bit4 1 0 1 Bit3 1 1 1 Bit2 1 0 1 Bit1 1 1 1 Bit0

Alistair C. Rooney 2002 All rights reserved ABAP is the registered trademark of SAP AG. Java is the registered trademark of Sun Microsystems Inc.

Alistair C. Rooney 2002 All rights reserved

Again Boolean Logic dictates that either bits can be 1 for the result to be 1. We have set bits 0 and 3 in this example. To XOR the bits we use the caret sign ^ . This is very useful when working at a bit level, but rarely used in workaday Java. XOR demands that either of the bits may be 1 but not both, to achieve a result of 1. Shifting operations can also be extremely useful. Lets examine them: The first is the shift right >>. Well use our first binary example (15) and shift the bits 2 to the right. byte a = (byte) 0x0e ; byte c = (byte)(a>>2); 0 0 Bit7 0 0 Bit6 0 0 Bit5 // 15 in hex // Shifted 2 to the right giving 3 0 0 Bit4 1 0 Bit3 1 0 Bit2 1 1 Bit1 1 1 Bit0

Word of Warning: Bit7 is the sign bit. If this was a negative it would be a 1 and each shift would move a 1 into the rightmost position. The example below demonstrates: 1 1 Bit7 0 1 Bit6 0 1 Bit5 0 0 Bit4 1 0 Bit3 1 0 Bit2 1 1 Bit1 1 1 Bit0

Remember that negative numbers are held as twos complement in Java. The above example is not 15. To force the zero to be fed in, regardless of the sign bit, we use >>> operator. Shifting Left is easier as the bits from the right are always filled with zeroes. byte a = (byte) 0x0e ; byte c = (byte)(a<<2); 0 0 Bit7 0 0 Bit6 0 1 Bit5 // 15 in hex // Shifted 2 to the left giving 60 0 1 Bit4 1 1 Bit3 1 1 Bit2 1 0 Bit1 1 0 Bit0

Alistair C. Rooney 2002 All rights reserved ABAP is the registered trademark of SAP AG. Java is the registered trademark of Sun Microsystems Inc.

Alistair C. Rooney 2002 All rights reserved

Well! Thats all there is for operators! Next upBlock Scope.

Block Scope.
Conceptually this is a very simple section. There are a few basic rules to be followed and then were home and dry. A block is any section of code contained in curly brackets {}. Rule number 1: If a variable is defined inside a block, it is not visible to any code outside of that block. In Java we can express this correctly as: { int x = 5; int y = x + 5; System.out.println(x = +x+ and y = +y); } However, the following would produce an error, since the variables are out of scope: { int x = 5; int y = x + 5; } System.out.println(x = +x+ and y = +y); Rule number 2: If a variable is defined outside a block, it is visible to any code inside of that block. We can illustrate that like this: int x = 5; int y = x + 5; { System.out.println(x = +x+ and y = +y); } Easy enough isnt it? In the next chapter were going to explore the wonders of Strings!

Alistair C. Rooney 2002 All rights reserved ABAP is the registered trademark of SAP AG. Java is the registered trademark of Sun Microsystems Inc.

S-ar putea să vă placă și