Hello has been spam certified post of the previous array.
-A method is a part or something like a game technique (class is like status * just my feeling). -** It doesn't work unless it is written in the {} (block) of the class **. -By using a method, when you do the same process, you only have to call it without writing the same process, so it's easy. -Easy code visibility and management.
kane.java
public static void method name() {
Process to be executed;
}
kane.java
class Main{
public static void main(String[]args) {
banana() ; //()Is required.
}
public static void banana() {
System.out.println("yellow") ;
}
}
・ What is the return value? The value (data) returned from the called method to the calling method is called ** return value or return value **.
public static void main(String[] args) { }
-** When a java program is executed, it automatically starts from the main method. **. -The main method gives instructions to each method, and each supported method executes individual processing.
・ If you don't forget to write it, it's OK now.
· Something like additional information given to the method. -A value can be passed from the caller when calling a method. -The value can be used even in the method passed by it.
In order to define a method that can receive arguments, specify a variable (formal argument (Karihikisu)) that will be a box for receiving arguments in the method definition part, and put the formal argument in () of public static void banana (). specify. Formal arguments, like variable definitions, ** must specify a data type **.
kane.java
class Main{
public static void main(String[]args) {
banana("Red") ;
}
public static void banana(String color) { //Specify data type
System.out.println("The color of this banana" + color + "is.") ;
}
}
Even if there is no information to hand over, () is necessary, so be sure to enter it.
**-When passing multiple arguments **
kane.java
class Main{
public static void main(String[]args) {
banana("Philippines", 5) ;
}
public static void banana(String origin, int year) {
System.out.println("This banana" + origin + "Of production" + year + "It's a year.") ;
}
}
Execution result
This banana is from the Philippines for 5 years.//It's rotten
Recommended Posts