• Post Reply Bookmark Topic Watch Topic
  • New Topic

why this happens?  RSS feed

 
Brian Smith
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi folks,

why objRef.length() creates a problem? could someone explain me please?
thanks.
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
orjRef.length() is a problem becuase Java is a strongly-typed programming language; that is, the methods that can be legally called on an object depend on the type of the reference to the object, not the type of the object itself. That is, if you have a reference to an Object, you can only call methods that are defined by the Object class (.equals(), .toString(), etc.) Since .length() is not a member of the Object class, you cannot use it whenever the reference to the object is an Object reference.
As an aside, other programming languages are weakly typed; that is, they do all of their type checking at run time. So in a language like Objective-C, it is legal to say:

Allowing this syntax results in a). slower code (since everything has to be checked at run-time) and b). potentiality for "dumb errors" that the compiler does not catch. Therefore, Java does not allow this syntax. (You can get the same semantics by using reflection, but the syntax does not support it.)
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!