[JAVA] How to create a method

What is a method? A process that has been named so that it can be called at any time. For example, when I wanted to write "Hello World" 10 times ...

method.jave


System.out.println("Hello World");
System.out.println("Hello World");
System.out.println("Hello World");
System.out.println("Hello World");
System.out.println("Hello World");
System.out.println("Hello World");
System.out.println("Hello World");
System.out.println("Hello World");
System.out.println("Hello World");
System.out.println("Hello World");

It is troublesome to write. If you use the method, you can write as follows.

method.jave


void sayHelloWorld(int n){
  for(int i=1; i<=n; i++){
  System.out.println("Hello World");
  }
}
sayHelloWorld(10) 

Once you have created a method for that process, you can call it at any time.

How to create a method Basically, it's almost the same as when creating variables. There is a part that describes the argument and a part that describes the process. In the above example, (1) "void" specifies the type of the result to be displayed. (2) "sayHelloWorld" determines the name of the method (the name used when calling). ③ You can specify an argument with "(int n)". (If not specified, only "()" is written after the method name.) (4) Specify the process to be executed by the code inside "{}". ⑤ In "say Hello World (10)", the process created above is executed 10 times.

Return value In the case of the method introduced above, the processing is only character input. If you set it as a return value when you output the result in the contents of the method, you can use it when you connect the result of the process of that method to another process.

Of course, even if the type name is "void", it is possible to get the result calculated by "System.out.println ();". The return value is effective when you want to obtain the result of processing as "moving decimal point, integer value, boolean value" instead of "character string".

For example Suppose you want to get the automatically calculated result by entering the list price of a certain product and the discount rate (% off) of that product.

method2.jv


double calculationOfDiscountPrice(int price, double discountRate){
  double discountPrice = price - (price*discountRate/100);
  return discountPrice
}
calculationOfDiscountPrice(1000,10)

↓ 900

You can get the result like this.

Parameters In addition, it is described in the first line of "method2.jv". 「double calculationOfDiscountPrice(int price, double discountRate)」 The part inside () is called a parameter, and multiple settings can be made. (Variables that can be given in the method) 「calculationOfDiscountPrice(1000,10)」 The (1000,10) part of is called an "argument" and is a numerical value / character string to be included in the parameter.

Recommended Posts

How to create a method
[Java] How to create a folder
How to create a Maven repository for 2020
How to create docker-compose
[rails] How to create a partial template
How to create a database for H2 Database anywhere
[Rails] How to create a graph using lazy_high_charts
How to create pagination for a "kaminari" array
How to create a class that inherits class information
How to create a theme in Liferay 7 / DXP
[1st] How to create a Spring-MVC framework project
How to easily create a pull-down in Rails
[Rails] How to create a Twitter share button
How to create an application
How to leave a comment
Create a java method [Memo] [java11]
How to insert a video
How to create a convenient method that utilizes generics and functional interfaces
How to create a Java environment in just 3 seconds
[Rails] How to create a signed URL for CloudFront
How to create a JDBC URL (Oracle Database, Thin)
How to create a Spring Boot project in IntelliJ
[Spring Boot] How to create a project (for beginners)
How to create a data URI (base64) in Java
How to mock a super method call in PowerMock
How to create and execute method, Proc, Method class objects
[Apple Subscription Offer] How to create a promotional offer signature
How to use the link_to method
How to use the include? method
How to add columns to a table
Preparing to create a Rails application
How to make a Java container
[Java] How to use join method
How to sign a Minecraft MOD
[Ruby] How to use any? Method
How to write a ternary operator
[Swift] How to send a notification
How to make a splash screen
How to make a Maven project
Try to create a server-client app
How to use Ruby inject method
How to make a Java array
How to create a lightweight container image for Java apps
How to create a form to select a date from the calendar
How to create a placeholder part to use in the IN clause
Create a method to return the tax rate in Java
How to test a private method with RSpec for yourself
How to create a service builder portlet in Liferay 7 / DXP
How to create and launch a Dockerfile for Payara Micro
[Xcode] How to add a README.md file
How to execute a contract using web3j
How to sort a List using Comparator
How to create a jar file or war file using the jar command
How to create a registration / update function where the table crosses
[Java] How to compare with equals method
[Basic] How to write a Dockerfile Self-learning ②
How to insert a video in Rails
How to use submit method (Java Silver)
Method to search
[Rails] How to use the map method
How to add a new hash / array