Question:
Recursion in Java?
Lo
2014-07-15 13:44:49 UTC
I'm having a very hard time trying to understand recursion. I've asked my teacher and looked in the book but I honestly have no idea what to do. I was given the code below and asked: Give the comment identifier containing the constructor that is executed in each of the following declarations:
(i) CC one = new CC(5,6);
(ii) CC two = new CC();
(iii) CC three = new CC(2, 8, 3.5);

I honestly don't even know where to begin with this. Would the first answer be CC(5,6) and the second answer just be CC(5).... I honestly don't know. Can someone help me understand recursion!? Thank you in advanced!


class CC {
private int u;
private int v;
private double w;
public CC() {} // CC1
public CC(int a) {} //CC2
public CC(int a, int b) {} //CC3
public CC(int a, int b, double d) {} //CC4
}
Four answers:
?
2014-07-15 13:59:38 UTC
Nothing in your question implies recursion.



The question has to do with what constructor is called, which has nothing to do with recursion.



Each of the declarations are standalone as in the previous on has no bearing on it.



What is going on is that three objects are being created from the CC class and the addresses of those classes are assigned to the appropriate reference.



It is pretty straightforward, match the arguments passed to the applicable constructor in CC.



CC one = new CC(5,6); matches with CC3 because that constructor takes 2 integers as arguments.



The previous declarations have no bearing on the current one. So CC two = new CC(); just calls the appropriate constructor, creates an object(which sets private variables to their defaults-0,0,0.0) and passes the address of that object to the reference two.



The object created with CC(5,6) is completely separate from CC();
_Object
2014-07-15 14:11:01 UTC
There's no recursion, this is called _overload resolution_. Referentially Transparent gave you a good explanation.



Recursion is the concept where a function calls itself.
husoski
2014-07-15 14:07:45 UTC
That's not recursion...it's "overloading". Just match the types of the arguments in the new CC(...) expressions to the parameter types to pick a constructor. That's what Java does.



So in (i) Java sees two int arguments to new CC, and picks CC3 because that's the constructor that accepts two int parameters. You should be able to handle the rest from there.



Also, don't confuse "overloading" with "overriding". That's as common as "expression" vs. "equation" confusion in math.



overloading: Giving two or more meanings for the name of a constructor or method, each with a different number of parameters and/or different types.



overriding: Redefining a method with a new implementation, hiding the old version.



Also, recursion is when a method directly or indirectly calls itself.
Lo
2014-07-15 15:01:20 UTC
Thanks so much guys! That's how much I don't know about this subject! I really appreciate y'alls help!


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...