What is ++int in Java?

13,504

Solution 1

a = 5; b = ++a; // a = 6, b = 6
a = 5; b = a++; // a = 6, b = 5

Solution 2

int a=1;
System.out.println(a++);

prints "1"

int a=1;
System.out.println(++a);

prints "2"

Or maybe i don't understand your question.

Solution 3

++int increments int by 1 before and int++ increments by one after

Share:
13,504

Related videos on Youtube

user1260584
Author by

user1260584

Updated on June 04, 2022

Comments

  • user1260584
    user1260584 almost 2 years

    Possible Duplicate:
    explain working of post and pre increment operator in Java
    What is the difference between int++ and ++int?

    In Java, what is ++int? What does it do? What does it mean?

    (Sorry, but I didn't ask the question correctly last time.)

    • Brendan Long
      Brendan Long about 12 years
      It's exactly the same as the C++ answer you got in your other question..
    • paxdiablo
      paxdiablo about 12 years
      In future, please search before posting a question.
    • Kirk Woll
      Kirk Woll about 12 years
      @Brendan, mostly true, but any differences are fully elaborated here.
  • Sionide21
    Sionide21 about 12 years
    Other way around: a is incremented after the assigment. b uses the old value of a