http://www.javadrive.jp/start/method/index5.html This site is for your reference.
In here,
class JSample5_1 {
public static void main(String args[]){
intkekka;
kekka=bai(9);
System.out.println(kekka);
kekka=bai(5);
System.out.println(kekka);
}
private static intbai(intn){
return n*2;
}
}
I don't understand the part of return n*2
because x2
is in bai(intn)
and x2
, is it correct that it is (bai=x2)
?
Then kekka=bai(9)
contains x2
and 18
,
Do you mean 10
with x2
in kekka=bai(5)
?
Also, it says void
at the beginning, but
If I write return
, does it mean that return
is applied and the first void
is canceled?
In a typical programming language, operations (+
or -
or *
or /
) are evaluated immediately, and n contains a
value of 9
and is evaluated immediately.The value is returned to the caller (main) and substituted for the variable kekka.Please note that multiplication is not substituted, but the result is 18.
n does not contain multiple
such as x2
.
Look at the example below.
intn=3;
pass kekka=bai(n);//3
There is no point in changing n because n=4;//kekka already has 6 substituted.
Changing the value of n after kekka contains 6 is slow.
Here is another example:
kekka=bai(2*3);
This works as follows:
In the process of programming in the future, along with the word delay evaluation
, there will be a system that does not evaluate immediately, but this case does not apply.Please be careful.
Gift
interface Calculator {
int calc(intlhs, intrhs);
}
classOpPlus implements Calculator {
public int calc(intlhs, intrhs) {
return lhs+rhs;
}
}
classOpMinus implements Calculator {
public int calc(intlhs, intrhs) {
return lhs-rhs;
}
}
class OpMultimplementations Calculator {
public int calc(intlhs, intrhs) {
return lhs*rhs;
}
}
classOpDivisions Calculator {
public int calc(intlhs, intrhs) {
return lhs /rhs;
}
}
public class JSample5_1 {
private static Calculator getCalculator(charop)throws RuntimeException {
switch(op){
case '+':
return new OpPlus();
case '-':
return new OpMinus();
case '*':
return new OpMult();
case '/':
return new OpDiv();
default:
through new RuntimeException ("Invalid operator");
}
}
public static void main(String[]args) {
// get an additional class as a return value
Calculator c=getCalculator('+');
// c has an additional class, so if you do calc, you get 1+2.
// A result 3 is returned after execution 3 is returned.
System.out.println(c.calc(1,2));
}
}
If you run the above source and change the '+'
part of the main to another operator, you will understand how to implement the method using the original return value.
The function object, which is a way to return the function itself, can be realized in Java 8, but it is probably better not to do it now that you are in the study stage.
Java
I recommend you to read the introduction to the language, etc., but
The proposed source contains one class and two methods for that class.
//Start Class JSample5_1 Declaration
class JSample5_1 {
// Starting the main method
public static void main(String args[]){
intkekka;
kekka=bai(9);
System.out.println(kekka);
kekka=bai(5);
System.out.println(kekka);
}
// Starting the bai method
private static intbai(intn){
return n*2;
}
}
The definition of each method has the following meanings:
// Static (static) public method
// Return value: None (void)
// arguments:array of type String
public static void main(String args[]){
// ...
}
// static private method
// return value:int type
// Arguments—Accessible by the name int type `n`
private static intbai(intn){
// ...
}
Also, at the beginning, it says void (do you mean disable the return value?) but
Do you mean that if I write a return, the return will be applied and the first void will be canceled?
The return value is void
only for the main
method.Therefore, there is no return (return
) in the main
method.
The bai
method returns the int
type.
Although you seem to understand the behavior of the bai
method, if you call the method, such as bai(5)
, the method is defined in the bai
method to access the arguments passed under the name n
.
Therefore, when you call bai(5)
, 5*2
is calculated and 10
is returned to the caller (return
).
The n
portion contains the number passed.
For bai(9)
, n
contains 9
For bai(5)
, n
contains 5
.
Returns n*2
, so
bai(9)
is replaced by 9*2
and
bai(5)
can be replaced by 5*2
.
In other words, it's practically
kekka=bai(9);
is the same as kekka=9*2;
and
kekka=bai(5);
can be considered the same as kekka=5*2;
.
void
in return value specification means "no return value" rather than "disable return value" or "return void".
In this case, the void specification is valid only for the main method, and the return value specification in bai is int, so writing a return does not negate the specification.
return void;
//void is an error in the first place
return0;
// Error trying to return value, return;
is OK
If you write in the main method block, the compilation should fail.
© 2024 OneMinuteCode. All rights reserved.