• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

JLS Postfix Increment Operator ++ Question

 
Ranch Foreman
Posts: 87
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In JSL §15.14.2, the second paragraph mentions "the result of the postfix expression must be a variable", then the third paragraph mentions "the result of the postfix increment expression is not a variable, but a value". Don't they contradict each other?

JSL §15.14.2 Paragraph 2 wrote:The result of the postfix expression must be a variable of a type that is convertible (§5.1.8) to a numeric type, or a compile-time error occurs


JSL §15.14.2 Paragraph 3 wrote:The type of the postfix increment expression is the type of the variable. The result of the postfix increment expression is not a variable, but a value.

 
Frank Mi
Ranch Foreman
Posts: 87
8
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got it.

The JLS consider "PostfixExpression ++" as the "postfix increment expression", the part without ++ as the "postfix expression". That is to say, ++ can be only used on variables. But the result is a value, so i ++ ++ doesn't work because the result of i ++ is a value.
 
Rancher
Posts: 3628
40
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You beat me.  But yes, that's right, exactly.
 
Saloon Keeper
Posts: 22505
151
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that this is the sentence that's confusing you:

JLS wrote:
The type of the postfix increment expression is the type of the variable.



A Postfix operator is a process with an input and an output. The input must reference a variable, The output is a value. And that value contains within itself a reference to the variable, which is what permits the ability to stack prefix and postfix and thus validly (if uselessly) create expressions like "--i++++".

The output of the aggregate pre/postfix expression set is a simple arithmetic value of the type of the original input variable. The reference to the variable itself, however, is lost once you exit the affixing expression context, leaving only a pure number.
 
Frank Mi
Ranch Foreman
Posts: 87
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Mike and Tim,  . Although I've deciphered the alien language, I would say the naming or the using of terms in plain English in JLS is pretty confusing.
 
Mike Simmons
Rancher
Posts: 3628
40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Frank Mi wrote:plain English in JLS


Parse error at line 1; sentence does not compile. ;)
 
Frank Mi
Ranch Foreman
Posts: 87
8
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Holloway wrote:... which is what permits the ability to stack prefix and postfix and thus validly (if uselessly) create expressions like "--i++++".



Not sure if I get you correctly, but I'm pretty sure --i++++ doesn't work.

 
Frank Mi
Ranch Foreman
Posts: 87
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Mike Simmons wrote:

Frank Mi wrote:plain English in JLS


Parse error at line 1; sentence does not compile. ;)



I'm so sorry my sentence failed to compile. I meant to say, when JLS wanted to use a man-made term "PostfixExpression", they should keep it is, rather than using it with the phrase "postfix expression" interchangeably. Please feel free to correct my grammar, I really appreciate that.
 
Marshal
Posts: 70333
283
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
... but ++++i-- will. (Actually, it won't because the compiler will think the rightmost minusplus is a uminus/unary minus/sign change minusunary plus and the preceding ++ increment doesn't have the right sort of operand.) Try − − − −i++ instead. Also tell us what the (unary) prefix + operator does.
 
Mike Simmons
Rancher
Posts: 3628
40
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Frank- no, sorry, I was joking.  The idea of "plain English in the JLS" sounds like a logical impossibility. Or at least, an improbability. . You were clear; nothing to apologize for.
 
Tim Holloway
Saloon Keeper
Posts: 22505
151
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Probably because the concept of prefix and postfix was ancient to the people who actually wrote the document and therefore self-evident.

I've often found system documentation to be less than ideal because the product changed between the original designers notes and the production release and because they'd gotten so acclimated to radical concepts that it didn't occur that they needed explaining. In the case of a formal language spec, the review process is supposed to weed that out, but here we are talking about something that was well-known long before Java was invented, so most likely the definition got carried over from somewhere without much thought about the full implications.
 
Frank Mi
Ranch Foreman
Posts: 87
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(This floor is hided because the poster doesn't know what he is talking about.)
 
Mike Simmons
Rancher
Posts: 3628
40
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell: well, you can get it to compile if you also assign it to something, e.g.

It doesn't work as a standalone expression.  More to the point, it does not in any way represent "the ability to stack prefix and postfix" - that statement was simply wrong.  Frank correctly addressed what Tim was actually talking about.
 
Frank Mi
Ranch Foreman
Posts: 87
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Mike Simmons wrote:Frank- no, sorry, I was joking.  The idea of "plain English in the JLS" sounds like a logical impossibility. Or at least, an improbability. .


Could I blame StackOverflow for my lacking of humor?  
 
Mike Simmons
Rancher
Posts: 3628
40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hah!
 
Campbell Ritchie
Marshal
Posts: 70333
283
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Mike Simmons wrote:. . . It doesn't work as a standalone expression. . . . .

No, I tried it as an argument to System.out.printlnl(). You can get it to compile on JShell as a standalone expression. But why on earth would you want to? It's one of those useless bits of code we take such delight in.
 
Frank Mi
Ranch Foreman
Posts: 87
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Mike Simmons wrote:. . . It doesn't work as a standalone expression. . . . .

No, I tried it as an argument to System.out.printlnl(). You can get it to compile on JShell as a standalone expression.



In my understanding, Jshell can take pure values instead of statements. It automatically assigns a non-statement value to an incremented variable. For example, if you just type 5, the value 5 is assigned to a variable named "$1". Kindly similar, System.out.println() also takes values.

But without Jshell, it doesn't compile the same as simple "i ++" as what Mike mentioned, since "+ i" is not a statement.
 
Tim Holloway
Saloon Keeper
Posts: 22505
151
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just did a check in Eclipse and its compiler does in fact indicate that you can't stack pre/post increment operators. That wasn't true historically in C, and as far as I know, not in Java either, although since it's bad practice, I've not coded anything that would have tested it.

So for the current edition of Eclipse at least, the "variable"-ness doesn't survive the affix and so it's strictly variable-in/value-out.

The expression "int k = - --i;" is valid, but the Eclipse java parser will not allow it unless you separate the prefix-minus and pre-decrement operators with a space or enclose the pre-decrement operation in parentheses. That's actually not consistent with the idea that spaces are ignored in arithmetic expressions and so a potential violation of the JLS and a bug in the compiler. You can argue that "---"is ambiguous, but there's only one meaningful way to interpret it, so it's really not.
 
Frank Mi
Ranch Foreman
Posts: 87
8
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Holloway wrote:I just did a test in Eclipse and its compiler does in fact indicate that you can't stack pre/post increment operators.


I just did the test using Java SE 11 compiler, and the Java SE 11 compiler doesn't allow pre/post increment stack either.


Tim Holloway wrote:You can argue that "---"is ambiguous, but there's only one meaningful way to interpret it, so it's really not.



I tried this code using Java SE 11 compiler. The statement was actually parsed to "int k = -- -i" according to the error message, that is why it doesn't compile.


First Update: just did some most useless tests in the world to prove why "---i" is parsed to "-- -i". Please pay attention to the position of the "^" symbol in error messages.
Very likely, the parser executed from left to right, takes -- one at a time until there is no -- any more. Then the ---i is parsed to --(-i), ---i is parsed to --(--i), -----i is parsed to --(--(-i)), and so on.


Second Update: Finally, I feel I'm on the right track. According to JLS §3.2 Lexical Translations, the longest tokens are used when tokenizing an expression no matter it makes sense or not.

JLS §3.2 wrote: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.


JLS §3.2 wrote:The input characters a--b are tokenized as a, --, b, which is not part of any grammatically correct program, even though the tokenization a, -, -, b could be part of a grammatically correct program.


This explains why "i+++++j" doesn't compile even if there is a valid interpretation "i++ + ++j", because the compiler always tokenizes it to "i ++ ++ + j". Below are some examples:

Some proof: In the first case, "i+++;" is tokenized to "i ++ + ;", the position of the last token ";" is actually the right hand side of the binary "+" which expecting an expression, thus that is an illegal start of expression. In the second case, "i++++" is tokenized to "i ++ ++", so the error message results from stacking of post increment "++ ++".



 
Mike Simmons
Rancher
Posts: 3628
40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim -

(written before seeing Frank's last post above)

If you try compiling with javac from the command line, I think you'll find this is not an Eclipse issue, but a Java rule.  As far as I know it's always been this way... though from my side, I guess I never explicitly tried to test it either.  But Frank already showed and explained the section of the JLS that prevents it... I can't find an online JLS older than 3rd edition (Java SE 6), but it was there back then as well:

15.14.2 Postfix Increment Operator ++

PostIncrementExpression:
       PostfixExpression ++

A postfix expression followed by a ++ operator is a postfix increment expression. The result of the postfix expression must be a variable of a type that is convertible (§5.1.8) to a numeric type, or a compile-time error occurs. The type of the postfix increment expression is the type of the variable. The result of the postfix increment expression is not a variable, but a value.


(emphasis added)

This is the same rule that bars stacking infix notation in the current JLS, that Frank originally asked (and answered) about.
 
Mike Simmons
Rancher
Posts: 3628
40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suspect this was an intentional change from C rules, where they were deliberately disallowing something that seemed unnecessary or confusing.  Like when they required the condition in an if statement to be boolean, preventing many of the errors when we mixed up = with == in C.  I very much doubt they would have changed this behavior in Java after public release... Sun just didn't do that sort of thing.  
 
Frank Mi
Ranch Foreman
Posts: 87
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Holloway wrote:You can argue that "---"is ambiguous, but there's only one meaningful way to interpret it, so it's really not.


For the interesting question mentioned by Tim, I also got something interesting from the JLS, and updated my previous answer (three floors above). I would like to discuss it with you guys, thanks. And sorry for the "reminder", because I updated an old answer, I'm not sure if you guys have checked it before.
 
Campbell Ritchie
Marshal
Posts: 70333
283
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Holloway wrote:. . . . You can argue that "---"is ambiguous, but there's only one meaningful way to interpret it, so it's really not.

But Frank Mi found a JLS section and explained that the javac tool, if it goes left to right, fails to resolve that ambiguity. (That merits a cow ) Yes, there is an internal inconsistency here, which you could reasonably call a bug, Tim.
 
Saloon Keeper
Posts: 12272
259
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Frank Mi wrote:And sorry for the "reminder", because I updated an old answer, I'm not sure if you guys have checked it before.


This is why we're typically not a fan of editing posts that people have already responded to: It disturbs the flow of the conversation. Even if you make an edit the way you did by clearly marking your updates, the topic will become harder to read because people can't easily determine what parts of it were replied to in subsequent posts. Please use the editing feature only to correct typos or formatting of your posts. If you have new information, make a new post.
 
Stephan van Hulst
Saloon Keeper
Posts: 12272
259
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Yes, there is an internal inconsistency here, which you could reasonably call a bug, Tim.


I don't see any inconsistency. The compiler first lexes the input, then parses the tokens. There are strict rules for both lexing and parsing. If there were ambiguity, you would be able to write a JLS-compliant compiler that treats ---i as valid code.
 
Campbell Ritchie
Marshal
Posts: 70333
283
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
---i would be valid code if parsed as -(--i). If the lexer matches the longest token first it will parse that as --(-i), which isn't valid. Since uminus and predecrement are right‑associative operators, it would be right to parse it as -(--i). The same applies to +/++. So I think Tim is right to call that a bug.
Obviously it would be very awkward to parse +++ or --- from right to left.
 
Stephan van Hulst
Saloon Keeper
Posts: 12272
259
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How is it a bug? It's intended behavior. It explicitly says so in the JLS.

Campbell Ritchie wrote:Since uminus and predecrement are right‑associative operators, it would be right to parse it as -(--i).


Why? Determining operator precedence is a parsing step. Lexing takes priority. Besides, why would it be correct to interpret ---i as -(--i) and not as -(-(-i))?
 
Campbell Ritchie
Marshal
Posts: 70333
283
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good point; I concede defeat.
 
Did you miss me? Did you miss this tiny ad?
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic