• 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
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Devaka Cooray
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Tim Holloway
  • Claude Moore
  • Stephan van Hulst
Bartenders:
  • Winston Gutkowski
  • Carey Brown
  • Frits Walraven

Question about difference between object type and reference type  RSS feed

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is  Question ID: 908907 of Kaplan





I don't understand the difference between object type and reference type in lines

 
Master Rancher
Posts: 1161
18
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's try a more clear example:

The refference type is Car, but the object type is Mercedes.
this wil only work of course if the object type extends the refference type.
an other example:
 
Marshal
Posts: 63447
206
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you are using the wrong names. If you look in the Java® Language Specification (=JLS), you will find out that the opposite of reference type is primitive type.

I think you meant to say declared type and runtime type.Here the declared type and runtime type are both the same: Car. I am not sure I would have a class Meredes, but you might have a class like this:-Now you can sayor even one suitable for after boozy Christmas parties‍ In all those cases, the declared type is Car, but the runtime type might be a Taxi; The last code snippet doesn't allow anybody to decide what type it is until runtime.
The javac tool tests the declared type of all references, and ensures that all objects assigned thereto are of that type (that type includes subtypes). So you can assign a Car or a Taxi to a reference declared Car. You can assign a Square, Rectangle or Rhombus to a Shape declared type. That means only methods declared in the types Car/Shape can be used. You cannot therefore writebecause there is no such method in the Car class. You can however cast the Car reference, and that will make me pay the £13.50, but I shall leave you to work out what might go wrong with this sort of code:-Look at line 23 in the code you posted.
I shall also let you work out why I have used the BigDecimal(double) constructor.
 
Campbell Ritchie
Marshal
Posts: 63447
206
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also work out the inheritance hierarchy in that code; it has a serious error in.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!