• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

Could anyone clarify pls: Why there is no converion when casting reference types?

 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When casting is applied to primitives, the primitive is converted to the left-side type. But the same is not done for reference types. Why is this? I would appreciate it if any of you can explain.
<code>
class Base {
String s = "Field, Base";
public void iPrint() {
System.out.println("Method, Base");
}
}
class Derived extends Base{
String s = "Field, Derived";
String e = " Extra Field in Derived";
public void iPrint() {
System.out.println("Method, Derived" + s);
}
}
public class CastTest {
public static void main(String s[]) {
int iv = 1;
float fv = 2.3f;
// casting primitives, conversion takes place
iv = (int) fv;
System.out.println("Integer: " + iv);
Derived d = new Derived();
// casting references, Why no conversion?
Base b = (Base) d;
b.iPrint();
d.iPrint();
}
}
</code>
Thanks.

------------------

 
Trailboss
Posts: 22997
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you saying that your
Base b = (Base) d ;
does not work?
I'm pretty sure that even
Base b = d ;
would work.
 
Thandapani Saravanan
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree. But when an object is casted to a super-class object, should'nt it be converted? If you see the object after casting it is same as it was before.
That is when we say,
Object o = (Object) new String("test")
o becomes a String. Why not just Object?
But when we say,
float f = 9.8;
int i = (int) f;
Then i becomes an integer. (not float anymore)
I think now you know what is my doubt.
Thanks a lot.
 
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Uh-oh.
Reference is just an address of an object in memory. It's up to JVM to store information about what object it is accessing.
So, if you have say:
<pre>
String s = "a";
Object o = s;
</pre>
You have two references to the same object.
So, you don't "make" Object to be a String or vise a versa.
Primitive types are not stored as references, but as values. How do you expect to have integer to be float ? They have different bit sequence implementation.
Uhh... I even feel hard to explain clear enough...
Okay, better to say : int variable can carry only integer value, so it just can't hold 3.2f.

------------------
With best of best regards, Pawel S. Veselov ( aka Black Angel )
 
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My take on this would be that you have to look at the potential advantages and disadvantages of the two approaches. If casting of object references actually converted the objects, information would be lost (casting a String to an Object loses the contents of the string as well as the fact that it is a String) so I find it hard to imagine when I might want to do it.
Preserving the object, on the other hand allows "polymorphism" to work, and very useful it is too.
Consider the class Vector. I use it a lot, because it can hold any objects. If I want a place to store a lot of Strings, I can use a Vector. If I want a place to store some visible Components, some of which may be Buttons, Panels and so on, I still use a Vector.
Notice that Java includes "object" versions of the primitive types (Integer, Float etc.). casting these has the same behaviour as casting other object references. The provision of these classes is an indication of how important and valuable this mechanism is.
I think I could live without conversion casting between primitives. In fact, I could still write perfectly effective programs without primitive data types at all. The same can't be said of objects and polymorphism.
 
Without subsidies, chem-ag food costs four times more than organic. Or this tiny ad:
create, convert, edit or print DOC and DOCX in Java
https://products.aspose.com/words/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!