[HD Pg 311, Sec. 11.3.3 - the-instanceof-operator]

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
OCAJO1
Posts: 221
Joined: Mon Nov 26, 2018 2:43 pm
Contact:

[HD Pg 311, Sec. 11.3.3 - the-instanceof-operator]

Post 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

admin
Site Admin
Posts: 10043
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: [HD Pg 311, Sec. 11.3.3 - the-instanceof-operator]

Post 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.
If you like our products and services, please help us by posting your review here.

OCAJO1
Posts: 221
Joined: Mon Nov 26, 2018 2:43 pm
Contact:

Re: [HD Pg 311, Sec. 11.3.3 - the-instanceof-operator]

Post 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

admin
Site Admin
Posts: 10043
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: [HD Pg 311, Sec. 11.3.3 - the-instanceof-operator]

Post 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.
If you like our products and services, please help us by posting your review here.

OCAJO1
Posts: 221
Joined: Mon Nov 26, 2018 2:43 pm
Contact:

Re: [HD Pg 311, Sec. 11.3.3 - the-instanceof-operator]

Post 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.

Username987654
Posts: 95
Joined: Sat Dec 26, 2015 6:37 pm
Contact:

Re: [HD Pg 311, Sec. 11.3.3 - the-instanceof-operator]

Post 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.)

admin
Site Admin
Posts: 10043
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: [HD Pg 311, Sec. 11.3.3 - the-instanceof-operator]

Post 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.
If you like our products and services, please help us by posting your review here.


Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 40 guests