This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

please explain

 
Ragu Sivaraman
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<CODE>
class Q43
{
int y=1;
Q43()
{
this(y);
}
Q43(int x)
{
y=x++ + ++x;
}
public static void main(String [] args)
{
Q43 t = new Q43();
System.out.println(t.y);
}
}
</CODE>
Why it doesnt compile?
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Compiler collects the instance field initialization and instance initializers in textual order and places the resulting code inside any constructor that does'n begin with this(...). It places that code after the implicit/explicit call to super(...) but before any code that the programmer wrote in the constructor. The compiler complains because if the declaration of y was placed in the constructor that takes argument calling this(y) refers a variable that is not initialized yet.
The resulting code is like this

To check that use javap
hope it helps
 
Ragu Sivaraman
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thankx for the reply..
int[] a={11,12,13,14};
int[] b={0,1,2,3};
System.out.println(a[(a=b)[3]]);
a=b doesnt have any effect??
 
Michael Ernest
High Plains Drifter
Sheriff
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not exactly. Look at the following:

------------------
Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide
 
Ragu Sivaraman
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Michael Ernest:
[B]Not exactly. Look at the following:

[/B]

Thankx for your help mike...
But i am still having difficulty in understanding..
(a[(a=b)[3]]);
How come this gives two different outputs?
Please help
 
Michael Ernest
High Plains Drifter
Sheriff
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interesting, huh? It's because the act of assignment only changes things once. After that, a and b are the same value. But because assignment operations have a low priority, they usually evaluate after index operations, thuis leaving the first expression unaffected by its use.
------------------
Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JLS 15.13.1 does wonders for explaining these kinds of expressions:

* First, the array reference expression is evaluated. If this evaluation completes abruptly, then the array access completes abruptly for the same reason and the index expression is not evaluated.
* Otherwise, the index expression is evaluated. If this evaluation completes abruptly, then the array access completes abruptly for the same reason.

Thus the evaluation of what it is inside [] can not affect to the evaluation of the array reference expression, because this has happened before. But an assingment within [] is effective to the code that is at the right of where occurs:
That is why
System.out.println(a[(a=b)[3]] * a[1]);
prints the result of 14*1
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic