38

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);
        }
    }
}
Snehal Masne
  • 3,403
  • 3
  • 31
  • 51
  • 2
    it's var2 < (--var1), where -- is prefixed decrementation – DeiAndrei Sep 08 '14 at 11:00
  • 5
    What truly awful, misleading code. Did the person writing it actively dislike the people who would have to maintain it? *(Good question, btw.)* – T.J. Crowder Sep 08 '14 at 11:02
  • Upvoting indicates _This question shows research effort[...]_ except this question doesn't show that in my opinion. The primary source for this information should be the Java language specification. Also, a search here on SO would likely have found the duplicates. So `-1` – jpw Sep 08 '14 at 11:05
  • 2
    after reading question, I thought Java has really introduced something interesting... – TechLover Sep 08 '14 at 11:08

2 Answers2

37

<-- 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
Daniel
  • 1,861
  • 1
  • 15
  • 23
8

<-- There is no such operator in java.

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

Not a bug
  • 4,286
  • 2
  • 40
  • 80