-6

I have this code,

class  Test
{
  public static void main(String args[])
  {
    int a=--4-2*2/9-3;//Line  1
    System.out.println(a);
  }
}

Why is this compile time error on --4? Please dont suggest me to have -(-4). I am just trying to figure out, why is above code is a compile time error and if I change the code at line 1 to, a=- -4-2*2/9-3 the O/p is 1. i.e -(-4). For a second I miss took that, it must be same as -= i.e a=a-(rest of the expression). It gives same error with ++ operator too! Can any one explain me this?

Gpar
  • 181
  • 1
  • 1
  • 7
  • 2
    What are you hoping to do? – squiguy Nov 14 '14 at 20:32
  • 1
    When you have an error/exception, **ALWAYS ALWAYS ALWAYS** post what the error is and where it's happening. Don't waste everyone's time and make us sit here and guess! – tnw Nov 14 '14 at 20:33
  • 2
    There is no `=-` operator. – Jesper Nov 14 '14 at 20:39
  • The error is invalid argument to operation --/++. Stating this only to get @tnw's BP down. – Gpar Nov 14 '14 at 21:13
  • @Gpar I was simply trying to be helpful, there's no need to be rude. I've been on SO for a while and posts claiming an error and then declining to bother telling what is is don't fare very well as evidenced here. – tnw Nov 14 '14 at 21:21
  • @tnw, YOU are the one being rude, writing nasty things like 'Don't waste everyone's time'. I'm making an effort to clarify a doubt and even if I have not been clear with my query, there is a polite way of asking me to clarify. This is an open forum; nobody's personal column. If you think I'm wasting your precious time, do not bother to reply to my query. There are other people who are patient and considerate enough to answer my question, as evidenced from the replies below, and I will be thankful & grateful to them, and yes, I will fare well, thank you. – Gpar Nov 14 '14 at 22:58
  • @Gpar You know it's not just me, right? 6 people completely unopposed voted down to indicate this question is unclear and not useful. But of course only you're right and everyone else is wrong. – tnw Nov 17 '14 at 15:01
  • @tnw Yes, I do know that 6 others have voted down. No problem; at least they haven't left caustic & terse comment like you. Secondly, for those 6 (and you) who are irritated by my question, there are 4 others who are considerate enough to help me solve my query, and have succeeded. So, not everyone is voting down. I never said only I am right or that everyone is wrong; if anything, I have admitted that I may have been unclear with my query, but that there is a polite way of asking me to clarify. If you find anybody's query unclear, just vote down & ignore; don't leave nasty comments. – Gpar Nov 18 '14 at 22:48
  • @Gpar Pot calling the kettle black. I genuinely tried to help you avoid downvotes in the future and you responded with your own nasty comment about getting my blood pressure down. Get real. Some people just don't want to be helped I guess. – tnw Nov 19 '14 at 17:42
  • @tnw You guessed wrong; if I didn't want to be helped, I wouldn't have come to this forum in the first place.There is a proper way of offering help or advise, and when done in a constructive way, as was done by the 4 other people below, I am receptive & grateful to the suggestions. Get real yourself first; your attitude & tone in your very first comment was more like that of throwing alms than 'helping'. The BP down point was in response to your agitated 'ALWAYS ALWAYS ALWAYS' comment. Downvotes don't affect me or matter to me; disparaging comments like 'Don't waste everybody's time' do. – Gpar Nov 19 '14 at 23:22
  • @Gpar You have to understand it's extremely frustrating to see people like you failing to include very basic information that would get their question answered much faster or at all. This time you got lucky that we were able to discern the problem without it, but in most cases this will keep you from getting any answers. You can sit there and get offended all you want that I called you out for doing something stupid, but not bothering to even tell us what error you're getting IS a waste of everyone's time. If you don't understand that, I honestly feel bad for you. – tnw Nov 20 '14 at 14:44
  • @tnw I take your point about framing the question in a better/adequate way, but castigating replies in open forums are not only unhelpful but also deeply demoralising, however basic the error committed may be. Even when its warranted, criticism should be done in a constructive way, not in a caustic manner. I have taken your point, albeit with disappointment at the way it was made; not sure if you are broad minded enough to take mine. – Gpar Nov 20 '14 at 21:51

4 Answers4

8

The issue is that you're trying to decrement a constant.

For instance, --var.

What is "happening" looks like:

var = var - 1;

If you expand the notation with the constant (4) it looks like

4 = 4 - 1;

And that doesn't make sense.

Tango Bravo
  • 3,221
  • 3
  • 22
  • 44
  • 1
    Beware of how prefix and postfix works, OP: http://stackoverflow.com/questions/5413548/java-prefix-postfix-of-increment-decrement-operators – tnw Nov 14 '14 at 20:34
2

Read the specification: 15.15.2. Prefix Decrement Operator --

A unary expression preceded by a -- operator is a prefix decrement expression.

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

krokodilko
  • 35,300
  • 7
  • 55
  • 79
  • Sorry I have always problems with understanding JLS documentation. Is it --4 is not a convertible type variable? – Gpar Nov 14 '14 at 21:06
  • 1
    4 is not a variable - it's constant, or `literal` as they name it in the specification. Think a while .... `while( --4 < 0 ){ doSomething(); }`, does this loop make any sense ? How can I decrement a constant ? – krokodilko Nov 14 '14 at 22:45
2

-- is a "decrement" operator in Java and many other languages. The reason the compiler doesn't treat it as two - operators is that there's a basic rule that the compiler will look for the longest sequence of consecutive characters that forms one of its "separators" or "operators", as defined here. (> characters are handled a bit differently because of generics.)

This is explicitly stated in JLS 3.2:

The longest possible translation is used at each step, even if the result does not ultimately make a correct program while another lexical translation would. There is one exception [for > characters]...

Thus, when the compiler sees --4, it treats it as a -- operator applied to 4, which is illegal. It doesn't backtrack and try to find other ways to interpret the --.

But if it sees - -4, with a space between the hyphens, it can't interpret the - as anything else except individual minus signs. This is legal and has the same meaning as -(-4).

ajb
  • 31,309
  • 3
  • 58
  • 84
1

You can't use the a pre-decrement aka "-- operator" on a constant value (like 4). You can only use it on variables.

Johannes H.
  • 5,875
  • 1
  • 20
  • 40