hey and happy new year!
i cant understand whats the difference between x++ and ++x
if anyone knows let me know :)

thanks in advance !

Recommended Answers

All 6 Replies

With x++, the value of x is evaluated before the increment. With ++x, it is evaluated after.
So

int x=0;
System.out.println(x++); // prints 0, x now = 1
System.out.println(++x); // prints 2, x now = 2

Ezzaral gave quite a nice example. x++ is known as a prefix increment and ++x is a postfix increment. Most commonly you will use a prefix increment (loops and such) ie., for(int i=0;i < 10;i++)

thanks both of you for your help!

public class Inheritance {


double width;
double height;
double deph;



Inheritance();
{


}


Inheritance(double w, double h, double x)
{
width = y;
height = h;
depth = d;
}
void volume();
{
System.out.println(width*height*depth);
}
}
class extend1 extends box
{
double weight;



extend1();
{


}
extend1(double w, double h, double d, double m)
{
super(w,g,d);
weight = m;
}


public static void main(String args[]);{



extend one = new extend1(14,11,12,15);
extend1 two = new extend1(18,13,12,15);


one.volume();
System.out.println("weight of one is "  one.weight);
two.volume();
System.out.println("weight of two is "  two.weight);


}


}

x++ is known as a prefix increment and ++x is a postfix increment.

Actually it's the other way around :)
pre == before
post == after

Actually it's the other way around :)
pre == before
post == after

Yeah, it was a long day :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.