Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is <-- in Java? [duplicate]

Tags:

java

operators

I came across below snippet. It outputs to 4 3 2 1

I never came across <-- in Java.

Is <-- an operator that makes the value of var1 to var2?

public class Test {      public static void main(String[] args) {          int var1 = 5, var2 = 0;         while (var2 <-- var1) {             System.out.print(" " + var1);         }     } } 
like image 797
Snehal Masne Avatar asked Sep 08 '14 10:09

Snehal Masne


People also ask

What is --> operator in Java?

The Java right shift operator >> is used to move the value of the left operand to right by the number of bits specified by the right operand.

What does * indicate in Java?

It is called a 'NOT' operator. It can be used to convert false to true or vice versa. By using this operator, the logical state of an operand is reversed. In simple words, it inverts the value of a boolean. Read Also: What does \n and \t mean in Java.

What is 2L in Java?

When you place 2L in code, that is a long literal, so the multiplications promote the other int s to long before multiplication, making your calculations correct by preventing overflow. The basic rules here to know here are: Java has operator precedence.


2 Answers

<-- is not a new Java operator (even though it may look like it), but there are 2 normal operators: < and --

while (var2 <-- var1) is the same as while(var2 < (--var1)), which can be translated to plain english as:

  1. decrement the var1 variable ( --var is a prefix decrementation, ie. decrement the variable before condition validation)
  2. Validate the condition var2 < var1
like image 125
Daniel Avatar answered Sep 23 '22 07:09

Daniel


<-- There is no such operator in java.

It is var2 < (--var1) A relational + decrement operator.

like image 32
Not a bug Avatar answered Sep 20 '22 07:09

Not a bug