In Java, which gets executed first, "+" or "++"?

vivek :

I tried the following code in Java

t1 = 5;
t2 = t1 + (++t1);
System.out.println (t2);

My view is since ++ has a higher precedence than +, the above becomes

t2 = t1 + (++t1);
t2 = t1 + 6;      // t1 becomes 6 here
t2 = 6 + 6;
t2 = 12;

However, I get the answer 11 for t2. Can someone explain?

Tim B :

You are nearly correct but you are subtly misunderstanding how the precedence rules work.

Compare these two cases:

int t1 = 5;
int t2 = t1 + (++t1);
System.out.println (t2);

t1 = 5;
t2 = (++t1) + t1;
System.out.println (t2);

The result is:

11
12

The precedence does indeed say to evaluate the ++ before the +, but that doesn't apply until it reaches that part of the expression.

Your expression is of the form X + Y Where X is t1 and Y is (++t1)

The left branch, i.e. X, is evaluated first. Afterwards the right branch, i.e. Y, is evaluated. Only when it comes to evaluate Y the ++ operation is performed.

The precedence rules only say that the ++ is "inside" the Y expression, they don't say anything about the order of operations.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

in Backbone model, which method gets executed first, success or parse?

Java Return Future that gets executed first with a specific condition, or process both

If I have two substring_index, which one gets executed first?

Will be executed everything which is into parenthesis first?

which file is executed first in pytest?

Does the unary operator really gets executed first?

JQuery function return statement gets executed first then only the condition inside gets executed

If there are 2 always blocks which block will be executed first

Python: Detect code which gets never executed in production

Which file gets executed before _Events.groovy in Grails?

MediaWiki fails executing JavaScript which gets executed fine in browser console

How to make other function gets called after first is executed?

setInterval in a countdown timer gets executed multiple times after first round

only first line of wp_enqueue_style gets executed

$(window).on("load") gets executed before $(document).ready. Shouldn't ready be executed first?

Who selects which overloaded function to be executed in Java?

Will an instruction/statement which comes before other/s guaranteed to be executed first?

Which will be executed first, added column to entity or flyway migration?

Which one gets loaded first? static block or spring bean?

Java Scanner in Loop only gets first input

Why 'and not' gets executed in this if statement?

What is the name of the matplotlib function that gets executed when initially plotting the data which sets all axes correctly?

why superclass gets executed first in this program - although I have no main method?

which path is the first priority in Java?

Android Studio – Which .java file is executed when I run tests?

Executed gets not called after PreviewExecuted

Nothing after DELETE gets executed

Nothing after DELETE gets executed

addEventListener gets executed on the second click

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive