Results 1 to 3 of 3
Thread: Question about ++x and x++
- 10-17-2010, 04:14 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 4
- Rep Power
- 0
Question about ++x and x++
In Java, do these two operators mean the same thing?
x++ and ++x
So if x = 6
x++ = 7
++x = 7
And the code below would print 14.
The problem is...Java Code:System.out.println(x++ + ++x);
when x = 6 and
I get results: 14, 8, 10Java Code:System.out.println(x++ + ++x); System.out.println(x++); System.out.println(++x);
Please can someone explain why? Thank you.
- 10-17-2010, 05:33 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
Straight from the Java Language Definition:
kind regards,
Originally Posted by JLS
JosLast edited by JosAH; 10-17-2010 at 05:37 PM.
- 10-18-2010, 10:50 AM #3
Member
- Join Date
- Oct 2010
- Posts
- 14
- Rep Power
- 0
Code:
System.out.println(x++ + ++x);
System.out.println(x++);
System.out.println(++x);
In this code first line makes x++ -> 7 again ++x -> 8
so, now the value of x when coming to second line is 8, so it prints the X value as 8,then
in second line one more increment,so x= 9 now, in third line pre-increment(++x) so it prints x value as 10 for the third SOP statement.
Regards,
Suresh. S.A.
Similar Threads
-
Question mark colon operator question
By orchid in forum Advanced JavaReplies: 9Last Post: 12-19-2010, 08:49 AM -
question
By berkeley in forum New To JavaReplies: 5Last Post: 06-03-2010, 08:56 PM -
help question
By 99w210 in forum NetBeansReplies: 0Last Post: 03-08-2010, 05:23 AM -
help me in dis question
By krishan in forum New To JavaReplies: 10Last Post: 02-16-2009, 07:04 PM -
question
By zizou147 in forum New To JavaReplies: 13Last Post: 07-04-2008, 07:05 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks