|
for the pervious post, ++ operator is increment. you can either put it after
myVar++; // this adds 1 to myVar, equivelant to myVar += 1 or myVar = myVar + 1.
or you could put it in front : ++myVar.
depending on how you use it, there is a difference. e.g.:
int num = 1;
System.out.println(num);
System.out.println(num++);
System.out.println(num)
for the conversion constructor, no clue. in java there is normal constructors but never heard of that.
the results would be 1, 1 and 2. you would think, why two ones since i incremented on the second? it is because first it prints num, then increments its value. its as if its on its own line, except it displays the variable first. then printing num after that will give its value which was incremented before.so
int num = 5;
int getnum = num++;
System.out.println(getnum);
System.out.println(num);
this would return 5 and 6., because assigning num++ to getnum is actually assigning num, then incrementing num. then displaying num would give the value which you incremented. you might think this is annoying, is there a way around it? in some places , it does not matter, such as a for loop,
for (int i = 0; i < 6; i++) { //code }
and
for (int i = 0; i < 6; ++i) { //code }
would be identical, no matter what the code inside is. the way around it is befire the variable, ++num. so
int num = 1;
int getnum = ++num;
System.out.println(getnum);
System.out.println(num);
would return 2 and 2, because it adds one to num, then returns num. so its setting getnum to num+1 and adding 1 to num.
int num = 5;
System.out.println(num);
System.out.println(++num);
System.out.println(num);
would return 5, 6 and 6.
so, yourVar++ is like returning yourVar, then adding 1 to yourVar's value, while
++yourVar, is adding one to yourVar and returning yourVar after 1 has been added;
hope that helped for your ++ question
Last edited by JT4NK3D : 11-15-2007 at 12:37 AM.
|