• 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
  • Liutauras Vilda
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Peter Rooke
  • Himai Minh
Bartenders:
  • Piet Souris
  • Mikalai Zaikin

Will class casting creats new object on the heap?

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If B extends A and you do:
A a = new B();
B b = (B)a;

will it creates two objects?
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer is no, an object is only really created when you use the new keyword. In your example 1 object is created and placed on the heap.
 
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, Jeudy has already explained it. Class casting never creates objects on heap it's the new keyword which is responsible for that. You could check that by executing the following code.


When Line 1 is executed the constructor of the Child class executes and displays the output child object however the same is not true for Line 2 which means the casting has not created another object on heap. Hope it helps.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you cast something, what it means is that you say to the compiler: "Compiler, I have a variable of type A here, but I want you to treat it as if it is of type B. (Because I know that the variable really refers to an object of type B)."

Casting does not create any objects or do any smart conversions.
 
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, only object will be created basically and i believe there will be now two reference variables on the stack
referring to the same object.

Cheers,
 
Ranch Hand
Posts: 537
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The main thing to remember for this is for what reason the casting is done and for whoom is it for the compiler or is it used during running the program.
 
Drove my Chevy to the levee but the levee was dry. A wrung this tiny ad and it was still dry.
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic