Java method

Learning log

What is a method

A collection of multiple processes and named as one process.

Benefits of using the method

-Since it is described in functional units, the range of correction can be limited. ・ The visibility of the program will be improved and it will be easier to understand the whole. -Working efficiency is improved by combining the same processing into one method.

Method definition


public static return type method name(argument){
Specific processing to be executed when the method is called
//The return type describes the data type of the value of the result of processing in the method. If the processing result is a numerical value, int,If it is a character string, it will be a String.

//Method call
Method name(argument); //The return value is not available.

What is an argument

You can pass a value from the caller when calling the method.


public static void main(String[] arg) {
  hello("Yamamoto"); //helloメソッドを呼び出している。Yamamotoという文字列のデータを(Actual argument)Is passed to the hello method.

public static void hello(String name) { //When calling a method, Yamamoto assigns it to the Ring type variable name as a formal argument.
  System.out.println(name + "Hi,"); //The data assigned to the variable name is output here.
//Execution result
Mr. Yamamoto, Hello
Suzuki, Hello
Kitamura, Hello//Becomes

//When there are multiple arguments
public static void main(String[] args) {
  add(100,20); //Execution result x+ y = 120
  add(200,50); //Execution result x+ y = 250
//Add method that accepts multiple values
public static void add(int x, int y) {
  int ans = x + y;
  System.out.println(x + "+" + y + "=" + ans);

What is the return value?

The value (data) returned from the called method to the calling method is called the return value or return value.


return Return value; //This will allow you to return a value to the calling method.
//This return value is the execution result of the method.

Recommended Posts

Java method
java (method)
Java method
[Java] method
[Java] method
Java8 method reference
[Java] forEach method
java8 method reference
[Java] Random method
[Java] split method
JAVA DB connection method
Java learning 2 (learning calculation method)
Java learning memo (method)
About Java method binding
[Java ~ Method ~] Study memo (5)
About method splitting (Java)
Studying Java 8 (see method)
Java programming (class method)
[Java] Basic method notes
[Java] New Thread generation method (2)
Java GC method determination conditions
Java Silver Study Method Memo
Create a java method [Memo] [java11]
Java test code method collection
[Java Silver] About equals method
[Java] Timer processing implementation method
[Java] Random number generation method (Random)
Java methods and method overloads
Benefits of Java static method
[Java Silver] Array generation method
[Java] New Thread generation method (1)
Java learning (0)
Studying Java ―― 3
[Java] array
[Java] Annotation
[Java] Module
Java array
Java tips, tips
Java methods
java (constructor)
to_i method
Java array
[Java] ArrayDeque
java (override)
[Java] Object-oriented syntax --class method / argument
Java Day 2018
java (array)
Java static
Java serialization
JAVA paid
Automatic photo resizing method in Java
getRequestDispatcher () method
Java (set)
java shellsort
merge method
Studying Java -5
java reflexes
java (interface)