• Post Reply Bookmark Topic Watch Topic
  • New Topic

Object[]  RSS feed

 
Jeremiah da Costa
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the following statement:

Object[] args = ((JavaEventBody) ctx.getEvent()).getJavaParameters();

I know that args is going to be an array so I can use the following:

String jeremiahid = (String) args[0];

So the jeremiahid is stored to the first array but what exactly is Object[] and the rest after the '=' sign really mean?

Thanks,
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Object[] args declares "args" as a variable that references an array of Objects.

So ((JavaEventBody) ctx.getEvent()).getJavaParameters() presumably returns a reference to an array of non-primitives.

Breaking this down, ctx.getEvent() should return a reference that can be cast to type JavaEventBody, and JavaEventBody should have a method getJavaParameters() that returns an array of non-primitives. This is what's being assigned to the variable "args." (Note: If ctx turns out to be null, or getEvent() returns null, or getEvent() returns a reference that cannot be cast to type JavaEventBody, then you will have a problem at runtime.)

String jeremiahid = (String)args[0] takes a reference to the first Object in the array (args[0]) and attempts to cast that reference to type String. It then assigns that reference to the variable "jeremiahid." (Note: If this Object is not really a String, then the cast will fail at runtime.)

So jeremiahid is not really "stored to the first array." Instead, it holds a reference (of type String) to the first element in the array, which presumably represents some sort of parameter associated with a ctx event (whatever that is).
[ December 14, 2007: Message edited by: marc weber ]
 
Bill Shirley
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
More context is always more useful, but...



I'm guessing there's a context, and you are retrieving an event from it.

It's likely some generic type of event that you want to treat as a specific event that you know it to be. So it is cast to that type.


Now that you have that type, you put parenthesis around it to force the order of operations. The period (.) binding is one of the tightest.

It would be much better coding style to say.

This will compile to the same efficiency of code as the initial but improves the readability (and debugability) of the code.

Finally, it appears that getJavaParameters() returns the array of Objects you were mentioning.

You could always loop over args.length members of the args to see them all.
 
Jeremiah da Costa
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so if I understand properly, there is an Event which I will 'get the Parameters' for and store those in an array 'args'.
And so... jeremiah will store the first parameter of the array as a string
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jeremiah da Costa:
so if I understand properly, there is an Event which I will 'get the Parameters' for and store those in an array 'args'.
And so... jeremiah will store the first parameter of the array as a string

That's correct (assuming all goes as expected).
 
Jeremiah da Costa
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to all...
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!