• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

Object Casting Not Functioning Properly

 
Greenhorn
Posts: 16
Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why does my following method not cast properly? As far as I see, this should not create any casting issues.

       

I know that the following would work, however I feel uncomfortable that the top example does not.

     

Any pointers as to why the first example does not work would be very much appreciated my fellow developers I am casting string1 to a String, and it seems it is not casted?
 
Greenhorn
Posts: 21
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should create new object references for casting to work. In your example string1 and string2 variables still point to the same Object types as before casting:
 
Marshal
Posts: 16631
278
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Chris Avila wrote:Why does my following method not cast properly? As far as I see, this should not create any casting issues.
       


You might have seen the problem sooner had you not chosen names that reflected your knowledge of what the objects were that you were passing in to this method. Casting an Object to String will only work as long as the only kind of objects you pass to this method are String objects. Since the method declares its parameters as Object, there's really no guarantee that this method will always receive Strings. What happens when someone who doesn't know that this method is  supposed to be passed only String objects passes it some other type of object, thinking that it should be ok, since the method signature says it will take any Object reference? It will throw a ClassCastException! This is bad code and  it would have been more evident how bad it was if you had used different names instead:

The only thing I changed here are the names. It was misleading to use "stringX" with references that were really of type Object. Now it's clearer that you're trying to cast an Object to a String but then you're assigning that reference right back to an Object. Since an Object has no length() method, you should expect to get a compiler error on those lines that are marked with "// Issue".
 
Chris Avila
Greenhorn
Posts: 16
Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys! Appreciate the help.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic