R Assignment Operators In Java

Two important assignment operators in R are
<-
and
=
. Lets look at some examples.

This first example assignes 3 to variable x. Notice that a space before and after the assignment operator makes the code more readable.

It is also possible to chain assignments

In this next example we declare a function called mysum.

We could also have used “=” for assignment

Difference between = and <-

However, there is a difference between the two operators. = is only allowed at the top level i.e. if the complete expression is written at the prompt. so = is not allowed in control structures. Here’s an example

In the example above we tried to set aa to 0 using the equals operator but that did not work, however setting bb to 0 using <-works. Note that in real life no programmer would really have to assign bb this way, so this example is for demonstration only.

There is another difference too. look at this example. <- opearator makes the variables available in the
current scope.

also note that this is legal

whereas this does not work

Other operators in R are :

  • “-&gt” : This operator is similar to the <- operator, however it assigns the value on the left to the variable on the right.

    >3-&gt;a

    >a

    [1]3

  • “<<-” and “->>”: This operator assigns the variable to the parent scope. It first looks for the variable in the parent scope and if it can’t find one then it creates a new one in the parent scope. The operator is mainly used inside functions.

    In Line 1 we assign 1 to x. In the function we assign 3 to x and 4 to y using the <<- operator but assign 5 to z using the <- operator. After function execution x is reassigned a new value and y is assigned a value but z is not.

    >x&lt;-1

    >myfun&lt;-function(){

    +  x&lt;&lt;-3

    +  y&lt;&lt;-4

    +  z&lt;-5

    +  return(x+y+z)

    +}

    >print(myfun())

    [1]12

    >x

    [1]3

    >y

    [1]4

    >z

    Error:object'z'notfound

>x&lt;-3

>x

[1]3

>

>a&lt;-b&lt;-3

>a

[1]3

>b

[1]3

1

mysum&lt;-function(a,b){return(a+b)}

x=3

mysum=function(a,b){return(a+b)}

>if(aa=0){print("test")}

Error:unexpected'='in"if (aa="

>aa

Error:object'aa'notfound

>if(bb&lt;-0){print("test")}

>bb

[1]0

1

mysum=function(a=2,b=3){return(a+b)}

1

mysum=function(a&lt;-2,b&lt;-3){return(a+b)}

Operators are special symbols (characters) that carry out operations on operands (variables and values). For example, is an operator that performs addition.

In Java variables article, you learned to declare variables and assign values to variables. Now, you will learn to use operators to manipulate variables.


Assignment Operator

Assignment operators are used in Java to assign values to variables. For example,

int age; age = 5;

The assignment operator assigns the value on its right to the variable on its left. Here, is assigned to the variable using = operator.

There are other assignment operators too. However, to keep things simple, we will learn other assignment operators later in this article.


Example 1: Assignment Operator

When you run the program, the output will be:

5 5

Arithmetic Operators

Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication etc.

OperatorMeaning
+Addition (also used for string concatenation)
-Subtraction Operator
*Multiplication Operator
/Division Operator
%Remainder Operator

Example 2: Arithmetic Operator

When you run the program, the output will be:

In above example, all operands used are variables. However, it's not necessary at all. Operands used in arithmetic operators can be literals as well. For example,

result = number1 + 5.2; result = 2.3 + 4.5; number2 = number1 -2.9;

The operator can also be used to concatenate two or more strings.


Example 3: Arithmetic Operator

When you run the program, the output will be:


Unary Operators

Unary operator performs operation on only one operand.

OperatorMeaning
+Unary plus (not necessary to use since numbers are positive without using it)
-Unary minus; inverts the sign of an expression
++Increment operator; increments value by 1
--decrement operator; decrements value by 1
!Logical complement operator; inverts the value of a boolean

Example 4: Unary Operator

When you run the program, the output will be:


You can also use and -- operator as both prefix and postfix in Java. The operator increases value by 1 and operator decreases value by 1.

int myInt = 5; ++myInt // myInt becomes 6 myInt++ // myInt becomes 7 --myInt // myInt becomes 6 myInt-- // myInt becomes 5

Simple enough till now. However, there is a crucial difference while using increment and decrement operator as prefix and postfix. Consider this example,


Example 5: Unary Operator

When you run the program, the output will be:

When statement is executed, the original value is evaluated first. The is increased only after that. That's why you are getting as an output. Then, when is executed, the increased value 6.2 is displayed.

However, when is executed, is increased by 1 first before it's printed on the screen.

Similar is the case for decrement operator.


Equality and Relational Operators

The equality and relational operators determines the relationship between two operands. It checks if an operand is greater than, less than, equal to, not equal to and so on. Depending on the relationship, it results to either or .

OperatorDescriptionExample
==equal to5 == 3 is evaluated to
!=not equal to5 != 3 is evaluated to
>greater than5 > 3 is evaluated to
<less than5 < 3 is evaluated to
>=greater than or equal to5 >= 5 is evaluated to
<=less then or equal to5 <= 5 is evaluated to

Equality and relational operators are used in decision making and loops (which will be discussed later). For now, check this simple example.


Example 6: Equality and Relational Operators

When you run the program, the output will be:

Here, we have used operator to check if number1 is greater than number2 or not.

Since, is greater than , the expression > is evaluated to .

Hence, the block of code inside is executed and the block of code inside is skipped.

If you didn't understand the above code, don't worry. You will learn it in detail in Java if...else article.

For now, just remember that the equality and relational operators compares two operands and is evaluated to either or .


In addition to relational operators, there is also a type comparison operator which compares an object to a specified type. For example,


instanceof Operator

Here's an example of instanceof operator.

When you run the program, the output will be . It's because is the instance of class.

You will learn more about operator works once you understand Java Classes and Objects.


Logical Operators

The logical operators (conditional-OR) and (conditional-AND) operates on boolean expressions. Here's how they work.

OperatorDescriptionExample
||conditional-OR; if either of the boolean expression is is evaluated to
&&conditional-AND; true if all boolean expressions are is evaluated to false

Example 8: Logical Operators

When you run the program, the output will be:

Logical operators are used in decision making and looping.


Ternary Operator

The conditional operator or ternary operator is shorthand for  statement. The syntax of conditional operator is:

variable = Expression ? expression1 : expression2

Here's how it works.

  • If the is , is assigned to .
  • If the is , is assigned to .

Example 9: Ternary Operator

When you run the program, the output will be:

To learn more, visit Java ternary operator.


Bitwise and Bit Shift Operators

To perform bitwise and bit shift operators in Java, these operators are used.

OperatorDescription
~Bitwise Complement
<<Left Shift
>>Right Shift
>>>Unsigned Right Shift
&Bitwise AND
^Bitwise exclusive OR
|Bitwise inclusive OR

These operators are not commonly used. Visit this page to learn more about bitwise and bit shift operators.


More Assignment Operators

We have only discussed about one assignment operator  in the beginning of the article. Except this operator, there are quite a few assignment operators that helps us to write cleaner code.

OperatorExampleEquivalent to
+=x += 5x = x + 5
-=x -= 5x = x - 5
*=x *= 5x = x * 5
/=x /= 5x = x / 5
%=x %= 5x = x / 5
<<=x <<= 5x = x << 5
>>=x >>= 5x = x >> 5
&=x &= 5x = x & 5
^=x ^= 5x = x ^ 5
|=x |= 5x = x | 5

Now you know about Java operators, it's time to learn precedence of Java operators; the order in which the operators in an expression are evaluated when two operators share a common operand.

0 comments

Leave a Reply

Your email address will not be published. Required fields are marked *