FAQ
Cover
This is the archive Discourse for the Processing (ALPHA) software.
Please visit the new Processing forum for current information.

   Processing 1.0 _ALPHA_
   Programming Questions & Help
   Syntax
(Moderators: fry, REAS)
   object[]
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: object[]  (Read 345 times)
benelek

35160983516098 WWW Email
object[]
« on: Jan 27th, 2003, 1:43pm »

i'm having some trouble using an Object array - Object[]
 
the following code seems to work fine in defining an object in the Object array:
Code:

Object[] moo = new Object[10];
 
void setup() {
  size(200,200);
  moo[0] = new String("hi");
  println(moo[0]);
}
 
void loop() {
}

 
my problem is in using members of the array. for instance, the following example does not work:
Code:

Object[] moo = new Object[10];
 
void setup() {
  size(200,200);
  moo[0] = new String("hi");
  println(moo[0].length());
}
 
void loop() {
}

 
am i using the wrong syntax for accessing the string-specific method of length()? p5 complains, "cannot find method "length()""
 
-jacob
« Last Edit: Jan 27th, 2003, 1:44pm by benelek »  
fry


WWW
Re: object[]
« Reply #1 on: Jan 27th, 2003, 3:00pm »

in the second example, you need to cast the object to a type. change the line:
 
println(moo[0].length());
 
to
 
println(((String)moo[0]).length());
 
the reason the println() works is that it internally, the vm uses toString(), which is a method of all objects, and in the case of a string, is particularly useful however, the object class itself is generic, so the java vm doesn't know what sort of type might be buried under there when you try to do anything else with it.using a typecast helps it out.
 
this is similar to when you have to cast after pulling elements out of a Hashtable or a Vector, i.e.  
 
String thismoo = (String) myvector.elementAt(2);
String anothermoo = (String) myhashtable.get("a moo");
 
oddly, the following would probably work too:
println(moo[0].toString.length());
but for what you're trying to do, the ((String)moo[0]) is what you want.
 
benelek

35160983516098 WWW Email
Re: object[]
« Reply #2 on: Jan 28th, 2003, 5:04am »

thanx. this seems to be one of those small areas that Sun doesn't document at all.
 
-jacob
 
benelek

35160983516098 WWW Email
Re: object[]
« Reply #3 on: Jan 28th, 2003, 12:49pm »

it also seems silly, considering that Object.getClass() will tell u that the class of the object is already known. so.... is there a reason for this lack of easability - a bonus in the direction of low-level programming specifics? something else...
 
ie, what, exactly, can u do with this little corner of specific java unassumedness?
« Last Edit: Jan 28th, 2003, 12:51pm by benelek »  
fry


WWW
Re: object[]
« Reply #4 on: Jan 28th, 2003, 3:17pm »

that's a good point about the lack of documentation.. i can't say i ever remember reading that in a book so much as just having seen code that does it or something like that. we seem to be uncovering lots of these things on the bboard.  
 
as for the "why".. on one hand, it's a java compiler issue. the names of methods and functions have to be resolved at compile time, in some part due to java's strong typing. that is, you don't want to be scooting along in your app, only to have it realize that the 'length()' method doesn't exist. this is an attempt to, through this level of strictness, keep the language tight and programs stable. (remember java was first developed for embedded devices and internet appliances.. it needed to be crash-proof)
 
i'd also imagine that some portion has to do with how the vm works. the vm has to be told, by how the compiler marks up the code, where to find the function that's being executed. different assembly language instructions are used for 1) regular class methods, 2) overridden class methods, 3) static class methods. there are different amounts of overhead related to each.
 
there may be a more succinct, more specific answer, but i think it's several small things like this that were taken into account when putting the java language spec together.
 
Pages: 1 

« Previous topic | Next topic »