So lets take this from the beginning ...
public class Variables {
public static void add1(int a) {
a = a + 1;
System.out.println("a = " + a);
}
public static void main(String[] args) {
int x = 1;
add1(x);
System.out.println("x = " + x);
}
}
could be rewritten ...
public class Variables {
public static void main(String[] args) {
int x = 1;
int a = x;
a = a + 1;
System.out.println("a = " + a);
System.out.println("x = " + x);
}
}
and run it would print ...
a = 2
x = 1
I've done the above to demonstrate that the method add1
has no effect on the variable x.
re: Question 1
Formal Parameter:
The identifier used in a method to stand for the value that is passed into the method
by a caller.
Actual Parameter:
The actual value that is passed into the method by a caller.
reference: http://chortle.ccsu.edu/CS151/Notes/chap34/ch34_3.html
As main
executes x
is declared, and it is then initialized to hold the value 1
.
Next add1
is called with the actual parameter 1
by main
, and since the method add1
has the formal parameter a
declared in its parameter list, it intializes a
to hold the value of 1
.
The add1
method then adds the value of 1
to the formal parameter a
assigning the resultant value back the to the formal parameter a
,
prints "a = 2
" to the standard out, and add1
returns.
The main
method then prints "x = 1
" to the standard out, and the program terminates.
re: Question 2
Value Method:
Will return a primitive or an object based on an input, or the program or object's state.
Action Method:
Will do something like print text to the screen, or perhaps display a window when called.
(Could return a boolean upon success or failure though)
Googling the phrase method composition isn't very enlightening is it.
"A Java Method is Composed of" ...
A Modifier or Modifiers:
Which define which and/or how Objects can call the Method.
public static void main(String[] args) {
// "public" and "static" are "Modifiers".
A Return Type:
Which states what the method will return.
public static void main(String[] args) {
// "void" is the "Return Type".
A Signature:
Which consists of "The Method's Name" (main) and "Parameter List" (args)
public static void main(String[] args) {
// "main(String[] args)" is "The Method Signature".
A Body:
In which it may declare local variables,
and which will contain the code which will be performed by the method.
{
int x = 1;
add1(x);
System.out.println("x = " + x);
}
// Everything between the "Curly Brackets" is "The Body".