Broad Network


Comparison and Arithmetic Operators in Java

Java Basics – Part 8

Forward: In this part of the series, I talk about some common Java Operators.

By: Chrysanthus Date Published: 2 Sep 2012

Introduction

This is part 8 of my series, Java Basics. In this part of the series, I talk about some common Java Operators. You have seen the logical operators. You have also seen the assignment (=) and equal (==) operators. In this part we look at comparison and arithmetic operators. Remember, take things the way I give you in these tutorials. Do not in your mind, add or subtract anything to what is in these tutorials, by trying to compare with mathematics and the Human Language. You can only do that after you have completed the tutorials, in order not to be confused.

Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.

Operand
An operand is a variable or a literal (value) associated with an operator. Consider,

    myIdent = 30;  //this ID was defined as an int

myIdent is a left operand and 30 is a right operand. = is the assignment operator, not the equal operator. The equal operator is, == and is used only in conditions.

Consider:

      myIdent && hisIdent && herIdent

There are three operands in the above expression. So, you can talk of the first, second and third operand.

Comparison Operators
A comparison operator compares the operands on its sides and returns a logical value depending on whether the comparison is correct or wrong. If the comparison is correct a logical value of true is returned. If it is wrong, a logical value of false is returned. Another name for Boolean Value is Logical Value, which is either true or false.

The Equal Operator
You have seen this before. It is ==, typed as a double assignment operator. The equal operator returns true if operands on either side are equal, otherwise it returns false. You have seen many examples of this.

The Not Equal Operator
The Not Equal operator is the opposite of the Equal Operator. The Not Equal operator is, != . It returns true if the operands are not equal, otherwise it returns false. Let us look at some examples:

Try the following code:

class NotEql
    {
        public static void main(String[] args)
            {
                int myIdent = 25;
                int hisIdent = 30;
                if (myIdent != hisIdent)
                    {
                        System.out.println("The values of the two objects are not equal.");
                    }
            }
    }

myIdent is 25, hisIdent is 30. The condition is read like this: If myIdent is not equal to hisIdent, then the if-block will be executed. Since the values of the fields (variables) are not equal,  (myIdent != myIdent) returns true.

In the following code, the values of the two objects are equal, so the condition returns false and the if-block is not executed.

class NotEqlFalse
    {
        public static void main(String[] args)
            {
                int myIdent = 50;
                int hisIdent = 50;
                if (myIdent != hisIdent)
                    {
                        System.out.println("The values of the two objects are not equal.");
                    }
            }
    }

Note: The letter O and the digit zero are not the same things. If you type the letter O in place of zero you will not have the right results. The digit zero is found in the number keypad of your keyboard. The letter O is found in the main keyboard area.

The Greater Than Operator
The Greater Than operator is, > . It returns true if the left operand is greater than the right operand. In the following example, the left operand is greater than the right operand. So the if-block is executed:

class GreaterThan
    {
        public static void main(String[] args)
            {
                int ident1 = 60;
                int ident2 = 70;
                if (ident2 > ident1)
                    {
                        System.out.println("The value of ident2 is greater than the value of ident1. ");
                    }
            }
    }

Read and try the code.

Greater Than Or Equal - Operator
The Greater Than or Equal operator is, >= (it is the math greater than sign followed by the math equal sign). It returns true if the left operand is greater than or equal to the right operand.

The Less Than Operator
The Less Than Operator is < .It returns true if the left operand is less than the right operand.

The Less Than or Equal - Operator
The Less than or Equal operator is, <= . It returns true if the left operand is less than or equal to the right operand.

Arithmetic Operators
Remember I said you should avoid making analogy between Java and mathematics (arithmetic). So you should see what I have from this section to the end of this tutorial as an application of Java and not Java in itself. An Arithmetic operator takes one or two numbers as operands (either literals or variables) and returns the answer, similar to what happens in arithmetic, but not exactly, especially when you are dealing with doubles. See what I have below as application of Java, not Java in itself.

The standard arithmetic operators are addition (+), subtraction (-), multiplication (*), and division (/).

Addition Operator
Read and try the following code. The explanation is given below:

class Addition
    {
        public static void main(String[] args)
            {
                    int id1 = 20;
                    int id2 = 30;
        
                    int id3 = id2 + id1;
                    System.out.println(id3);
            }
    }

20 is kept in the field named by the variable, id1. 30 is kept in the field named, id2. In the third statement of the block of the main method, Java takes the content of id2 and adds it to the content of id1, then it puts the result as content for the field of the newly declared variable, id3. This addition is done without affecting or changing the contents of id2 and id1. The addition (rough work) is done in a different area of memory.

Subtraction Operator
Read and try the following code. The explanation is given below:

Code example:

class Subtraction
    {
        public static void main(String[] args)
            {
                int id1 = 20;
                int id2 = 30;
        
                int id3 = id2 - id1;
                System.out.println(id3);
            }
    }

20 is kept in the field named, id1. 30 is kept in the field named, id2. In the third statement of the block of the main method, Java takes the content of id2 and subtracts from it the content of id1, then it puts the result as content for the field of the newly declared variable, id3. This subtraction is done without affecting or changing the contents of id2 and id1. The subtraction (rough work) is done in a different area of memory.

Multiplication Operator
Read and try the following code. The explanation is given below:

class Multiplication
    {
        public static void main(String[] args)
            {
                int id1 = 20;
                int id2 = 30;
        
                int id3 = id2 * id1;
                System.out.println(id3);
            }
    }

Note that the multiplication operator is * and not X. 20 is kept in the field named, id1. 30 is kept in the field named, id2. In the third statement of the block of the main method, Java takes the content of id2 and multiplies it with the content of id1, then it puts the result as content for the field of the newly declared variable, id3. This multiplication is done without affecting or changing the contents of id2 and id1. The multiplication (rough work) is done in a different area of memory.

Division Operator
Read and try the following code. The explanation is given below:
Code example:

class Division
    {
        public static void main(String[] args)
            {
                int id1 = 3;
                int id2 = 15;
    
                int id3 = id2 / id1;
                System.out.println(id3);
            }
    }

Note that the division operator is, / . 3 is kept in the field named, id1. 15 is kept in the field named, id2. In the third statement of the block of the main method, Java takes the content of id2 and divides it by the content of id1, then it puts the result as content for the field of the newly declared variable, id3. This division is done without affecting or changing the contents of id2 and id1. The division (rough work) is done in a different area of memory.

Other operators are the Modulus (%), Increment (++), Decrement (--), and the Negation operators. You have to learn the particular way in which each of these operators behaves.

Modulus Operator
The modulus operator divides the first operand by the second operand and returns the remainder. Read and try the following code:

class Modulus
    {
        public static void main(String[] args)
            {
                int id1 = 17;
                int id2 = 12;
        
                int id3 = id1 % id2;
                System.out.println(id3);
            }
    }

The Modulus operator is the percentage sign. For the third statement in the main method block above, Java takes the content of id1 and divides it by the content of id2. The remainder of the division is put in the field of id3. The division (rough work) is done in a different area of memory.

Increment Operator
The Increment Operator is, ++. It works with one operand, not two as the others. The operand has to be a number. When the operator is placed in front (prefix) of the operand, it behaves in one way. When it is placed after (postfix) the operand, it behaves in another way.

Prefix: When it is prefix, it adds 1 to the operand and returns the incremented operand value. Read and try the following code:

class IncremPr
    {
        public static void main(String[] args)
            {
                int id1 = 10;
                int id2 = ++id1;

                System.out.println(id2);
            }
    }

In the code, initially, 10 is assigned to the field of id1. Then there is a statement. In the statement you have a new variable, id2, the assignment operator and then “++id1”. What interest us here is “++id2”, where the increment operator is in front of the variable. The value the increment operator returns is assigned to the field of id2. If you have tried the code, you would noticed that the value of id2 is 11. This means, if used prefix, it increments the operand and then returns the incremented content of the operand field.

Postfix: When it is postfix, it returns the operand value before adding 1 to it. The returned value is the original value of the operand. The increased value is the new value of the operand, which is not returned. Read and try the following code.

class IncremPo
    {
        public static void main(String[] args)
            {
                int id1 = 10;
                int id2 = id1++;

                System.out.println(id2);
                System.out.println(id1);
            }
    }

If you have tried the above code, you would have noticed that the value for id2 is 10 and the final value for id1 is 11, confirming that the incrementing took place after the value was returned. When it is postfix, you make use of the incremented value in a different statement after the current statement.

Decrement Operator
The Decrement operator, -- , behaves like the increment operator with the only difference that it subtracts 1 instead of adding 1. It works prefix and postfix like the ++ operator.

Negation Operator
This operator is the negative sign, - . It works with one operand (on its right); it negates the operand like in math. Read and try the following:

class Negation
    {
        public static void main(String[] args)
            {
                int id1 = 15;
                int id2 = -id1;

                System.out.println(id2);
            }
    }

Adding Value to a Field
Imagine that you have an int field already having a value. You can add some other value to the field as follows:

    int myField = value;
    myField = myField + newValue;

e.g.

    int myInt =3;
    myInt = myInt + 5;

The new value of myInt is 8.

Try the following code:

class AddToItself
    {
        public static void main(String[] args)
            {
                int myInt =3;
                myInt = myInt + 5;

                System.out.println(myInt);
            }
    }

You can also subtract a new value from the value already in a field in the same way.

We have come to the end of this part of the series. A rather long ride! Rendezvous in the next part.

Chrys
NEXT

Related Articles

Java Course

Comments

Become the Writer's Fan
Send the Writer a Message