Learning Java. I will make a note so that I can look back on it for review. It's almost my study memo. Please do not excessive expectations.
A method is a part to which some processing is assigned. When you execute a Java file, the main method is automatically executed first. When the processing of the main method is divided into methods, the main method gives instructions to each method and The structure is such that each method executes an individual process.
--Method flow
class Main {
public static void main(String[] args) { //①
hello(); //②
}
public static void hello() {
System.out.println("Hello World"); //③
}
}
① The main method is called
↓
(2) The hello method is called in the main method.
↓
③ In the hello method
System.out.println ("Hello World") is executed
--Method definition
To use a method, you first need to define it. How to define is as follows
How to define
class Main {
public static void method name(){
//Process to be executed;
}
}
Also, the method is defined in the class. Taking the above code as an example, It is defined in the block ({}) of the Main class. If you do not define it in the class like this, an error will occur.
--Method call
To call the method, enter method name ();
.
Also, when calling a method, enter it in the main method.
Because, as I mentioned earlier in the flow,
In a structure where the main method gives instructions to each method and each method executes individual processing
Because it has become.
class Main {
public static void main(String[] args) {
hello(); //Method call
}
public static void hello() {
System.out.println("Hello World");
}
}
--Arguments
Arguments are like additional information given to a method. If you pass an argument with the method when you call it, you can use that value in the method.
To pass an argument to a method, you must first define a method that can accept the argument. To do so, specify the variable (dummy argument) that will be the box for receiving the arguments in the method definition part.
Then, specify a formal argument in () of "public static void method name ()". Data type specification and Variable definition must be specified for the formal arguments.
Specifying formal arguments
class Main {
public static void method name(Data type variable name){
//Process to be executed;
}
}
Example
class Main {
public static void study(String language){
//Process to be executed;
}
}
--Pass arguments to the method
To pass arguments to a method, call the method as method name (argument)
.
The passed argument is assigned to the variable specified by the method's formal argument,
That variable can be used in the processing of the method.
Example
class Main {
public static void main(String[] args) {
study("Java");
}
public static void study(String language){
System.out.println("now," + language + "I'm studying.");
}
}
Output result
I'm studying Java now.
--Multiple arguments
In order for the method to receive multiple arguments, define formal arguments separated by commas (,) . In addition, the arguments are called "first argument, second argument ..." in order from the left.
Specifying multiple formal parameters
class Main {
public static void method name(Data type variable name 1,Data type variable name 2){ //First argument, second argument in order from the left
//Process to be executed;
}
}
In fact, pass multiple arguments to the method. The formal parameters can be ordered arbitrarily, but when calling a method, You must pass the arguments in the order of the formal arguments.
So, if study ("Java", 2) and the order of passing arguments are reversed I get an error because I try to put an integer in the String type formal argument.
Example
class Main {
public static void main(String[] args) {
study(2, "Java");
}
public static void study(int month, String language){
System.out.println(month + "From a month ago" + language + "I'm studying.");
}
}
Output result
I've been studying Java for two months.
--Method return value
You may want to use the method processing result in the method caller. In such cases, let the method return a return value.
For a method with a return value, use return in the method, Change the void part of "public static void" that was standard so far to specified data type .
You can use return inside a method to return the value of return to the method caller. In addition, void, which has been a standard until now, means no return value . Therefore, specify void for methods that have no return value (no return) like this.
Method with return value
public static Return data type Method name(argument){
return Return value;
}
Example
class Main {
public static void main(String[] args) {
int total = add(2, 8); //The variable definition is "Variable name"=Can be assigned by "value"
System.out.println(total);
}
public static int add(int a, int b){
return a + b;
}
}
Since the return value of the add method is an integer, it becomes "public static int".
Output result
10
--Method overload
Basically, the method with the same name cannot be defined in principle. However, if the type and number of arguments are different , you can define a method with the same name. Defining a method with the same name in this way is called overloading.
Example
class Main {
public static void main(String[] args) {
study(); //①
study("Java"); //②
}
public static void study(){
System.out.println("I'm studying Java.");
}
public static void study(String language){
System.out.println("now," + language + "I'm studying.");
}
}
Output result
① I am studying Java.
② I'm studying Java now.
--Application that combines arguments and return values
Example
class Main {
public static void main(String[] args) {
printData(fullName("John", "Christopher", "Smith"), 175.5);
}
public static void printData(String name, double height) {
System.out.println("my name is" + name + "is");
System.out.println("How tall are you" + height + "cm");
}
public static String fullName(String firstName, String middleName ,String lastName) {
return firstName + " " + middleName + " " + lastName;
}
}
Output result
My name is John Christopher Smith
Height is 175.5 cm
--Return value of boolean value
The boolean data type is boolean . The isEven method created here checks if the argument value is even and returns true if it is even and false if it is odd.
Example
class Main {
public static void main(String[] args) {
int number = 4;
if(isEven(number)){
System.out.println("Is an even number");
} else{
System.out.println("Is odd");
}
}
public static boolean isEven(int a){
return a % 2 == 0; //If it is divisible by 2, it is an even number, so it returns true.
}
}
Output result
Is an even number
[Java ~ Variable definition, type conversion ~] Study memo [Java ~ False Value ~] Study Memo 2 [Java ~ Conditional branching / Iterative processing ~] Study memo 3 [Java ~ Array ~] Study Memo 4
Recommended Posts