Java method()++ VS method()+1 -
this question has answer here:
i'm trying class.method()++
won't work.
simple example :
person class
public class person { private int age; public void age(int value) { this.age = value; } public int age() { return this.age; } }
in main class
following statements error p1.age()++
:
public static void main(string[] args) { person p1 = new person(); p1.age(p1.age()++); // error }
but below works fine :
public static void main(string[] args) { person p1 = new person(); p1.age(p1.age()+1); // works fine }
the main question :
why p1.age()++
error p1.age()+1
doesn't ?
p.s :
i know can :
person p1 = new person(); int myage = p1.age(); p1.age(myage++);
because
x++;
is short for
x = x + 1;
and in case be
p1.age() = p1.age() + 1; // error
and can't have method call on left hand side of assignment.
Comments
Post a Comment