-2

I have the following code:

Integer b = 4;      
System.out.println(b+++10);

Why does System.out.println evaluate that expression? If you look at Java's source code you will not find the code that evaluates this expression.

Also, why does b+++ work? I thought that there could only be two + after a variable.

Thanks in advance.

sam1370
  • 335
  • 2
  • 18
BenMansourNizar
  • 1,558
  • 4
  • 21
  • 42
  • 9
    You realize that `System.out.println(b+++10);` is the same as `System.out.println(b++ + 10);`? – Pshemo Jun 01 '13 at 17:18
  • 2
    It's not really println that's evaluating it - if you assigned it to a variable and printed that you'd get the same result. (And this has definitely been answered before but I can't get a site search for "+++" to work.) – Rup Jun 01 '13 at 17:19
  • Do you also realise the evaluation of the expression has nothing to do with the use of System.out.println? Java 7 is context free and an expression is always evaluated the same way regardless of how it is used. – Peter Lawrey Jun 01 '13 at 18:57

2 Answers2

4

If you put the code in Netbeans, and format it (Alt-Shift-F) you will see this:

    Integer b = 4;
    System.out.println(b++ + 10);

So the result is 4 + 10 which is 14. The value of b is 5 after the command.

user000001
  • 32,226
  • 12
  • 81
  • 108
-1

Let me quote @Rup here for your first question:

It's not really println that's evaluating it - if you assigned it to a variable and printed that you'd get the same result.

To add onto that, it's Java that's evaluating that expression. If you put that into any other function it would work as well. It's part of Java — it's called "nested expressions."

For your other question see @user000001's answer, and there was also a discussion in the comments that added onto that (I have edited it to make it more clearer):

It's not clear at all normally the result is 15. b has been incremented.

In other words why Integer b = 4; System.out.println(b++ ); gives 4 even though b has been incremented. – BenMansourNizar

@BenMansourNizar This is because you are using the postfix operator (see http://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html.) With b++ the value is first used, and then incremented. If you use ++b, the value will be incremented before being used. – user000001 Jun 2 '13 at 17:58

Thanks a lot, user0000001 :) - BenMansourNizar

I hope you and other people find this information helpful, I really just quoted a bunch of stuff and commented on it :P

sam1370
  • 335
  • 2
  • 18
  • Would you have posted this post as well if it wouldn't be full of copied stuff and would only contain your own sentence "To add onto that, it's Java that's evaluating..."? Or would you agree, that this is more a comment than it's own answer? – Tom Sep 27 '17 at 17:10
  • @Tom I do not agree, this is not more a comment than its own answer. No, I probably would not have posted this. Here I'm just gathering information so that other people can find what they need to know instead of having to look through the comments. – sam1370 Sep 27 '17 at 17:56
  • I think a good analogy for your answer would be "Why have Wikipedia when you can read all the books about your topics?" Wikipedia is easier to access – sam1370 May 13 '18 at 04:41