Try Java return value

agenda##

--Introduction --Level that the poster understands --Try using it with code --Explain in the processing procedure -(Conclusion) What is the return value?

Introduction

This article is just as unclear as I was a programming novice, as I was supposed to finally understand the return values that I couldn't understand at all after reading the description! This is an article that I will explain for those who say. Since I am not studying by reading books, the basic knowledge of the poster is low. (* Since you pointed out in the comments, I am making corrections) I would appreciate it if you could let me know in the comments about the points that are wrong in my recognition. Also, I don't understand the parable! Those who say that will apologize first. I'm sorry.

Level that the poster understands

This is the range that I currently understand while learning Java. In other words, I don't know anything else, so I don't know if it's an applied expression. (Excuse)

--Basic type --Method meaning --Types of basic variables --Basic operator --Basic conditional branching --Repeat processing --Array

Try using it with code

First of all, I will explain with the code. This time I will explain with this code.

qiita.jv


public class ReturnValue{
   public static void main(String[] arg){
      int a = 1;
      int b = 5;
      int c = a*b;

      System.out.println(a + "x" + b + "=" + c);
   }
}

The result of executing this code is

1x5=5

It will be. At this point this program can be run without any problems. Let's take a look at the program, assuming you want to introduce arguments and return values into this program and get the same result.

First, create a method different from the main method to set the arguments. Let's call it the back method here. And since we want to process the calculation formula (a * b) outside with an argument, specify the back method for the value of the variable c.

qiita.jv(2)


public class ReturnValue{
   public static void main(String[] arg){
      int a = 1;
      int b = 5;
      int c = back();

      System.out.println(a + "x" + b + "=" + c);
   }
   static int back(){

   }
}

Since the variable int is used, I created a back method with int type.

Execution result 1x5=5 In order to issue, it is necessary to redefine the variable in the back method and write the calculation formula.

qiita.jv(3)


public class ReturnValue{
   public static void main(String[] arg){
      int a = 1;
      int b = 5;
      int c = back();
      System.out.println(a + "x" + b + "=" + c);
   }

   static int back(){
   int a = 1;
   int b = 5;
   int c = a*b;
   }
}

If you try to process it with the back method, it will be like this. However, since the same description is described in another method and the return value must be set for the int type, an error will occur in the first place. Describe return to return to return value to return processing result. In addition, I would like to inherit the values of variables a and b to the back method using arguments. Then

qiita.jv(4)


public class ReturnValue{
   public static void main(String[] arg){
      int a = 1;
      int b = 5;
      int c = back(a,b);
      System.out.println(a + "x" + b + "=" + c);
   }

   static int back(int x,int y){
   int a =x*y;
   return(a);
   }
}

It will be like this. First, describe the variables a and b that you want to inherit in the () of the return value "int c = back ();" that you want to return the processing result of the back method. In this case, there are two variables, so separate them with ",". Then, in the back method that is the inheritance destination, prepare a device that accepts the inherited values and variables a and b. This time, I prepared variables x and y. At this point, the values of variables a and b of the main method are inherited by variables x and y of the back method. In other words

int a = 1 = int x int b = 5 = int y

It means that. Redefine int a with the values of variables x and y that inherited the value of the main method. And finally, the value of the variable a, which is the processing result so far, is returned up to "int c". Since it is returned (a) to int c, the processing result of the processing result 1 * 5 in the back method is assigned to the variable c.

int c = 5

It will be.

System.out.println(a + "x" + b + "=" + c);

Is processed by the main method, so the value of the variable a redefined by the back method is not reflected. Therefore, by substituting the assigned value and character string as usual,

1x5=5

And you get the same result as an expression without arguments and return values.

Explain in the processing procedure

Up to this point, I explained with the code, but I think that some people may not understand well when going out and back. I was confused when I heard such an explanation.

Therefore, I would like to organize and explain the series of steps here. I'm sorry many times, but please take a look at the final completed code.

qiita.jv(4)


public class ReturnValue{
   public static void main(String[] arg){
      int a = 1;
      int b = 5;
      //Step 1
      int c = back(a,b);
   //Step 2

   //Step 6
      System.out.println(a + "x" + b + "=" + c);
   //Step 7
   }

   static int back(int x,int y){
  //Step 3
   int a =x*y;
  //Step 4
   return(a);
  //Step 5
   }
}

This time, the processing procedure is described in the comments so that you can see the code. (It's easy to understand!) Now, I would like to explain how the process is performed again according to the procedure.

Step 1 It defines variables a and b.

Step 2 The value to be inherited by the back method is defined in the variable c.

Step 3 From the main method, the values of variables a and b could be inherited by the vessels of the back method, variables x and y. This gives int x = 1 and int y = 5.

Step 4 Redefine the variable a. The result of the formula for the variables x and y inherited in step 3 is redefined in the variable a.

Step 5 By return, the variable a redefined by the back method that is the processing result is returned to the caller.

Step 6 The variable a redefined in the back method is assigned to the variable c in the main method. As a result, int c = 1 * 5 = 5.

Step 7 The value assigned to each and the character string are concatenated and output.

The above is the processing procedure of the explained code.

(Conclusion) What is the return value?

In other words, the return value represents the processing result. (And explained in the comments.) In this case, it's the calculation result. Since the return value reflects the result of the processing called calculation, the return value = the processing result.

I'm sorry that I didn't understand at all because I felt like I understood it, I couldn't understand the explanation, and I added extra elements.

Recommended Posts

Try Java return value
Try Java 8 Stream
Roughly try Java 9
[Java] Get multiple values from one return value
Try using RocksDB in Java
9 Corresponds to the return value
Try DB connection with Java
Try scraping using java [Notes]
Try calling JavaScript in Java
Try developing Spresense in Java (1)
Try functional type in Java! ①
Try gRPC with Java, Maven
[Java ~ Boolean value ~] Study memo (2)
Try implementing GraphQL server in Java
[Java] Graphics transparency (alpha value) change
Java arguments, return values and overloads
Try running Selenuim 3.141.59 in eclipse (java)
Try using Redis with Java (jar)
Try an If expression in Java
Java pass by value and pass by reference
Java
[Java] Try to implement using generics
Try bidirectional communication with gRPC Java
Try running AWS X-Ray in Java
Try to implement Yubaba in Java
Try using IBM Java method tracing
Java
Try Eclipse 4.7 Oxygen New 30+ / Java 10 var!
Try using Hyperledger Iroha's Java SDK
[Java] Where did you try using java?
Declare a method that has a Java return value with the return value data type
Return value when using Java No. 1 Optional.ifPresent that was useful in business
Interface Try to make Java problem TypeScript 7-3
Java: Try implementing your own comma-separated formatter
Try using Java framework Nablarch [Web application]
Try to solve Project Euler in Java
Try to implement n-ary addition in Java
Reverse Key from Value in Java Map
Let's try WebSocket with Java and javascript!
Study Java Try using Scanner or Map
Try running a Kubernetes Job from Java
Try various Java Stream API methods (now)
Ruby methods return the last evaluated value
Try making a calculator app in Java
Use name attribute, value attribute, form tag [Java]
Try using JobScheduler's REST-API --Java RestClient implementation--
Try managing Java libraries with AWS CodeArtifact
Try calling Nim from Java via JNI
Try using the Wii remote with Java