Java Note 5-modifiers, overloads, recursion, arrays

Source: Internet
Author: User

Definition of a method
Modifier return type method name (argument list) throws exception type list {
Method body
}
==
How to design a method:
Case:
Please design the method according to the following business separately
1. is a leap year judged based on the given years?
public boolean isleapyear (int year) {
}

2. Find the sum of two floating-point numbers
Public double Add (double A, double b) {

}
3. Finding the factorial of a given integer
Public long factor (int n) {

}
4. Binary string form for a given integer
Public String tobinary (int i) {

}
5. Ask for the greatest common divisor of two integers
public int gcd (int a, int b) {

}
6. Print out the perfect number in the specified range
public void Printperfectnumber (int start, int end) {

}

--------------------------------------
1. Overloading of methods [overload]
Defined:
In the same class, methods that have the same method name but different argument lists form the overloads of the method
Characteristics:
Method name is the same
Parameter list is different
1). Different number of parameters
2). Different parameter types
3). On the basis of 2), the order is different

Role:
1). For the caller of the method, there is no need to write a different method name for the same business method
2). The overloaded method must have the same business.

Such as:
public void Xxx {
public int Add (int a, int b) {return a + B;}

Public double Add (double A, double b) {return a + B;}

public double Add (int b, int a) {return a + B;} does not constitute an overload

public double Add (int a, double b) {return a + B;}

Public double Add (double b, int a) {return a + B;}

}

For the JVM, how to locate a method that relies on the method name and the parameter type.
So, the method table for the above class is as follows:
Add (Int,int)
Add (double,double)
Add (int,double)
Add (Double,int)
For overloaded methods, the JVM chooses when a call occurs:
1). First exact match
Such as:
X.add (5, 9); Invoke Add (Int,int)
X.add (5.0,9.0); Invoke Add (double,double)
2). If the exact match is not true, follow the nearest up-up matching principle for the type.
Such as:
int i = 19;
float F = 18;
X.add (i, f); Invoke Add (int,double)
BYTE B1 = 18;
byte B2 = 19;
X.add (B1,B2); Invoke Add (Int,int);
----


2. Recursive method [Recursive]
Definition refers to the method of invoking itself directly or indirectly, that is, the recursive method.

The role of recursive methods
Used to solve some special problems, such as: Hanoi, Fipona array, etc.

Conditions for using recursion
1). Be sure to have a regular
2). Be sure to have an exit condition

Such as:
To find the factorial of an integer
Using a circular approach
Public long factor (int n) {
Long result = 1L;
for (int i=n;i>=1;i--) {
Result *= i;
}
return result;
}
Using recursive thinking
5! = 5 * 4!
4! = 4 * 3!
3! = 3 * 2!
2! = 2 * 1!
At this point, we know 1! equals 1.
Use the Mathematical table method:
F (N) = n * F (n-1)
Code
Public long factor (int n) {
1. Specify Exit Criteria
if (n = = 1) {return 1L;}
2. Recursion
return n * factor (n-1);
}

--
Fipona, it's a series of numbers.
1 1 2 3 5 8 13 21 34 55 89 ....
Design a method to find the Fipona sequence value at the specified position
public long Fibo (int pos) {

}

-----------------------------
Invocation of the method
First, the method is the member of the class [member]
Second, there is no static in the modifier of the method
Because of the static method, it can be called by the class name. Method Name.
Without a static method, the object must be called later by creating an object. method
==
About real-participation parameters
When a method is called, the value of the argument is copied to the parameter.

===============================
arrays [Array]
An array, which is itself an object data type.
It is used to hold a set of data with the same data type.

Grammar:
data type [] array name;
Or
data type [] Array name = new data type [length];
=============================
Data type array name [];
Or
Data type array name [] = new data type [length];

Note: The array must be specified in length at initialization, and once the length is determined, it cannot be changed.
The space of the array is contiguous.

Features of the array
1). Array elements share the same number of group names
2). Use the subscript to access the element, subscript from 0 to length-1. If the subscript is in this range,
An exception to the array subscript bounds will occur.

Like what:
To record an individual's age, use
int age = 19;
What if you want to record the age of 5 people?
int age1 = 18;
int age2 = 17;
int age3 = 20;
int age4 = 15;
int age5 = 16;
Here we use an array to record the words, then we just need to define an integral type array to
int[] arr = new INT[5];
Arr[0] = 18;
ARR[1] = 17;
ARR[2] = 20;
ARR[3] = 15;
ARR[4] = 16;


Note:
Although an array is an object type, an array element can be any type.
Like what:
Int[] intarr; Declares an array of integers
String[] SARR; Declares an array of type string

--
Once an array is initialized, the element has a default value, even if the element is not assigned a value.
which
Int,byte,long,short=> 0
Double,float = 0.0
Boolean = False
Char=> ' \u0000 '
Object type = NULL

==
How do I assign a value to an array element?
1). Press the subscript to assign a value.
Such as:
int[] arr = new INT[3];
ARR[0] = 100;
ARR[1] = 150;
ARR[2] = 200;

2). When initializing an array, assign a value to the element directly with curly braces
Grammar:
data type [] Array name = new data type []{value 1, value 2,...., value n};
Or
data type [] Array name = {value 1, value 2,...., value n};
Note: The 2nd type of grammar cannot be written separately.

Such as:
int[] arr = new int[]{100, 150, 200};
Or
Int[] arr2 = {199,32,234,23432,4234,23423};

Int[] ARR3;
ARR3 = {1,4,3,34,4,34}; Error
ARR3 = new int[]{1,4,3,34,4,34}; Ok

3). If the element value has a certain regularity, you can assign a value by looping
Such as:
int[] arr = new INT[3];
for (int i=0;i<arr.length;i++) {
Arr[i] = + 50*i;
}
//

Practice:

Job 1:
Design a method that returns an array of Fipona-filled array values, based on the given length.

Job 2:
Using recursive thought to find out the greatest common divisor of any two integers
public int gcd (int a, int b) {

}
Job 3:
Design a method to find the number of daffodils within 1000 [daffodil]
Narcissus number refers to an n-digit (n>=3) equal to the sum of the N powers of all its digits.
Such as:
157 = 1^3 + 5^3 + 7^3

Java Note 5-modifiers, overloads, recursion, arrays

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.