java for abap programmers 6

Upload: aaron-villar

Post on 03-Apr-2018

234 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/28/2019 Java for ABAP Programmers 6

    1/7

    Alistair C. Rooney 2002 All rights reserved

    JavaTM for ABAP programmers Lesson 6

    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 theirorder 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 thenumbers 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 betweentwo variables. These are called:

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

  • 7/28/2019 Java for ABAP Programmers 6

    2/7

    Alistair C. Rooney 2002 All rights reserved

    Relational Operators

    Big Point no. 1. The result of these operations will always return a true orfalseBoolean value.

    Big Point no. 2. The equals operator is = = (double equals sign) notjust = as in ABAP.

    OK then. Lets have a sail through these. You should be familiar with just about all of

    them already.

    Less than .

  • 7/28/2019 Java for ABAP Programmers 6

    3/7

    Alistair C. Rooney 2002 All rights reserved

    The code for a loop (well cover loops later) could be:

    while(x

  • 7/28/2019 Java for ABAP Programmers 6

    4/7

    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 cannegate 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 reservedABAP is the registered trademark of SAP AG.Java is the registered trademark of Sun Microsystems Inc.

  • 7/28/2019 Java for ABAP Programmers 6

    5/7

    Alistair C. Rooney 2002 All rights reserved

    Important Note

    Negative figures are held in twos complement on any computer. Explaining twoscomplement 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 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; // 15 in hexbyte b = (byte) 0x05; // 5 in hexbyte c = (byte)a&b ; // the 2 are ANDed resulting in 5

    0 0 0 0 1 1 1 1

    0 0 0 0 0 1 0 1

    0 0 0 0 0 1 0 1

    Bit7 Bit6 Bit5 Bit4 Bit3 Bit2 Bit1 Bit0

    Remember youre Boolean Logic? Both bits must be 1 before the result can be 1. So wehave effectively maskedbits 0 and 3.

    If we were to OR the same values we would have:

    byte a = (byte) 0x0e; // 15 in hexbyte b = (byte) 0x05; // 5 in hexbyte c = (byte)a|b ; // the 2 are ANDed resulting in 15

    0 0 0 0 1 1 1 1

    0 0 0 0 0 1 0 1

    0 0 0 0 1 1 1 1

    Bit7 Bit6 Bit5 Bit4 Bit3 Bit2 Bit1 Bit0

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

    http://www.duke.edu/~twf/cps104/twoscomp.htmlhttp://www.duke.edu/~twf/cps104/twoscomp.html
  • 7/28/2019 Java for ABAP Programmers 6

    6/7

    Alistair C. Rooney 2002 All rights reserved

    Again Boolean Logic dictates that either bits can be 1 for the result to be 1.

    We havesetbits 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 2to the right.

    byte a = (byte) 0x0e; // 15 in hexbyte c = (byte)(a>>2); // Shifted 2 to the right giving 3

    0 0 0 0 1 1 1 1

    0 0 0 0 0 0 1 1

    Bit7 Bit6 Bit5 Bit4 Bit3 Bit2 Bit1 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 0 0 0 1 1 1 1

    1 1 1 0 0 0 1 1

    Bit7 Bit6 Bit5 Bit4 Bit3 Bit2 Bit1 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; // 15 in hexbyte c = (byte)(a

  • 7/28/2019 Java for ABAP Programmers 6

    7/7

    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 reservedABAP is the registered trademark of SAP AG.Java is the registered trademark of Sun Microsystems Inc.