• 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
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

passing primitive and reference data types into functions

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys. I am trying to get a handle on understanding the passing of primitive and reference data type in to functions. It seems to me that passing primitive data into a function is analogous to passing a reference data. I have written some code to help me explain myself.

Basically, I created a Persons class which contains, among  other things, a redefinition of the toString method that was inherited from the Object class. In main, I have two functions, function1(double) and function2(Object).
When int number is passed into function1, I understand number is implicitly promoted to a double to match the signature of function1. Is it right to say that the same effect happens when a derived object Persons is passed into function2? An alternative way of looking it is that since Persons extends Object, every Persons instance is an instance of Object.
 
Marshal
Posts: 70654
288
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remove the cast. You don't need it, since all objects have toString(). And remember that polymorphism means you get the correct overriding version for the runtime type of the object.
You would have to check the Java® Language Specification, but as far as I can remember, there is no “promotion” of reference types.

. . . since Persons extends Object, every Persons instance is an instance of Object.

Correct, but call the class Person (singular) not Persons. Reserve plural forms for utility classes.
 
ken luong
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Campbell Ritchie! You're absolutely right. Not only the cast is not needed, it's wrong to have it in the definition of saySomething(Object). Suppose another object of type different than that of Persons is passed in, my original definition would call the toString() method  of Persons every time, which is not the desired result.

I picked up learning Java a few weeks ago and have been part of this site for the same amount of time. In this short period of time, I've gained valuable information, particularly from you. Thanks very much again!
 
Sheriff
Posts: 15936
265
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

ken luong wrote:Suppose another object of type different than that of Persons is passed in, my original definition would call the toString() method  of Persons every time, which is not the desired result.


No, you would get a ClassCastException. The Java runtime will always check the actual type and if it finds that the object cannot be cast to Persons, it will throw the exception.
 
ken luong
Greenhorn
Posts: 10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:

           "No, you would get a ClassCastException. The Java runtime will always check the actual type and if it finds that the object cannot be cast to Persons, it will throw the exception."

You are right Junilu Lacar. Any object that isn't of a class which is derived from Persons would cause a ClassCastException to be thrown. I hope I thought it through this time😁. Thank you for your correction!
 
Campbell Ritchie
Marshal
Posts: 70654
288
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

ken luong wrote:Thanks Campbell Ritchie! . . .

That's a pleasure

my original definition would call the toString() method  of Persons every time . . .

More likely, you will be unable to complete the cast, and the program will fail with an exception.
Remember that toString() is an instance method and is therefore implicitly subject to polymorphism. [Only instance methods can be polymorphic.] The runtime starts looking for a definition of the method in the Class<?> object corresponding to the Object you are using. If there is no such method definition, it goes to the superclass and keeps going until it finds such a method. Since it is declared in Object, it “knows” it will eventually find such a method.
 
If I had asked people what they wanted, they would have said faster horses - Ford. Tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic