-1

I am trying to understand below statement came across code in my project

if(name != null && +ssnCount.getLongValue > 0){
    ......
}

Can some one explain me +ssCount.getLongValue mean? and difference. Thanks for you time.

Danubian Sailor
  • 1
  • 38
  • 145
  • 223
userJ
  • 65
  • 1
  • 11
  • What don't you understand about it? – Sotirios Delimanolis Jan 10 '14 at 20:23
  • 2
    That is not an increment, just an explicit + sign – Andreas Fester Jan 10 '14 at 20:24
  • this wouldnt compile. +ssnCount doesnt make any sense. but what do you need? – Hrishikesh Jan 10 '14 at 20:24
  • 4
    Here the `+` is the unary `+` operator. – rgettman Jan 10 '14 at 20:24
  • difference in using " && ssn.getLongValue" and" && +ssn.getLongValues"" – userJ Jan 10 '14 at 20:25
  • 2
    There is no difference between these... – tilpner Jan 10 '14 at 20:27
  • 1
    @rgettman why on earth is there a unary + operator... I couldn't see ever writing `int i = +5` – Cruncher Jan 10 '14 at 20:29
  • 1
    @Cruncher: [There is *some* purpose.](http://stackoverflow.com/questions/2624410/what-is-the-purpose-of-javas-unary-plus-operator) – tilpner Jan 10 '14 at 20:30
  • yes that's not a increment,it compiles and yep thats unary operator, may i know the difference in using " && ssn.getLongValue" and" && +ssn.getLongValues"" ? – userJ Jan 10 '14 at 20:30
  • @userJ: I already told: "There is no difference between these..." assuming getLongValue is a field of type long. – tilpner Jan 10 '14 at 20:32
  • @userJ ake a look at the link StackOverflowException posted (it might answer your question) – Алексей Jan 10 '14 at 20:33
  • Apart from your recent typo? Nothing. – Bohemian Jan 10 '14 at 20:33
  • @Cruncher It seems useless to me. I never use it. The unary numeric promotion it performs seems to be performed without it, when necessary, through assignment conversion, method invocation conversion, or binary numeric promotion. – rgettman Jan 10 '14 at 20:34
  • @rgettman I just realised, that as a result of this, I can use ++ as both a unary AND binary operator! `int i = 5 ++ 2;` or will java complain about this? EDIT: Nevermind, it recognizes `++` as a symbol on its own and complains. EDIT2: `System.out.println(i+++-+-+2);` Java didn't complain about this. That's pretty scary – Cruncher Jan 10 '14 at 20:37

2 Answers2

4

Oracle tutorial

  • Unary plus operator; indicates positive value (numbers are positive without this, however)

Actually there is no significant difference between an number variable and an number variable prefixed with the unary operator plus. It is interesting that this operator unboxes wrapped values to primitive types. For example if you declare:

Integer a = 10;

then using the reference 'a' will be of the reference type Integer, but using '+a' will be of the primitive type int. To be clearer:

Integer a = 10;
System.out.println(a instanceof Integer);// true
System.out.println(+a instanceof Integer);// compile time error - unexpected type
egelev
  • 1,175
  • 2
  • 11
  • 27
0

This looks like damaged code. The () after getLongValue is missing (I presume that, with that name, it was intended to be a function call), and unary plus is VERY rarely used.

I'd suggest taking it back to whoever wrote it, pointing these issues out to them, and asking them what they had in mind.

keshlam
  • 7,931
  • 2
  • 19
  • 33