r/learnjava • • Aug 23 '22

Why pre increment has no effect in this case?

i=20; j=30; System.out.println(i+++j); \\output 50 System.out.println(j); \\ output 30

1 Upvotes

11 comments sorted by

•

u/AutoModerator Aug 23 '22

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions

  • You include any and all error messages in full - best also formatted as code block

  • You ask clear questions

  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit/markdown editor: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

5

u/desrtfx Aug 23 '22 edited Aug 23 '22

i+++j

At first glance, this is extremely ambiguous. Yet, following the precedence order which prioritizes postfix, it should actually post increment i and then add j.

So, it should actually be i++ + j according to precedence rules.

You could verify that by printing i as well.

2

u/8igg7e5 Aug 23 '22

I presume the lexer is just matching on the order of the production in the JLS (3.12) but this could very likely have been ordered to match the language precedence.

It's a shame they didn't consider adding productions for these ambiguous cases so that such code would simply be rejected - preventing any 'correct' code from narrowing the pool of possible operators in the future... And making these pointlessly obscure puzzler cases impossible.

2

u/AreTheseMyFeet Aug 23 '22 edited Aug 23 '22

What does i+++j to expand to?
i + (++j) or (i++) + j?

Without proper spacing the intent isn't at all clear and you're at the mercy of whatever precedence rules java has implemented.
TBH, I'm a bit surprised it compiles at all with how ambiguous it is.

Edit: The question above was more of a rhetorical one for OP than me genuinely asking was the precedence actually is.
I know about the precedence specifications (though didn't remember the ordering of this off the top of my head) and I meant ambiguity from a code style perspective. If I was reviewing this as a pull request I would reject it based solely off that regardless of whether it's technically valid java syntax.

Thanks though /u/desrtfx and /u/fredoverflow for actually explaining and linking the specs. I'm sure others will find it useful.

5

u/fredoverflow Aug 23 '22

whatever precedence rules

+++ is already resolved to ++ + on the lexing level: https://en.wikipedia.org/wiki/Maximal_munch

3

u/desrtfx Aug 23 '22

Funny enough, following the precedence order it is not ambiguous at all.

First postfix unary, then prefix unary, then the rest.

0

u/Ruin-Capable Aug 23 '22

This is undefined behavior.

i+++j is ambiguous.

It *could* mean "(i++) + j".

It *could* mean "i + (++j)". (this seems to be what you expect).

It has no effect because apparently, the first interpretation of your code is the one the compiler used.

1

u/[deleted] Aug 23 '22

it's not undefined behavior, Java doesn't have that. it's perfectly well definrd. it is ambiguous though

-2

u/v4dk4n Aug 23 '22

Java goes from top to bottom, from left to right so basically you did i++ + j. In that line i was 20 j was 30 so i + j =50 In the next line i was 21 j was 30.

4

u/desrtfx Aug 23 '22

from left to right

This is not entirely correct as operations are done in precedence order.

In this particular case it (conveniently) matches the left to right order, but consider the following: i + j * 10 - what would the result be here? Unsurprisingly, it would follow the normal PEMDAS rules which do, in this case, not correspond to left-right order.

1

u/v4dk4n Aug 23 '22

True, sorry. I meant normally. So no special case.