By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,034 Members | 819 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,034 IT Pros & Developers. It's quick & easy.

Meaning of notation (Type)variable

P: n/a
Hello,
I am planning to program an Xml displayer that will show an Xml file
in a tree view. This will involve the development of a tailored tree
model which will use DOM nodes instead of the DefaultMutableTreeNode.
To get an idea of how a tree model can be generated, I have downloaded
an example from the sun homepage in which file objects are used as
TreeNodes. This program resembles mine in that the tree will be
constructed from objects which are differnt from the standard TreeNode
object. HOwever, I think that I have not yet got a very basic conecept
of Java, because I do not know what e.g. the following lines mean
<Snip>
public boolean isLeaf(Object node) { return ((File)node).isFile();
}

public int getChildCount(Object parent) {
String[] children = ((File)parent).list();
if (children == null) return 0;
return children.length;
}
</Snip>
particular the notation "((File)node)".
The isLeaf function receives an "Object" as argument which probably
does not have an "isFile" method. So does the expression ((File)node)
gives some of the methods for "File" objects to the general "Object"
object? How is this language feature called, because I have frequently
seen lines like this and I would like to understand that topic.
Thanks in advance
Peter
Jul 17 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
pi********@gmx.de (Piet) wrote in news:39cbe663.0406151233.12a75385
@posting.google.com:

<SNIP>
of Java, because I do not know what e.g. the following lines mean
<Snip>
public boolean isLeaf(Object node) { return ((File)node).isFile();
}

public int getChildCount(Object parent) {
String[] children = ((File)parent).list();
if (children == null) return 0;
return children.length;
}
</Snip>
particular the notation "((File)node)".

<SNIP>

This is a fairly basic concept in Java; you need to learn this!

This is known as a cast. See http://mindprod.com/jgloss/cast.html
for a further explanation. (Thanks, Roedy!) Be sure to follow up the four
references (in red at the bottom of the article).

In the case of "(File)node", "node" had better be of actual type "File" (or
of a subclass of "File"). If it is, then it will have an "isFile()" method
(either from "File" or overridden by the subtype). If "node" is NOT of
type "File" or a subclass of it, then an exception "ClassCastException"
will occur.

--
Ian Shef
These are my personal opinions and not those of my employer.
Jul 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.