Ask your own question, for FREE!
Computer Science 8 Online
OpenStudy (anonymous):

what is the difference between ++top and top++in data structures and algorithms in java?

OpenStudy (ajprincess):

Guess u r referring the one that comes in stack @Zaara. Right?

OpenStudy (anonymous):

yes u r ryt.... im littl confused with dat.....

OpenStudy (ajprincess):

when u initialize the top u give the value -1 which means that there is no element in the stack.If there is one element the index of top will be 0. when u insert the first element the index shoud be iterated from -1 to 0. In this case u use ++top. The iteration has to be done before the insertion. Am I clear?

OpenStudy (anonymous):

just compare ++top and top++... i like to knw the difference...

OpenStudy (asnaseer):

++top means "first increment top, then read its new value" top++ means "read the current value of top, then increment it" e.g. top = -1; x = ++top; will result in x=0, top=0 whereas: top = -1; x = top++; will result in x=-1, top=0

OpenStudy (anonymous):

asnaseer has nailed it... Java has two increment/decrement operators: ++ and --. These operators can be used both before and after the operand they are applied to. When used before they carry out prefix incrementing or decrementing. When used after the operand they carry out postfix incrementing or decrementing. The results are different in each case. For example top= 10; newTop = ++top; System.out.println(newTop); prints 11, but top = 10; newTop = top++; System.out.println(newTop); prints 10 In the second line of the first case top is incremented by 1 and then newTop is initialized with top. In the second line of the second case newTop is incremented to the value of Top and then Top is incremented. Hope this helps...

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!