• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

++ Operator.......

 
Ranch Hand
Posts: 435
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

options :
1)It will print 2,3
2)It will print 1,3
3)It will print 2,2
4)It will print 1,2
5)It will not compile.
Answer: 2)It will print 1,3
I feel the answer should be 1)i.e it will print 2,3
because a++ will make a = 2 and b++ will make b=2 + b i.e 2+1=3 Hence 1,3
Please explain..
Sonir
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when the increment operator is at suffix (i.e a++) then precedence will be last. Therefore a = a++ first a is assigned to a and a is incrementd later.
 
High Plains Drifter
Posts: 7289
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The ++ operator's location in a statement influences the outcome. Both a++ and ++a are legal, and both will ultiamtely give the same result. The question is, at what point will they give the same answer.
Given the example:

b will equal 1, while c will equal 2.
Now with that line of reasoning alone, you might expect the result to be 1,2 instead of 1,3 in the example you provided. But before we get to that, the more important point, certification or not, is that statements like b = b++ + b are just a bad idea. They're confusing and they make for stimulating exams, but they're not in there to be learning points. They're in there because C syntax leaves us a certain legacy that makes this klind of statement legal.
Having said that, read the JLS on the order of evaluation. It's well written, and will get you started on looking at the source of definitions rather then relying on everyone else's summary of them.
 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi , Michael Ernest
very glad to see you here!
:-)
i am reading your book now.
this concept is be explianed in "JAVA2 Certification Study Guud" P25

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I might be missing something, but I ran the code sample below
class Test5
{
public static void main(String[] args)
{
int a = 1;
int b = a++;
System.out.println("a = " + a + " b = " + b);
int c = ++a;
System.out.println("a = " + a + " b = " + b + " c = " + c);
}
}
The outcome was
a = 2 b = 1
a = 3 b = 1 c = 3
I don't quite understand how c could be 2 in this example. Was that a typo or have I missed something!
 
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe Michael meant to write c= a++; in his example.
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look at the precedece of the operators. The assignement "=" has lowest precedence, so it gets evaluated last.
d = 1;
d = d++;
The d++ expression has two parts:
1) The evaluation of the value of d
2) The incrementing of d
Since this is a postfix operator (after the operand), the VALUE of the expression is just the current value of d (before increment). So this just 1. Remember this. The VALUE of the expression d++ is one. That's what we are assigning to the left hand side.
THEN d gets incremented, so d is now 2.
THEN the value of the expression on the right hand side is stored in the variable on the left hand side. The value of the expression is 1, remember? SO that is what gets stored in d, effectively, writing-over the increment to d.
Rob
 
Ranch Hand
Posts: 417
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i am still not there:
int i=10;int j=i++;System.out.println("i= "+i+"j= "+j);
prints i = 11 and j = 10;
i is being incremented.
ok please consider this:
int i =10;
i = i++; i understand that first value of i on RHS is assigned to LHS, no increment. so i on LHS gets 10. fine. but then on RHS it gets incremented. and since there is only one place in memory where value of i is, then it would now get incremented. If yes then actually the value of i is incremented one now.
can you point out where is my reasoning incorrect ?

Originally posted by Rob Ross:
Look at the precedece of the operators. The assignement "=" has lowest precedence, so it gets evaluated last.
d = 1;
d = d++;
The d++ expression has two parts:
1) The evaluation of the value of d
2) The incrementing of d
Since this is a postfix operator (after the operand), the VALUE of the expression is just the current value of d (before increment). So this just 1. Remember this. The VALUE of the expression d++ is one. That's what we are assigning to the left hand side.
THEN d gets incremented, so d is now 2.
THEN the value of the expression on the right hand side is stored in the variable on the left hand side. The value of the expression is 1, remember? SO that is what gets stored in d, effectively, writing-over the increment to d.
Rob

 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The assignment is the last thing that happens. It happens after the variable has been incremented. Then the OLD value, the value before the increment happened, is stored in the variable.
i understand that first value of i on RHS is assigned to LHS, no increment
NO NO NO NO!!!
The LHS assignement doesn't happen until the ++ operand is fully evaluated, that includes the increment operation. So the very last thing that happens is the storing of the OLD value in the LHS.
Rob
[ January 25, 2002: Message edited by: Rob Ross ]
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
int a = 1; //line 1
a++; //line 2 - a is now 2
a = 1; //line 3
System.out.println(a); //prints 1
What happened to 'a' being 2??? Where did the 2 go?
It's gone . You overwrote it with the new assignment.

Rob
 
Ranch Hand
Posts: 516
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i am having similer doubt as mark stone.
i=10;
i=i++;//(1)
after line (1)
LHS i=10// ok no problom
after line (1)
RHS i=11// this is also no problom
then we starts printing
System.out.println(i);
prints 10 //problom is here
we know that 'i' is actually 11 now.but why 10 is printing??
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
check this
u will get the answer...
HTH
Ragards
Ravish
reply
    Bookmark Topic Watch Topic
  • New Topic