8

I was programming something and I was making a less than or equals operator and I was about to press the equals sign but I was accidentally holding down the Shift key so it made a plus sign. And it made this: <+ and IntelliJ isn't saying that it's an error so I just want to know what <+ does.

I tried looking it up online but I didn't really see anything about it

if (Integer.toString(data.getPhoneNumber()).length() <+ 10)

I thought it would give me an error or something.

Davislor
  • 14,674
  • 2
  • 34
  • 49
Victor Lomba
  • 181
  • 3
  • 3
    I think that is a unwary plus, it does nothing. The other operator would just be a less than comparison. – vandench May 12 '19 at 16:03
  • 2
    That code is 100% equivalent to `if (Integer.toString(data.getPhoneNumber()).length() < 10)` without the `+`. The plus does **nothing**. – luk2302 May 12 '19 at 16:05
  • 2
    It compares the length of the phone number to `+10`, known familiarly as `10`. There's a [list of Java operators](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/opsummary.html). – Dave Newton May 12 '19 at 16:05
  • `< +10` less than the number positive 10 (as opposed to -10). the plus is redundant – Michael May 12 '19 at 16:05
  • 1
    @rm-rfstar no, this is **not** a good question. The list of java operators is very well defined. It is obvious what the operators actually do here. And on top of that this question will not be searchable / finable by future users. – luk2302 May 12 '19 at 16:14
  • 1
    Ah, the good old [does-not-go-to operator](https://stackoverflow.com/q/1642028/1983772), I didn't know Java had it too. – Norrius May 12 '19 at 18:25
  • 1
    a very good example of a very bad formatted code. – Eugene May 13 '19 at 07:20

2 Answers2

24

It's just the spacing that makes it look special. Here's more traditional spacing:

if (Integer.toString(data.getPhoneNumber()).length() < +10)

which is

if (Integer.toString(data.getPhoneNumber()).length() < 10)

because the unary + doesn't do anything when applied to an int (10 is an int in that code).

From JLS§15.15.3:

15.15.3. Unary Plus Operator +

The type of the operand expression of the unary + operator must be a type that is convertible (§5.1.8) to a primitive numeric type, or a compile-time error occurs.

Unary numeric promotion (§5.6.1) is performed on the operand. The type of the unary plus expression is the promoted type of the operand. The result of the unary plus expression is not a variable, but a value, even if the result of the operand expression is a variable.

At run time, the value of the unary plus expression is the promoted value of the operand.

(their emphasis)

Community
  • 1
  • 1
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Little note to extend the answer: The reason why this exists in the first place is to explicitly highlight that a literal is **positive** as opposed to **negative** `+10` vs `-10`. Although it is probably rather confusing and you should stick to just `10`. – Zabuzard May 12 '19 at 16:57
1

In that case, the + operator simple specifies that the number 10 is positive: +10, as - would mean that is negative: -10. It doesn't matter if is away from it or close like: + 10, is the same as +10. But since the numbers that don't have a - (minus) sign are by default positive, the + sign is not necessary there. The < operator doesn't mind that + sign because it knows it belongs to that positive number.

If it was with a minus like this:

if (Integer.toString(data.getPhoneNumber()).length() < -10)

Then the program would compare the length() of the phone number to see if it is less than negative 10, which wouldn't make sense for the length of a telephone number.

Jeremy Then
  • 525
  • 2
  • 12
  • *"which wouldn't make sense for a telephone number."* Or indeed anything where 'length' has the conventionally-understood meaning! ;-) –  May 12 '19 at 16:55