[Java] Basic method notes

Simple method definition

The minimum required definition is as follows. Here, the main method calls the hello method and is used.

--Caller: main method --Callee: hello method

↑ Sometimes I say this.

public class Main{

	//Main method definition
 	public static void main(String[] args){

	//hello method definition
	public static void hello(){
		System.out.println("Hello World!");

Of course, method calls can be nested.

	//Main method definition
	public static void main(String[] args){

	//methodA definition
	public static void methodA(){
		System.out.println("this is methodA");

	//methodB definition
	public static void methodB(){
		System.out.println("this is methodB");

Use of arguments

Arguments can be set in the method definition statement. It seems that the argument described by the callee is ** formal argument **.

	//Main method definition
	public static void main(String[] args){

	//printName method definition
	public static void printName(String name){
		System.out.println( name + "Hi,");

When using multiple arguments, overloading can be used. In this case, the number, type, and order of the arguments may be different. It is also said that the ** signature ** should be different.

	//Main method definition
	public static void main(String[] args){
		System.out.println("The argument is 2:"+ov(10,20));
		System.out.println("The argument is 3:"+ov(10,20,30));
		System.out.println("The argument is 4:"+ov(10,20,30,40));
		System.out.println("Arguments can be characters:"+ov("takahashi"));

	//ov method definition
	public static int ov(int p, int q){ //Formal argument 2
		int ans = p *q;
		return ans;
	public static int ov(int p, int q, int r){ //Formal argument 3
		int ans = p * q - r;
		return ans;
	public static int ov(int p, int q, int r, int s){ //Formal argument 4
		int ans = p * q - r * s;
		return ans;
	public static String ov(String name){ //
		String ans = name;
		return ans;

By the way, the main method itself is also a method without exception, so if you type a command line argument in the terminal, you can add that information to the program.

For example at the terminal

java Main 24 year old student

If you type, in the main method of the Main.java file

public static void main (String[] args)

The elements 24 years old, student are added to this String type array ʻargs. ʻArgs [0] == 24 years old, ʻargs [1] == student`.

Use of return value

You can pass the value to the main method by setting the return value in the method declaration statement. Here, the return value of int type is returned to the main method with return.

	//main method definition
	public static void main(String[] args){
		System.out.println("The product of 100 and 200 is"+ret(100,200));

	//ret method definition
	public static int ret(int x, int y){
		int ans = x * y;
		return ans;

Precautions when using arrays

When passing an array as an argument to a method normally, it looks like this. Here, it is sufficient to set the argument type to ʻint-> ʻint [] when defining the printArray method.

	//main method definition
	//Create an array here
	public static void main(String[] args){
		int[] array={10,20,30,40};

	//definition of printArray method
	public static void printArray(int[] x){
		for(int value: x){

Of course, an array can also be used as the return value. In that case, set the return value of the makeArray method definition statement to void-> ʻint []`. In this program, the makeArray method determines the number of elements according to the value based on the value specified in the main method, and creates an array as 1,2,3, .... It is returned to the main method as a return value.

	//main method definition
	public static void main(String[] args){
		int[] newArray= makeArray(5);
		for(int value: newArray){

	//definition of makeArray method
	//Create an array here
	public static int[] makeArray(int size){
		int[] x = new int[size];
		for(int i=1; i<x.length; i++){
			x[i] = i;
		return x;

Pass by value and by reference

When exchanging arguments and return values between methods normally, it can be said that the values themselves are exchanged (** pass by value, call by value ). However, when exchanging an array as an argument / return value between methods, the address in memory is passed instead of the value itself ( pass by reference, call by reference **). Therefore, the array created by the main method

int[] array = {1,2,3,4} //main method

As an argument to another method, then within that method

array[0]=10; //Called method

If you substitute something like that, the value in memory will change. Therefore, in the main method


If you hit something like that, the returned value will be 10 instead of 1. That the array can be rewritten as 10,2,3,4.

P.S### A variable to which an address in memory is assigned, not the value itself, is called a ** reference type **. For non-array types, ** class type ** is included in the reference type.

Sample code

Here is an example of rewriting an array by passing by reference.

	//main method definition
	public static void main(String[] args){
		int[] array={1,2,3,4};
		for (int value: aray){

	//incArray method definition
	public static void incArray(int[] x){
		for(int i=0; i<x.length; i++){

The returned array should be 2,3,4,5.

Reference book

[Introduction to Java 2nd Edition] (https://www.amazon.co.jp/dp/B00MIM1KFC/ref=dp-kindle-redirect?_encoding=UTF8&btkr=1) Chapter 5 Pp.170-202, 336.

Recommended Posts

[Java] Basic method notes
Java method
java (method)
java notes
Java method
[Java] method
[Java] method
[Java] Basic types and instruction notes
Java basic grammar
Java basic grammar
[Java] Array notes
Java8 method reference
Java basic knowledge 1
[Java] Basic structure
[Servlet] Basic method
Java basic grammar
Java serialization notes
java8 method reference
Java exercises [Basic]
[Java] split method
Concurrency Method in Java with basic example
[Java] Stream Collectors notes
Java formatted output [Notes]
JAVA DB connection method
Java learning 2 (learning calculation method)
Java learning memo (method)
[Java] Data type ①-Basic type
About Java method binding
[Java ~ Method ~] Study memo (5)
[Java] Control syntax notes
Ruby Thread # [] = method notes
About method splitting (Java)
Studying Java 8 (see method)
Java basic date manipulation
Java basic naming conventions
Java programming (class method)
Java learning memo (basic)
Maven basic study notes
Java basic data types
Basic Java OOps concepts
Java basic learning content 7 (exception)
[Java] New Thread generation method (2)
Basic Authentication with Java 11 HttpClient
Java basic syntax + α trap
Try scraping using java [Notes]
Java GC method determination conditions
[Java] Basic statement for beginners
Basic usage notes for Jackson
Java basic learning content 5 (modifier)
Java Collections Framework Review Notes
Create a java method [Memo] [java11]
[Java] Thymeleaf Basic (Spring Boot)
Java test code method collection
Implement Basic authentication in Java
Basic knowledge in method definition
[Java Silver] About equals method
[Java] Random number generation method (Random)
[Ruby] Notes on gets method
Benefits of Java static method
[Java Silver] Array generation method
Java Basic Learning Content 8 (Java API)