wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes how exactly does this code compile? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how exactly does this code compile?" Watch "how exactly does this code compile?" New topic
Author

how exactly does this code compile?

joyita raksit
Greenhorn

Joined: Oct 12, 2006
Posts: 17
Hi,

I�ve been twisting over this for 3nights now and I�m officially in groundhog day. I�m working though Headfirst java, and I�ve come across a piece of code that I just cant understand � it�s from an excersise where you�re meant to figure out the output, so here it is inc output, and I�ll explain what I do & don�t get, any help would be so so so much appreciated, I might cry if I actually understand. I�ve even had a dream about this. seriously� need� help�

~~~~~~~~~~~~~~~~~~~~~~~~~~~

public class Mix4 {
int counter = 0;
public static void main(String [] args) {
int count + 0
Mix4 [] m4a = new Mix4[20];
Int x = 0;
while (x<9) {
m4a[x] = new Mix4();
m4a.counter = m4a[x].counter + 1
count = count + 1
count = count + m4a[x].maybeNew(x)
x = x + 1
}
System.out.println(count + � �
+ m4a[1].counter);
}

public int maybeNew(int index) {
if (index<5) {
Mix4 m4 = new Mix4();
m4.counter = m4.counter + 1;
return 1;
}
return 0;
}
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~

output is:
14 1


so I understand the maybenew method takes the int index arguement which is passed as x which is declared as 1, which is less than 5, therefore the return value is 1. 1 array and 1 object created from class�.
I guess I�d like to know how the compiler reads this code, how those output values (count + m4a[1].counter)
are arrived at, the addition of what iterations in which loop, what numbers when? Basically, can anyone could help walk me through how that output was computed?

Thanks for reading
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by joyita raksit:
...output is:
14 1...

Welcome to JavaRanch!

Yes, that's the output, once all the typos are corrected in your code.

I'll start by offering the cleaned up code, with Code Tags to keep the formatting intact...


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by joyita raksit:
... Basically, can anyone could help walk me through how that output was computed? ...

TIP: Adding println statements in the right places will allow your code to walk you through this. Here's your code with println statements telling you what's happening and when...
Tim LeMaster
Ranch Hand

Joined: Aug 31, 2006
Posts: 226
Better TIP: Using a debugger will allow you to literally walk through your code.
Robert Hill
Ranch Hand

Joined: Feb 24, 2006
Posts: 94
Debuggers have an extra learning curve. Trying to learn a programming languages can be a frusterating experience. Trying to learn a programming language plus tools(IDE, debugger, ect) compounds that frusteration exponentially.

Debuggers are valuable tools, but not when it gets in the way of learning. Adding in println statements take little time and requires no learning curve.
[ October 12, 2006: Message edited by: Robert Hill ]
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Robert Hill:
...Debuggers are valuable tools, but not when it gets in the way of learning...

I've been using a debugger for LotusScript for years. It's a crutch that's bailed me out plenty of times, but I haven't learned much.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how exactly does this code compile?