• 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

what is the purpose of the type associated with a reference?

 
Ranch Hand
Posts: 63
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Loosely speaking, as I understand it, when I write a statement such as:

myType x;

this means that x is a reference to objects of type, "myType" and no memory has been set aside for an instance of this object.

I think it's true that x also has the starting memory address of this object.

What I feel uncertain about is if x is telling the JVM(?) where it can find the starting address of this object, why do we care about the type that x is?
i.e., if we assign x to an object totally unrelated to this type then the compiler would complain - correct? If so, how come, since all x is doing is
telling the JVM(?) where to find your object (via the address it contains).

I suppose all I am really questioning is why a reference has a type associated with it if all the reference does is help point you to the object?

Thanks,
Dan
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

If the compiler doesn't know that dog is a Dog, how would it know if it has a bark() method?
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dan Bromberg wrote:L . . . What I feel uncertain about is if x is telling the JVM(?) where it can find the starting address of this object, why do we care about the type that x is? . . .

If you didn't have strong typing, you could writeThe JVM never makes that sort of mistake because the javac tool never allows such code to get anywhere near the JVM.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is the difference between static and dynamic typing. Languages with dynamic static typing - like Java - allow the compiler to check for certain types of error. Like calling a method that doesn't exist on that object, or passing an object of one type as an argument when a method expects an object of another type.

The idea behind this is that errors that can be caught at compile time are usually much easier to diagnose and fix than errors that only happen at run time. Especially if the error is in code that doesn't get run every time, and especially if you don't have a complete set of automatic tests that will catch these errors.

In order to do this sort of static checking, you need to tell the compiler what the type of each variable is. There are some statically typed languages that try to infer the type from how the variable is used, but that isn't easy with a language with inheritance, and Java doesn't do that.
 
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
Are you sure Java® is dynamically typed? I thought it was static.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Are you sure Java® is dynamically typed? I thought it was static.


Sorry, slip of the fingers and/or brain .
 
Dan Bromberg
Ranch Hand
Posts: 63
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Campbell,
Matthew:
Well said. Goodness knows I need all the protection I can get from shooting myself in the foot!
Thanks for your replies.
Dan
 
Eliminate 95% of the weeds in your lawn by mowing 3 inches or higher. Then plant tiny ads:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic