Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Parameter Passing in Java

 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Mike Cunningham
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Corey,
Nice Flash example. Shows that Flash can be a useful tool even without Motion Tweening.
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
really nice ...
one picture is worth 1000 words
I think a link should be provided from javaranch to this page.
 
mkumar
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Corey,
This really servers for all programmers to remember a lot for SCJP and understanding core concepts too,
Thanks,
kumar
 
Marilyn de Queiroz
Sheriff
Posts: 9065
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nice, Corey.
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Corey,
Thanks for the application. I don't require to pull out any more hair on my head .
 
Tim Voet
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Seany Iris
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I cann't see,why?
Is it available still?
 
Vanitha Sugumaran
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, it is available. Just viewed it.
Good Job Corey!
Vanitha.
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 249
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Really nice job Corey !
Regards,
Bhushan
 
Nosa Irenen
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Crystal clear for a newbie like me.
Thanks.
- Nosa
 
Matt Ghiold
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Very nice Cory!
-Matt
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic