Page 1 of 1
[HD Pg 311, Sec. 11.3.3 - the-instanceof-operator]
Posted: Fri Feb 01, 2019 2:17 pm
by OCAJO1
I have a somewhat an off-the-wall question.
Just like using instanceof operator to determine if an instance belongs to an object, does java provide or is there a neat code to check to see what type of variable has been accepted by a method. For example, say a method accepts all types of numbers as input. Now for whatever reason one needs to determine if the number that came in was a byte, an integer, a float, etc.
Thanks
Re: [HD Pg 311, Sec. 11.3.3 - the-instanceof-operator]
Posted: Fri Feb 01, 2019 7:31 pm
by admin
Assuming that you are talking about primitives, no, that is not possible because primitives are mere values. Once this value gets assigned to a variable, it has no trace of where it came from. So, when you assign an int value to a double variable, the int value becomes a double value.
Re: [HD Pg 311, Sec. 11.3.3 - the-instanceof-operator]
Posted: Fri Mar 22, 2019 6:33 pm
by OCAJO1
Given the 3 ways to invoke the removeseed() method, not withstanding any requirements to keep a reference such as m, is one more efficient than others?
Code: Select all
public void crush(Fruit f){
if(f instanceof Mango){
new Mango().removeseeds();
//or
Mango m = (Mango) f;
m.removeseeds();
//or
((Mango) f).removeseeds();
System.out.println("crushing mango...");
}else if (f instanceof Apple)
System.out.println("crushing Apple...");
else
System.out.println("crushing some other fruit...");
}
Thanks
Re: [HD Pg 311, Sec. 11.3.3 - the-instanceof-operator]
Posted: Fri Mar 22, 2019 8:43 pm
by admin
Neither of 2 and 3 is more efficient than the other.
But 1 i.e.new Mango().removeseeds() is illogical. You should know why by now. If not, I will have doubts about your OOP understanding.
Re: [HD Pg 311, Sec. 11.3.3 - the-instanceof-operator]
Posted: Sat Mar 23, 2019 1:10 pm
by OCAJO1
Wow I think I am developing some sort of intuition about OO concepts.
I was going to ask why would JVM not throw an exception when calling a Mango method from crush() method using a new Mango object, while it is aware that its parameter is a copy (value) of a reference of type Fruit (superclass of Mango). But I figured that kind of question was getting old (since I've asked it a couple of times about other things in the past), and it must be one of those cases that has other uses. I guess in this case one of those uses would be, if there were a requirement that achieving it would require to instantiate a new Mango() object to do something different with it than that of the object pointed to by (Mango) f.
But if there is more to it than that - I appreciate any further OOP 101 hints in this regard.
Re: [HD Pg 311, Sec. 11.3.3 - the-instanceof-operator]
Posted: Sun Oct 13, 2019 9:21 pm
by Username987654
Just like the cast operator, the instanceof operator is also used only sparingly in professionally written code. Usage of instanceof reflects a bad design and if you feel the need to use instanceof operator in your code too often, you should think about redesigning your application.
What is the threshold and/or rule-of-thumb on "too often" for instanceof and casting? In other words, for code in an existing (production) application or perhaps in the process of initial design, what "metrics" are recommended to determine if a (partial or complete) redesign is in order? (I'm open to other [Enthuware] books that may give this more examination.)
Re: [HD Pg 311, Sec. 11.3.3 - the-instanceof-operator]
Posted: Sun Oct 13, 2019 10:47 pm
by admin
I am sorry but I am not aware of any book that mentions any metrics about the usage of instanceof but I can tell you my rule of thumb.
If a method is supposed to work with just one specific class and uses instanceof to check whether an object is an instance of that class, then that is fine. For example, the equals method. In equals method, it is ok to use instanceof to check whether the passed reference is of correct type.
But if a method uses instanceof to identify several types and do different things depending on the type, then that it not ok. The class/method should be redesigned now or sometime later.
HTH,
Paul.
Re: [HD Pg 311, Sec. 11.3.3 - the-instanceof-operator]
Posted: Sun Oct 13, 2019 11:27 pm
by Username987654
Thanks Paul.