This is the 15th day of my participation in the August Text Challenge.More challenges in August

Hello, I’m Grey Ape! A super bug writing program ape!

With adhere to create technology, with the fingertips knock the future! Like many of you, I am a “creator” on the Java path. Also want to rely on technology to change the future, change the world! Because we believe that every keystroke can make life smarter and the world more interesting! In this column, Java Core Interview Guide, document our Day 5 to prepare for our dreams!

An interview technique point every day, today to record with you in Java interview in the method and recursion of the frequently met questions and solutions.

Parameter passing in Java by value or by reference?

In Java, there is only value passing and no reference passing, so the only way to pass parameters in Java is by value passing.

Follow-up: how is the specific transmission under different circumstances?

When the type of an argument is a primitive data type, the value of the argument is passed, so the argument cannot be modified. When the argument type is an object, a reference to the object is passed. In this case, the object referenced by the argument can be modified, but the argument cannot reference the new object.

What are the components of a method’s signature?

The method signature is composed of the method name and parameter list, modifiers and return values are not part of the method signature, method signature is generally used for method overloading, method overloading must be different method signature.

When will a compilation error occur when using method overloading?

A compilation error occurs when an ambiguous call occurs. If a method call has multiple possible matches, and the compiler cannot determine which method best matches, it is called an ambiguous call.

What are the advantages and disadvantages of using recursive algorithms?

Advantages: The advantages of using recursive algorithm are simple and easy to understand the code, disadvantages: large time and space consumption, each function call needs to allocate space in the memory stack, stack operation may also need time, so time and space complexity is high.

If there is overlap among the sub-problems, the time complexity may be too high due to double calculation without memorization.

Because stack space is limited, if there are too many recursive calls, the call stack may overflow.

5. Follow-up: What can be done to solve the shortcomings of recursion?

Tail recursion is one way to do it, and memorization is another way to avoid double-counting, and iteration is another way to do it.

Vi. Follow-up: What is tail recursion?

A recursive call is tail recursion when it is the last statement executed in a method and its return value is not part of the expression. Tail-recursion, which returns directly to the original caller without passing through the intermediate caller, is important because most modern compilers take advantage of this feature to automatically generate optimized code. Using tail recursion instead of regular recursion can lead to significant improvements in both time and space.

Can we change the general recursion of Fibonacci sequence to tail recursion?

An ordinary recursive implementation of the Fibonacci sequence:

/** * Fibonacci sequence ordinary recursive implementation *@param index
	 * @return* /
	public static long fibonacci(long index) {
		if (index <= 1) {
			return index;
		} else {
			return fibonacci(index - 1) + fibonacci(index - 2); }}Copy the code

Tail recursive implementation of Fibonacci sequence:

/** * Fibonacci tail recursion implementation *@param index
	 * @return* /
	public static long fibonacciTailRecursion(long index) {
		return fibonacciTailRecursion(index, 0.1);
	}

	/** * recursive body *@paramIndex Cycle *@paramPresent Current value *@paramNext The next value *@return* /
	public static long fibonacciTailRecursion(long index, int present, int next) {
		if (index == 0) {
			return present;
		} else {
			return fibonacciTailRecursion(index - 1, next, present + next); }}Copy the code

The daily summary

Today, in the core interview examination point about method recursive call, we need to grasp the characteristics of method overloading is that method signature must be different, understand the basic idea of recursive call and recursive algorithm optimization strategy.

I’m going to share these questions with you about methods and recursion. If you have any other questions, please feel free to comment in the comments section and I will ask you to do soSummary to the articleWelcome to join us in the comments section! Friends can also add my friends in the left side to discuss learning!

I am aAsh little apeAnd we’ll see you next time!