3

Following this tutorial I saw that at the end in the MainActivity.java there is an array that I understand but dont know what a single + sign does at the end

String Slecteditem= itemname[+position];

I remove the + and I think it works the same way, but not sure if there is a purpose that I cant see.

Jason
  • 11,448
  • 3
  • 41
  • 46
Pulse9
  • 311
  • 3
  • 14

4 Answers4

8

+ is the unary plus operator. It's unnecessary here as array indices can never be negative

Reimeus
  • 155,977
  • 14
  • 207
  • 269
3

It's a unary positive, so your intuition was correct +position is the same as position.

Elliott Frisch
  • 191,680
  • 20
  • 149
  • 239
3

I bet the author of that code was just writing too much JavaScript lately. In JavaScript, this is a popular trick because unary + is the most concise way to convert a string to a number. In Java, unary plus can only be used for more obscure conversions, but that doesn't apply here because position is already an int, and I won't recommend ever writing such code anyway.

Community
  • 1
  • 1
Sergey Salnikov
  • 321
  • 1
  • 5
0

single positive(+) and single negative(-) signs in java are unary operators meaning they are used for indices, that is to depict that a number is either positive or negative. your code will still work since array values are never negative

Agbalaya Rasaq
  • 115
  • 1
  • 10