This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes using Maha's trick for the hairy ++ ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "using Maha Watch "using Maha New topic
Author

using Maha's trick for the hairy ++ ?

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
This is one of the qstns on RHE's CD .....

The commented line is in the exam and the one below
is my qstn. Using Maha's trick-Ajiths' post this is what I think should happen:
Names[k++] = Names[k]+".";
Names[1(2)] = Names[2]+".";
removing the braketed values:
Names[1] should be "Three."
Obviously its not....any clues....
The above code prints:
One
Two.
Three
Thanks.
- satya

Btw, with the commented line (assuming the line below is not there), the foll is printed:
One.
Two
Three

[This message has been edited by satya5 (edited June 05, 2000).]
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
Satya,
Maha's method STILL works. May be I DID NOT say this explicitly in the previous posts since this topic didn't comeup there.
Please see carefully. What happens if there is a statement and both lefthandside and righthandside has got some expressions to evaluate before this statement is executed ? This is what happens. The left hand side expression is evaluated first and the result value is remembered, then the right hand side expression is evaluated.
In case of expressions involving out hairy ++/-- you still apply Maha's SAME OLD method. But you SHOULD NOT CARRY OVER the result of a var from left hand side calculations TO RIGHT HAND SIDE calculations. THis is the point to be noted here/not explicitly shown in previous discussions. Thanks Satya for brought to notice.
So let us get into action. When you cut and paste this code check each case separately.
<pre>

class Test {
public static void main(String[] args) {
String Names[] = {"One", "Two", "Three"};
int i = 1;
int j=1;

/*-------------case 1---------------
// Names[--i] += ".";
// Names[--i] = Names[--i]+".";
// Names[(0)0] = Names[(0)0] + ".";
// Names[0] = Names[0] +".";

*/

/*prints
One.
Two
Three
*/


/*-------------case 2------------------
Names[--i + (++i)] = Names[--i]+".";
//Names[(0)0 + (1)1] = Names[(0)0] +".";
//Names[0+1] = Names[0]+".";
//Names[1] =Names[0]+".";
*/

/*prints
One
One.
Three */


/*------Case 3--------------------

Names[++i] += ".";
// Names[++i] = Names[++i]+".";
// Names[(2)2] = Names[(2)2] +".";
//Names[2] =Names[2]+".";
*/

/*prints
One
Two
Three. */


for (int j = 0; j < Names.length; j++)
System.out.println(Names[j]);
}
}

</pre>
Satya, if you examine this code ( I included 1 extra case also apart from 2 cases of yours )
you know why you got the above results of yours. Please reply back.

Also please refer to JLS here for further Java tasty food especially this section. There is a fine example at the bottom of this JLS section which is almost same as our discussion here. Ok I extract ONLY the example.
JLS Example in the above link
The following program illustrates the fact that the value of the left-hand side of a compound assignment is saved before the right-hand side is evaluated:
<pre>

class Test {
public static void main(String[] args) {
int k = 1;
int[] a = { 1 };
k += (k = 4) * (k + 2);
a[0] += (a[0] = 4) * (a[0] + 2);
System.out.println("k==" + k + " and a[0]==" + a[0]);
}
}

</pre>
This program prints:
k==25 and a[0]==25
The value 1 of k is saved by the compound assignment operator += before its right-hand operand (k = 4) * (k + 2) is evaluated. Evaluation of this right-hand operand then assigns 4 to k, calculates the value 6 for k + 2, and then multiplies 4 by 6 to get 24. This is added to the saved value 1 to get 25, which is then stored into k by the += operator. An identical analysis applies to the case that uses a[0]. In short, the statements
k += (k = 4) * (k + 2);
a[0] += (a[0] = 4) * (a[0] + 2);
behave in exactly the same manner as the statements:
k = k + (k = 4) * (k + 2);
a[0] = a[0] + (a[0] = 4) * (a[0] + 2);

regds
maha anna

[This message has been edited by maha anna (edited June 05, 2000).]
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Maha:
Thanks, yet another incredible example and explanation.
While I agree to all you said, I wanted to stretch the
logic and I seem to miss something...
//Case 4
/*-------------case 4 extending Case 2 -----------
String Names[] = {"One", "Two", "Three", "Four"};
int i = 1;
Names[(--i) + (++i) + (++i)] = Names[(--i) + (++i)]+".";
//Names[(0)0 + (1)1 + (2)2] = Names[(0)0 + (1)1] +".";
//Names[0+1+2] = Names[1]+".";
//Names[3] =Names[1]+".";
*/
Following the logic above I expect to see:
One
Two
Three
Two.
However I happen to see:

/*prints
One
Two
Three
Four. */
Am I missing something....I still have to read the JLS
section though ...
ps: I am not trying prove your logic is wrong....trying
to understand it correctly
. I am sure you don't mind.
Thanks.
- satya
Maha:
There seems to be some difference in your current explanation and your previous one about Arrays or so I think.
Previously you said:

int i=5;
array[i++]=++i+i++;
array[5(6)] = (7)7 + 7(8);

Can you throw some light here .....
Are we not taking the value 6 from LHS to RHS in the above case...
Thanks.
- satya

[This message has been edited by satya5 (edited June 05, 2000).]
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Maha:
Again a million thanks for the JLS link. I think I have
figured out the things to a certain extent. (Well, what else
was I doing for the last 24 hrs...heheh).
Here goes my research:
For all cases use:
i = 1;
names = {"One", "Two", "Three", "Four"};
Case 1:
names[(--i) + (++i) + (++i)] = names[(--i) + (++i)] + ".";
LHS: names[(0)0 + (1)1 +(2)2] = names[3]
and now i = 2
RHS:
Start with i = 2
(since this is not a compound stmt += kind of)
names[(1)1 + (2)2] = names[3]
So the equation is:
names[3] = names[3] + "."; and i = 2 at the end.
Case 2:
names[--i + ++i] = names[--i] + ".";
Same as Case 1. Finally:
names[1] = names[0] + "." and i = 0
Case 3:
names[--i] += ".";
names[--i] = names[--i] + ".";
Since this is a compoud stmt, the LHS is expression is evaluated only once. So,
names [(0)0] = names [(0)0] + ".";
names[0] = names[0] + ".";
Value of i = 0;
Case 4:
names[i++] += ".";
Similar to Case 3 above. Finally,
names[1] = names[1] + ".";
Value of i = 2;
Case 5:
i = i++ + ++i + 7 + --i + i--;
Discussed in previous session (link above). Well,
i = 1(2) + (3)3 + 7 + (2)2 + 2(1);
Finally i = 15;
Case 6:
i += i++ + ++i + 7 + --i + i--;
Simplyfing the compound stmt,
i = i + i++ + ++i + 7 + --i + i--;
i = 1 + 1(2) + (3)3 + 7 + (2)2 + 2(1);
Finally i = 16;
Case 7:
names[i++ + ++i + 7 + --i ] = names[0] + ".";
Watch closely, I am trying to force an
ArraIndexOutOfBoundsException.
Read the clause "When the expression is terminated abruptly ... in the JLS link posted by Maha.
Evaluating:
names[1(2) + (3)3 + 7 + (2)2] = names[0] + ".";
names[13] = names[0] + ".";
Value of i = 2; This has changed.
Case 8:
names[i++] += names[--i + ++i] + ".";
Compound stmt.
names[i++] = names[i++] + names[--i + ++i] + ".";
LHS expression is evaluated only once and used on both sides
then the remaining RHS is evaluated seperately.

names[1(2)] = names[1(2)] + names[(1)1 + (2)2] + ".";
names[1] = names[1] + names[3] + ".";
Value of i is 2.
Case 9:
names[i++] = ++i + i++ + " ";
names[1(2)] = (3)3 + 3(4) " "; //" " is to convert the int value to string.
names[1] = 6 + " ";
Again the previous discussion holds good.
Value of i is 4.
Case 0: (zero)
names[i/0] += ".";
Watch closely, I am forcing an ArithmeticException (int), for the
same reason as in Case 7 above.
Nothing should change.

Thus ends my research for now. Bottomline most of our
previous discussion holds.
The rules get pretty messy
for compound expressions involving arrays and array index
expressions. Just wanted to gets things straightned out.
Though the cert exam may not ask such complex qstns (from
what others have said on the ranch) this was a good experience
for me.
I will post all the code in a foll post.
Hope this helps.
Maha thanks again. Regds.
- satya

[This message has been edited by satya5 (edited June 06, 2000).]
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
The foll is the code for the above nine odd cases.
Execute it with an int corres to the case stmt above.
Ex: java ArrComp 1 would give the output for case 1.
Reading the JLS link helps, I strongly suggest it.
Again, the rules are messy and I (may not) have not
covered everything. I was in a dis-array before reading
the link (my reply to Maha's post).
Hope this helps.
Regds.
- satya



[This message has been edited by satya5 (edited June 06, 2000).]
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
Satya,
You moulded Maha's simple and Easy method to fit the compound statements involving ++/-- in the LHS . Thanks.
I was a dumb when I said 'the result of LHS id NOT carried over ro RHS'. The resulting var values from LHS calculations are in fact CARRIED OVER to RHS as I said in Alkesh's post before. So the point to be taken care is this. In case of compound statements involving ++/-- in the LHS side

Case 8:
names[i++] += names[--i + ++i] + ".";
Compound stmt.
names[i++] = names[i++] + names[--i + ++i] + ".";
LHS expression is evaluated only once and used on both sides
then the remaining RHS is evaluated seperately.
names[1(2)] = names[1(2)] + names[(1)1 + (2)2] + ".";
names[1] = names[1] + names[3] + ".";
Value of i is 2.

ONce again thank you Satya.
regds
maha anna
--------------------------------------------------------------
[This message has been edited by maha anna (edited June 06, 2000).]
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944

Maha:
I hope I din't upset you or something...
All I did was extend your well known logic
with the ++/-- operators.
Just wanted to clear this stuff ...... I got mixedup
with the compound operators and the simple ones.
After reading the JLS link things were clear.
Regds.
- satya
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
No NO No NOt at all Satya. Why did you get such a thought of I may be upset. In fact before I saw your AFTER READING JLS post, I came here and was about to give the SAME solution as yours.
You made Maha's work easier. . ANd you know what ? I am really glad I made you do some work, and now you understand the feeling of some achievement however small/medium/hard concepts when we dig and find out on our own. You are a good spirit.
regds
maha anna
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944

Thanks Maha.
I had the exact same feeling.
But I do need your help and
encouragement though, which I always had
and ofcourse will .
You are a very good mentor.
Regds.
- satya
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: using Maha's trick for the hairy ++ ?
 
Similar Threads
Code segment question #3(5)
Displaying output for the given format
Is this a bug in Java ?
Nested For Loops - understanding them
Switch case