Re: enumeration - follow up


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Re: enumeration - follow up

  1. #1
    Mark Guest

    Re: enumeration - follow up


    Paul:

    Thanks for your clear response! The question to ask next is... why, if it
    was a String in the hashtable do you need to do a "class cast"? By putting
    and pulling the data into and outof the Hashtable does it somehow alter it?
    In the code, which tries to pull it out of the Hashtable, it is being assigned
    to a String variable so why do you need to cast it?

    Thanks again,

    Mark

    "Paul Clapham" <pclapham@core-mark.com> wrote:
    >Yes, that's exactly what it's saying. The Java name for this is "class
    >casting", and there is a bunch of rules for how it works. You can't just
    >cast any class to any other class. So the programmer who wrote this code
    >would have known that the objects in the hashtable were strings. Since

    the
    >hashtable and enumeration are generic and widely usable, they can only deal
    >with Objects. And since all classes descend from Object, any object can

    be
    >cast into an Object variable.
    >
    >So the phrase "(String)buildingZone.get(buildingKey)" means: "Get the
    >Object from the Hashtable buildingZone that belongs to the key buildingKey.
    >And I know it was a String when I put it there, so cast it into a String
    >variable."
    >
    >But if it wasn't a String but some other class of Object, say a File or

    an
    >Integer, this will fail and a ClassCastException will be thrown at the
    >program.
    >
    >Mark <msorteberg@inspec.com> wrote in message news:394a37d2@news.devx.com...
    >>
    >> Here is the syntax in question:
    >>
    >> for (int startYear=initYear; startYear<initYear+3; startYear++) {
    >> for (Enumeration e=buildingZone.keys(); e.hasMoreElements(); ) {
    >>
    >> String buildingKey = (String)e.nextElement();
    >> String zone = (String)buildingZone.get(buildingKey);
    >>
    >> generateBuildingSummaryReport(startYear, buildingKey, zone);
    >> }
    >> }
    >>
    >> I think I understand what hashtables are and that enumeration provides

    a
    >> way to loop through a hashtable (in this case buildingZone is a

    >hashtable).
    >> nextElement() is the way enumeration goes through the keys located within
    >> buildingZone. If I've got it pegged this far, then the question is, "Why
    >> is (String) in front of e.nextElement() and why is (String) in front of

    >buildingZone.get(buildingKey)?
    >> Is this saying that whatever e.nextElement() finds we want to make it

    >into
    >> a string variable? I am confused as to what (String) is being used for

    >here.
    >> Thanks in advance for you help.

    >
    >



  2. #2
    Paul Clapham Guest

    Re: enumeration - follow up

    When you stored it in the Hashtable, you stored an Object. It's still a
    String at heart but the Hashtable doesn't know that. When you retrieved it
    from the Hashtable, you used the get() method, which returns an Object. You
    may know it's a String, but the Java compiler doesn't know that at this
    point. So you have to tell it that. Hashtable was designed this way to
    have maximum flexibility, so we didn't have to create one Hashtable-like
    class for Strings and another for Files and so on.

    Java has specific rules on when you can assign an object of one type to a
    variable of another type. In particular, it doesn't always allow you to
    just assign x=y, if x and y are different types. You can assign x=y if y's
    class is a subclass of x's -- as, for example, String is a subclass of
    Object so you can assign a String to and Object variable. But when you want
    to do it the other way around, you must cast the assignment. There are
    other rules for primitive classes (integer, float, etc.) and for interfaces.
    (Those rules are there so that Java can implement the OO concept of
    polymorphism.)

    Mark <msorteberg@inspec.com> wrote in message
    news:394a50b9$1@news.devx.com...
    >
    > Paul:
    >
    > Thanks for your clear response! The question to ask next is... why, if it
    > was a String in the hashtable do you need to do a "class cast"? By

    putting
    > and pulling the data into and outof the Hashtable does it somehow alter

    it?
    > In the code, which tries to pull it out of the Hashtable, it is being

    assigned
    > to a String variable so why do you need to cast it?
    >
    > Thanks again,
    >
    > Mark
    >
    > "Paul Clapham" <pclapham@core-mark.com> wrote:
    > >Yes, that's exactly what it's saying. The Java name for this is "class
    > >casting", and there is a bunch of rules for how it works. You can't just
    > >cast any class to any other class. So the programmer who wrote this code
    > >would have known that the objects in the hashtable were strings. Since

    > the
    > >hashtable and enumeration are generic and widely usable, they can only

    deal
    > >with Objects. And since all classes descend from Object, any object can

    > be
    > >cast into an Object variable.
    > >
    > >So the phrase "(String)buildingZone.get(buildingKey)" means: "Get the
    > >Object from the Hashtable buildingZone that belongs to the key

    buildingKey.
    > >And I know it was a String when I put it there, so cast it into a String
    > >variable."
    > >
    > >But if it wasn't a String but some other class of Object, say a File or

    > an
    > >Integer, this will fail and a ClassCastException will be thrown at the
    > >program.
    > >
    > >Mark <msorteberg@inspec.com> wrote in message

    news:394a37d2@news.devx.com...
    > >>
    > >> Here is the syntax in question:
    > >>
    > >> for (int startYear=initYear; startYear<initYear+3; startYear++) {
    > >> for (Enumeration e=buildingZone.keys(); e.hasMoreElements(); ) {
    > >>
    > >> String buildingKey = (String)e.nextElement();
    > >> String zone = (String)buildingZone.get(buildingKey);
    > >>
    > >> generateBuildingSummaryReport(startYear, buildingKey, zone);
    > >> }
    > >> }
    > >>
    > >> I think I understand what hashtables are and that enumeration provides

    > a
    > >> way to loop through a hashtable (in this case buildingZone is a

    > >hashtable).
    > >> nextElement() is the way enumeration goes through the keys located

    within
    > >> buildingZone. If I've got it pegged this far, then the question is,

    "Why
    > >> is (String) in front of e.nextElement() and why is (String) in front of

    > >buildingZone.get(buildingKey)?
    > >> Is this saying that whatever e.nextElement() finds we want to make it

    > >into
    > >> a string variable? I am confused as to what (String) is being used for

    > >here.
    > >> Thanks in advance for you help.

    > >
    > >

    >




  3. #3
    John Guest

    Re: enumeration - follow up


    Mark,

    The answer to your most recent question is that Enumeration.nextElement()
    is declared to return Object. The Java compiler won't accept assignment
    of the return from this function to a more specific type (e.g. String) without
    casting.

    In the case of String, that isn't completely true. Object, which as Paul
    said is the base type for all objects in Java, has a method called toString().
    It can be overridden for any class (since all classes inherit from Object)
    so that you can customize how any particular class is converted to a string
    (although in many cases this doesn't make any sense). But the upshot is
    that any object can be converted to a String.

    John

    "Mark" <msorteberg@inspec.com> wrote:
    >
    >Paul:
    >
    >Thanks for your clear response! The question to ask next is... why, if

    it
    >was a String in the hashtable do you need to do a "class cast"? By putting
    >and pulling the data into and outof the Hashtable does it somehow alter

    it?
    > In the code, which tries to pull it out of the Hashtable, it is being assigned
    >to a String variable so why do you need to cast it?
    >
    >Thanks again,
    >
    >Mark
    >
    >"Paul Clapham" <pclapham@core-mark.com> wrote:
    >>Yes, that's exactly what it's saying. The Java name for this is "class
    >>casting", and there is a bunch of rules for how it works. You can't just
    >>cast any class to any other class. So the programmer who wrote this code
    >>would have known that the objects in the hashtable were strings. Since

    >the
    >>hashtable and enumeration are generic and widely usable, they can only

    deal
    >>with Objects. And since all classes descend from Object, any object can

    >be
    >>cast into an Object variable.
    >>
    >>So the phrase "(String)buildingZone.get(buildingKey)" means: "Get the
    >>Object from the Hashtable buildingZone that belongs to the key buildingKey.
    >>And I know it was a String when I put it there, so cast it into a String
    >>variable."
    >>
    >>But if it wasn't a String but some other class of Object, say a File or

    >an
    >>Integer, this will fail and a ClassCastException will be thrown at the
    >>program.
    >>
    >>Mark <msorteberg@inspec.com> wrote in message news:394a37d2@news.devx.com...
    >>>
    >>> Here is the syntax in question:
    >>>
    >>> for (int startYear=initYear; startYear<initYear+3; startYear++) {
    >>> for (Enumeration e=buildingZone.keys(); e.hasMoreElements(); ) {
    >>>
    >>> String buildingKey = (String)e.nextElement();
    >>> String zone = (String)buildingZone.get(buildingKey);
    >>>
    >>> generateBuildingSummaryReport(startYear, buildingKey, zone);
    >>> }
    >>> }
    >>>
    >>> I think I understand what hashtables are and that enumeration provides

    >a
    >>> way to loop through a hashtable (in this case buildingZone is a

    >>hashtable).
    >>> nextElement() is the way enumeration goes through the keys located within
    >>> buildingZone. If I've got it pegged this far, then the question is,

    "Why
    >>> is (String) in front of e.nextElement() and why is (String) in front

    of
    >>buildingZone.get(buildingKey)?
    >>> Is this saying that whatever e.nextElement() finds we want to make it

    >>into
    >>> a string variable? I am confused as to what (String) is being used for

    >>here.
    >>> Thanks in advance for you help.

    >>
    >>

    >



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center