• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Downcasting from Object to String

 
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have code that retrieves an object and needs to cast it to an Integer. Is it possible to down cast in this manner?
 
Ted Gress
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So basically I want to do this:

Pool.java

Object getobject()
{
         Object t = new Object();
         return t;
}

Main.java

getFromPool()
{
         String myString = (String) getobject();
}

 
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please use code tags when posting code, that makes code much more readable. You can see how that looks in my posted snippet.

Anyway, when you tried below approach, what did you conclude from that?

 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Every object has a toString() method. You may use this.

How are you getting your Object instances? Why do you need to cast?
 
Ranch Hand
Posts: 67
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No you cannot do that from the code you wrote because the declared type of the reference variable and the actual type of the object are both Object.

An easy way to get clear about types and casting is this- an Object's type is like a promise of features on something you bought.

A String and any subclass of String promises to have a certain set of features. The features are: every feature Object has and some more special String features.

If I hand you an Object but I promised you a String, when you tried to use some feature on what I handed you, believing it's a String,  the whole thing would throw an exception. Why? Because I lied to you; I really handed you an Object , which has no special String features at all.

You cannot cause one type object to become another type merely by casting. That is what your program does.  

An object is born a certain type and it will always be that type and never any other.

The only little qualification to that is an object is not just its declared type, but is also has the type of any of its superclasses or interfaces it implements.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David Simkulette wrote:. . .  an object is not just its declared type, but is also has the type of any of its superclasses or interfaces it implements.

An object may also have a runtime type which is a subtype of its declared type.
 
Liutauras Vilda
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think Campbell with his shortest answer explained more than we did with our long ones.

Of course OP's question also isn't clear enough. Either OP's intention is to convert Object X to Object Y or cast reference variable, from Y to X while the actual object at run-time appears to be indeed X, so that is allowed, but to make actual Y to be actual X no.

I started mumbling here. OP, please clarify your question with a better example probably or a more examples so we'd see a pattern what you are actually asking.
 
David Simkulette
Ranch Hand
Posts: 67
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:An object may also have a runtime type which is a subtype of its declared type.



But only if that declared type is for some reason a supertype of its created type.

Essentially what you're refering to is   is "undeclaring" the type it is for some reason.

So I use reference type Object but put a String in it thus:

Object secretString= new String("Hello world");

Now what you said is true, but it scans or reads like we're telling him "an object's type can be both its superclass type and its subclass type" which is NOT true.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David Simkulette wrote:

Campbell Ritchie wrote:An object may also have a runtime type which is a subtype of its declared type.



But only if that declared type is for some reason a supertype of its created type. . . . .

Aren't those two things the same?

Also remember that, like subsets and supersets, every type is simultaneously a subtype of itself and a supertype of itself in many circumstances.
 
David Simkulette
Ranch Hand
Posts: 67
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We both know that but I'm afraid we have confused the poor OP.


OP :

Below is meant to be any old assignment stattement.:

Animal animal = new Moose();

It has 3 parts. People online do actually refer to these parts of an assignment statement as such and you should know them.

The L-value, or reference variable, part:

Animal animal

the assignment operator:

=

and the R-value or item-in-memory part:

new Moose();

In Java, once either an L-value or a R-value has been assigned a type, that type NEVER changes NO MATTER WHAT.

In our example, the l-value type is "Animal and any SUB-classes of Animal" .

In our example, the r-value is "Moose and any SUPER-classes of Moose".

You cannot assign to an l-value a type it is not.

Since Moose is a subclass of Animal (I am just telling you this now, but it should make sense.. a moose is a specialized kind-of animal) , you can do what we did, to wit:

Animal animal = new Moose();// ok animal, our l-value has type Animal and I am assigning to it a Moose which is a subclass of Animal


What you cannot do is this:

Moose moose = new Animal();// oh heck no. You can only assign to an l-value, in this case moose,  a variable or new object which is of the same type as that l-value, in this case a Moose or SUBclass of Moose. Animal is not any of those things !


Nor any form of this:

Animal moose = new Animal(); // ok...
Moose mooseToo = moose; // oh heck no. You can only assign to an l-value, in this case mooseToo, a variable or new object which is of the same type as that l-value, in this case a Moose or SUBclass of Moose. Animal is neither 9depite the variable name)



In your code you tried to (effectively) do this:

Object object = new Object();//ok...
Strring string = object// oh heck no. For the same reason as the previous example.









ou can't cast an instance of an object to a type it was not the
 
Ted Gress
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Would it be possible to do:

Object object = new String(); since String is a subclass of Object?
 
David Simkulette
Ranch Hand
Posts: 67
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ted Gress wrote:Would it be possible to do:

Object object = new String(); since String is a subclass of Object?



Sure. For just the reason you said. The resulting object would be casatable to a String and assignable to a String-and-its-superclasses-typed L-value, thus :

String string =(Stirng) object;

But without casting or reassigning to such an L-value, no method whose formal parameters call for a String is going to accept it as a String to a method invocation.  

It's great to clearly get these concepts clear in your mind so you don't always have to test things out on the compiler, (and can answer test questions !) but also, learning to see the compiler as a LittleLab where you can ask just these sorts of questions as hypotheses and get them confirmed and disconfirmed is a powerful habit of mind to develop and will give you a kind of programming intellectual independence later when your programming  questions get tougher nad no one actually knows the answer anymore.
 
Ted Gress
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok. So this is the part that confuses me. I guess it has to do with the "l" and "r" values.

Object object = new String() works because String is a subclass of object.
But....
Object object = new Object and then casting object to a string doesn't work even though String is still a subclass of Object
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ted Gress wrote:. . . Object object = new Object and then casting object to a string doesn't work even though String is still a subclass of Object

It should be obvious that, if you persuade that sort of code to compile, you will have problems with lines 3‑4. But what about line 5 and 7?There is no evidence that c is a Siamese cat, only an ordinary cat. Nor is there any evidence that the Animal is a cat of any kind.
That is the same situation as casting an Object to String. A String IS‑AN Object, but an Object IS NOT‑A String. Unless you have provided a String to that reference, you might get it past the compiler, but you will have problems at runtime. The compiler is programmed to believe the programmer but when the runtime finds out it has been lied to with (String)myObject, it will make its displeasure felt in no uncertain terms (an exception).If that code prints 49 the runtime will know that I was telling the truth about that cast, and you will know that I can't count letters
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ted Gress wrote:
Object object = new Object and then casting object to a string doesn't work even though String is still a subclass of Object



Casting doesn't change the object that sits on the heap, that is the object that has been physically created.
It just changes the type of the reference variable.

So in the above, you have created an Object object on the heap.
The reference for that object is assigned to the object variable.
When you try and cast that variable to a String, the JVM will look at the actual object it is referencing on the heap and see that it is not actually a String, and therefore cannot be cast as such.
 
David Simkulette
Ranch Hand
Posts: 67
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ted Gress wrote:
Object object = new Object and then casting object to a string doesn't work even though String is still a subclass of Object



It's what everyone is telling you. See my first post again:

David Simkulette wrote:You cannot cause one type object to become another type merely by casting. That is what your program does.  

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic