Home > DeveloperSection > Blogs > Assignment of Operators

Assignment of Operators

Java Java 
5 Comment(s)
 1053  View(s)
Rate this:

The Simple Assignment Operator

One of the most common operators that you'll encounter is the simple assignment operator "=". You saw this operator in the Bicycle class; it assigns the value on its right to the operand on its left:

 int cadence = 0;
 int speed = 0;
 int gear = 1;

This operator can also be used on objects to assign object references, as discussed in Creating objects..

The Arithmetic Operators

The Java programming language provides operators that perform addition, subtraction, multiplication, and division. There's a good chance you'll recognize them by their counterparts in basic mathematics. The only symbol that might look new to you is "%", which divides one operand by another and returns the remainder as its result.

+Additive operator (also used for String concatenation)
-Subtraction operator
*Multiplication operator
/Division operator
%Remainder operator

The following program, ArithmeticDemo, tests the arithmetic operators.

class 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);

This program prints the following:

1 + 2 = 3
3 - 1 = 2
2 * 2 = 4
4 / 2 = 2
2 + 8 = 10
10 % 7 = 3

You can also combine the arithmetic operators with the simple assignment operator to create compound assignments. For example, x+=1; and x=x+1; both increment the value of x by 1.

The + operator can also be used for concatenating (joining) two strings together, as shown in the following ConcatDemo program:

class ConcatDemo {
    public static void main(String[] args){
        String firstString = "This is";
        String secondString = " a concatenated string.";
        String thirdString = firstString+secondString;

By the end of this program, the variable thirdString contains "This is a concatenated string.", which gets printed to standard output.

The Unary Operators

The unary operators require only one operand; they perform various operations such as incrementing/decrementing a value by one, negating an expression, or inverting the value of a boolean.

+Unary plus operator; indicates positive value (numbers are positive without this, however)
-Unary minus operator; negates an expression
++Increment operator; increments a value by 1
--Decrement operator; decrements a value by 1
!Logical complement operator; inverts the value of a boolean

The following program, UnaryDemo, tests the unary operators:

class UnaryDemo {

    public static void main(String[] args) {

        int result = +1;
        // result is now 1

        // result is now 0

        // result is now 1

        result = -result;
        // result is now -1

        boolean success = false;
        // false
        // true

The increment/decrement operators can be applied before (prefix) or after (postfix) the operand. The code result++; and ++result; will both end in result being incremented by one. The only difference is that the prefix version (++result) evaluates to the incremented value, whereas the postfix version (result++) evaluates to the original value. If you are just performing a simple increment/decrement, it doesn't really matter which version you choose. But if you use this operator in part of a larger expression, the one that you choose may make a significant difference.

The following program, PrePostDemo, illustrates the prefix/postfix unary increment operator:

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

Assignment operator

By Mayank Tripathi on   2 years ago
Assignment operator is "=", people some times get confused with equal sign (=) in mathematics.
For equality java uses "==" double equal sign , so never get confused between assignment operator and equality check

x = 5;
This statement assign 5 to variable x 
x == 5;
Here we check where value of x is equal to 5 or not.

assignmet of operator

By ANkita gupta on   2 years ago
agreed, thanks.

assignment of operator

By ANkita gupta on   2 years ago
in java language. there are comparison operators, and one of them is == which is known for equal purpose .
Will you please clear it more elaborately, whether double sign of equal is used for assignment purpose or for comparison purpose? As i learned in C & C++ that == is used for assignment purpose, but i have little bit doubt on it. 

assignment of operator

By Mayank Tripathi on   2 years ago
== means comparison or equality check
means assignment of value

Just remember this little rule, u will never get confused among these two

assignment of operator

By Mayank Tripathi on   2 years ago
There is also a concept of "===" super equality in some languages like Java Script
But its a very complex notion. But in java we have only
==  for equality and
= for assignment

Don't want to miss updates? Please click the below button!

Follow MindStick