>> (2)Is there a way to find out what type of each element belongs to?
>> (say I forgot 1st element is Person, 2nd element is String, etc)
>
> if(a instanceof Person)...
> Patricia
Sorry. I am from a procedure language world. Trying to look at Java from
a different angle, like passing a method as a parameter into another
method to build higher level method.
Could you elaborate how the following works:
if (a instance of Person) ...
if (a instance of String) ...
Thank you.
Hendrik Maryns - 15 Sep 2006 15:19 GMT
Shawn schreef:
>>> (2)Is there a way to find out what type of each element belongs to?
>>> (say I forgot 1st element is Person, 2nd element is String, etc)
[quoted text clipped - 6 lines]
> a different angle, like passing a method as a parameter into another
> method to build higher level method.
Java does not do automatic type inference. The pre-1.5 collection
classes are not type aware. Generics is a (much debated) solution to
this, but only guarantees compile-type safety.
> Could you elaborate how the following works:
>
> if (a instance of Person) ...
> if (a instance of String) ...
Read better: if (a instanceof String) { // a is a String
String bigA = ((String) a).toUppercase();
}
Alternatively: a.getClass(), or other reflection methods.
H.
- --
Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
Patricia Shanahan - 15 Sep 2006 16:36 GMT
>>> (2)Is there a way to find out what type of each element belongs to?
>>> (say I forgot 1st element is Person, 2nd element is String, etc)
[quoted text clipped - 6 lines]
> a different angle, like passing a method as a parameter into another
> method to build higher level method.
In my experience, it is very important to approach each language on its
own terms, and first learn the normal ways of doing things in that
language. For that, books are invaluable. Once you understand the normal
ways of doing things in Java, a lot of the features will seem much less
arbitrary.
Patricia