aspose file tools *
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Parameter Passing in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Parameter Passing in Java" Watch "Parameter Passing in Java" New topic
Author

Parameter Passing in Java

Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
It seems to me that a lot of people ask about passing parameters (reference parameters, specifically) in Java. Unfortunately, I've always had a hard time explaining what's happening behind the scenes because I can't draw a stack trace very well for them.
Well, I've tried to solve this problem. I've made a small Flash application that is designed to draw a stack trace for a simple Java application. I'd appreciate it if some of you could take a look at it and let me know what you think. The description I have below is very rough - I just wanted to put something there, but I plan to rework that section.
You can find the application here. There are two links, one for high-res (1024x768+) and low-res (800x600-).
Thanks,
Corey
[ April 16, 2002: Message edited by: Corey McGlone ]

SCJP Tipline, etc.
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Corey !!! really nice !
my comments:
- in main the first int variable is called a and in your activation stack you write i.
- in modifyArguments intArray[length-1] should be intArray[intArray.length-1]
- it is better to use the verb "invoke" instead of "call" (in the comments)
- the comments when modifyArguments will be invoked should be "...a and anArray." instead of "...a and intArray."
- the comments when modifyArguments is about to return should be "...removed from the activation stack." instead of "...removed from the stack." so that it is clear about which stack we are talking.
- the comments when System.out.println is invoked in main should be "...a and anArray." instead of "...i and anArray."
- the comments when main is about to return should be "...removed from the activation stack." instead of "...removed from the stack." so that it is clear about which stack we are talking.
Moreover, I'm not quite sure yet, but I think that the loop variable i in the for loop should appear in the activation frame for method main. But I think it depends on the implementation of the compiler.
I think the rest is correct at the first sight !! Truly amazing. You should maybe add one of our favorite StringBuffer object in the code so that people are really aware that a copy of an object reference is passed to the method. It may not be obvious to everybody that an array is an object.
Keep it up...
[ March 05, 2002: Message edited by: Valentin Crettaz ]

SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Mike Cunningham
Ranch Hand

Joined: Nov 14, 2000
Posts: 129
Corey,
Nice Flash example. Shows that Flash can be a useful tool even without Motion Tweening.
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Thanks for all the catches, Val. I guess I had more typos than I thought.
I've fixed pretty much everything you pointed out except the addition of the StringBuffer. The app I made was really a prototype that grew into the real thing. Unfortunately, that makes it pretty difficult to maintain and/or modify. I hope to work on a new version, in which I'll use a StringBuffer, but that might take a little while.
I'd appreciate it if you'd take one more quick look to see if I've missed anything else.
Thanks,
Corey
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
That seems alright, although I have one more comment. args should be present in the activation record of the method main much the same way the parameters of modifyArguments are present in the activation record of modifyArguments. Your code does not use args but it is just for consistency purposes...
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
this was very cool. How long does it take to make this in Flash?
It would be cool to write a little applet framework to do it in java. I know it would probably take longer to do it in java than Flash, but you'd learn a lot of java by doing it, plus you'd be able to extend it in ways you can't with Flash.


Rob
SCJP 1.4
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by Rob Ross:
this was very cool. How long does it take to make this in Flash?
It would be cool to write a little applet framework to do it in java. I know it would probably take longer to do it in java than Flash, but you'd learn a lot of java by doing it, plus you'd be able to extend it in ways you can't with Flash.

I made this in Flash in a couple hours. Actually, if I would have put more thought into this, it could be made to be pretty flexible in Flash. Unfortunately, I was just messing around and the junk that I was messing with turned into this application. Now, it's almost impossible to change and maintain. I have some good ideas of how I can "redo" it and make it much nicer - now I just have to find the time to do it.
You're right, Val, the parameter args should be in that activation record. Unfortunately, I don't have anywhere to put it and the way I have this thing thrown together, moving everything around just wouldn't be worth it. I'll try rebuilding this thing and I'll be sure that gets in the next version.
Corey
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
No rush Corey
Your application is very good as it is... Don't overdo it, the concepts are crystal-clear
Keep it up !
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Okay, I feel like an idiot. I've been trying to rewrite the text beneath that Flash application and I've been trying to find the part of the JLS where it says something like "all parameters are passed by value" and I just can't find it.
So, I humbly defer to our walking JLS, Valentin, to aid me in my plight.
Of course, if anyone else can tell me where it is, I'd be happy to hear from them, as well.
Thanks,
Corey
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Hello,
Very nice.
If you're looking for nitpicking...
You have one or two grammatical errors in the text on the bottom half of the page, but they aren't significant.
A "Back" button would be nice.
Good Luck,
-Dirk Schreckmann


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
This is somehow contained in the following sections but you have to read between the lines a little:
15.12.4.5 Create Frame, Synchronize, Transfer Control
8.4.1 Formal Parameters
R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5371
really nice ...
one picture is worth 1000 words
I think a link should be provided from javaranch to this page.


"Thanks to Indian media who has over the period of time swiped out intellectual taste from mass Indian population." - Chetan Parekh
mkumar
Greenhorn

Joined: Mar 06, 2002
Posts: 1
Corey,
This really servers for all programmers to remember a lot for SCJP and understanding core concepts too,
Thanks,
kumar
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9053
    
  12
Nice, Corey.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
mkumar,
Please, read the Javaranch Naming Policy and change your publicly displayed name to comply with our unique rule. Thank you for your cooperation.
mounika paruchuri
Greenhorn

Joined: Feb 12, 2002
Posts: 26
hi corey ,
The application is real praise worthy ....
this will be appreciated surely for the effort u've put to clear a concept for a new programmer...
Mounika
Arun Pai
Ranch Hand

Joined: Mar 11, 2002
Posts: 143
Corey,
Thanks for the application. I don't require to pull out any more hair on my head .
Tim Voet
Ranch Hand

Joined: Jan 30, 2002
Posts: 30
Hi,
nice job....
very well done. Its the first attemps at a visual explanation of Parameter passing i have found. ( just wish i found it before i spent the time trying to figure out on paper....
I also think that this link should be put on the Javaranch's links page.
Tim


---<br />Nothing is impossible, only improbable !!!
Seany Iris
Ranch Hand

Joined: Jan 08, 2002
Posts: 54
I cann't see,why?
Is it available still?


help you means help me
Vanitha Sugumaran
Ranch Hand

Joined: Apr 11, 2001
Posts: 356
Yes, it is available. Just viewed it.
Good Job Corey!
Vanitha.
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Make sure you have the Flash plug-in if you want to view this application. If you don't have it, or don't know if you have it, go to http://www.macromedia.com/.
Bhushan Jawle
Ranch Hand

Joined: Nov 22, 2001
Posts: 249
Really nice job Corey !
Regards,
Bhushan
Nosa Irenen
Greenhorn

Joined: Mar 09, 2002
Posts: 8
Crystal clear for a newbie like me.
Thanks.
- Nosa
Matt Ghiold
Ranch Hand

Joined: Feb 24, 2002
Posts: 213
Very nice Cory!
-Matt


-Matt<br /> SCJP2<br /> SCJD
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
I hate to be a bother but, due to some changes on my website, I've changed the location of this Flash Application. If you happen to have bookmarks or anything set up to link to this, please update them. You can find my homepage at: http://www.geocities.com/mcglonec1978/ or you can link directly to this Flash application by linking to: http://www.geocities.com/mcglonec1978/javacert/javacert.html.
I don't plan on moving this again, so, hopefully, I won't bother anyone about this again in the future. I just wanted to make sure that anyone that wanted to use this as a reference could still find it.
As a side-note, I've edited the initial post in this thread to link to the correct location.
Thanks,
Corey
Jeff Allen
Greenhorn

Joined: Feb 09, 2002
Posts: 20
Thanks Corey!!
You Flash presentation was most helpful in my SCJP2 studying. (I had to allow active-X to be run on my browser before I could get it but it was well worth it).
Bravo,
Jeff Allen - Still Studying ...
La Vish
Ranch Hand

Joined: Apr 17, 2002
Posts: 161
Hi Corey,
I just viewed your flash application. Not for nothing they say a picture is worth a 1000 words.
You are a genius,mate!
You have made it so easy for a greenhorn like me :roll: to understand the concept.
Thanks a lot.


La Vish
SCJP 1.4, President 60s Club
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Parameter Passing in Java