Sunday, December 14, 2014

Assignment, Arithmetic, and Unary Operators

The easy Assignment Agent


One of the most extremely common operators you will encounter will be the simple project operator "=". You saw this operator within the Bicycle course; it assigns the significance on its to the operand with its still left:

int cadence = 0;
int rate = 0;
int equipment = 1;

This operator can also be used on items to assign object personal references, as discussed in Generating Objects.

The particular Arithmetic Operators


The Java programming language provides operators that execute addition, subtraction, multiplication, and division. There exists a good opportunity you'll understand them by their counterparts with basic math. The simply symbol which may look new at all to you can be "%", which splits one operand by another and returns the rest as it is result.
The particular Arithmetic Operators


This program, ArithmeticDemo, tests the arithmetic operators.


course ArithmeticDemo {

    public static void main (String[] args) {

        int result = 1 + 2;
        // result is now 3
        System.out.println("1 + 2 = " + result);
        int original_result = result;

        result = result - 1;
        // result is now 2
        System.out.println(original_result + " - 1 = " + result);
        original_result = result;

        result = result * 2;
        // result is now 4
        System.out.println(original_result + " * 2 = " + result);
        original_result = result;

        result = result / 2;
        // result is now 2
        System.out.println(original_result + " / 2 = " + result);
        original_result = result;

        result = result + 8;
        // result is now 10
        System.out.println(original_result + " + 8 = " + result);
        original_result = result;

        result = result % 7;
        // result is now 3
        System.out.println(original_result + " % 7 = " + result);
    }
}

The program prints the next:

1 + a couple of = 3
3 -- 1 = a couple of
2 * a couple of = some
4 / 2 = a couple of
2 + 8 = 10
10 percent 7 = 3

Also you can combine the arithmetic operators while using the simple project operator to make compound assignments. For example, x+=1; and x=x+1; both increment the significance of times by 1.

The + operator can also be used for concatenating (joining) two strings with each other, as shown within the following ConcatDemo plan:


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);
    }
}
Because of the end in this program, the variable thirdString has "This is really a concatenated sequence. ", that gets branded to common output.

The particular Unary Operators

The particular Unary Operators

The unary operators require only one operand; they execute various operations including incrementing/decrementing any value by one, negating a manifestation, or inverting the significance of any boolean.

This program, UnaryDemo, tests the 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
        System.out.println(result);

        result = -result;
        // result is now -1
        System.out.println(result);

        boolean success = false;
        // false
        System.out.println(success);
        // true
        System.out.println(!success);
    }
}
The increment/decrement operators could be applied before (prefix) as well as after (postfix) the operand. The particular code result++; and ++result; will both end in result getting incremented by one. The only difference is the prefix type (++result) evaluates towards the incremented benefit, whereas the postfix type (result++) evaluates towards the original benefit. If you happen to be just performing a straightforward increment/decrement, this doesn't happen really subject which version you select. But if you use this operator partly of an increased expression, one which you choose could make a significant difference.

The following program, PrePostDemo, shows the prefix/postfix unary increment driver:


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

No comments:

Post a Comment