• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Dan Bromberg
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • 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
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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?
 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Mark post as helpful
  • send pies
  • 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.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • 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: 56593
172
  • Mark post as helpful
  • send pies
  • 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
  • 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: 52
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!