Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2007

Tip: Looking for answers? Try searching our database.

Object retval = (Object)bindings.get(var); // why not OK?

Thread view: 
metaperl - 23 Aug 2007 14:05 GMT
I'm wondering why I cannot simply cast the return result of .get() to
satisfy the compiler expectation that the method return an object.
Instead I have to  assign the result of .get() to a variable and then
return that.

package redick;

import java.util.*;
import java.util.Iterator;

public class Environment {
    /*
    * map from a variable name to an Object - which may be a datum or a
    * procedure
    */
    public Map<String,Object> bindings = new HashMap<String, Object>();
    public Environment parent;

    public void put(String var, Object value) {
        bindings.put(var, value);
    }
    public Object get(String var) {

        Object retval = bindings.get(var); // cast not enough
        /* Cannot comment this section out */
        if (retval == null) {
            return (Object)null;
        } else {
            return retval;
        }
        /* End required section */
    }

    public String toString() { return bindings.toString(); }

}
Lew - 23 Aug 2007 14:15 GMT
> I'm wondering why I cannot simply cast the return result of .get() to
> satisfy the compiler expectation that the method return an object.
> Instead I have to  assign the result of .get() to a variable and then
> return that.

You never have to upcast, so a cast to Object is never needed.

> package redick;
>
> import java.util.*;
> import java.util.Iterator;
>
> public class Environment {

Please do not embed TABs in Usenet posts.

>     /*
>     * map from a variable name to an Object - which may be a datum or a
[quoted text clipped - 9 lines]
>
>         Object retval = bindings.get(var); // cast not enough

There is no cast here, and indeed, none is needed.

>         /* Cannot comment this section out */
>         if (retval == null) {
[quoted text clipped - 8 lines]
>
> }

You could simply "return retval;" or even "return bindings.get(var);"

public Object get(String var)
{
  return bindings.get(var);
}

You haven't shown us a complete (i.e., "SSCCE") example of the erroneous code
that gives you trouble, or what the error is.  I don't see what your
difficulty could have been.

There are times when cover methods delegate with one-line bodies, but
sometimes that's an indicator that you can use the Map directly instead of
wrapping it.

Signature

Lew

Daniel Pitts - 23 Aug 2007 16:48 GMT
> I'm wondering why I cannot simply cast the return result of .get() to
> satisfy the compiler expectation that the method return an object.
[quoted text clipped - 32 lines]
>
> }

Works for me, what're your error message?
Mark Space - 23 Aug 2007 20:12 GMT
> I'm wondering why I cannot simply cast the return result of .get() to
> satisfy the compiler expectation that the method return an object.
[quoted text clipped - 27 lines]
>         }
>         /* End required section */
        return retval;
>     }
>
>     public String toString() { return bindings.toString(); }
>
> }

As Lew mentioned, you just need to add the "return retval;" (see above)
to the code snippet.  Then you should be able to comment out the
indicated section.

Just one of the late-night programmer-didn't-see-it things, I guess... ^_^


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.