Using Primitive Data Type Variables as Parameters
Submitted by GeePee on Monday, May 18, 2015 - 21:55.
The following java program application is about Primitive Data Type Variables as Parameters. When a method is called, the value of the actual parameter is copied into the corresponding formal Parameter. If a formal parameter is a variable of a primitive data type, then after copying the value of the actual parameter, there is no connection between the formal parameter and the actual parameter. I will be using the JCreator IDE in developing the program.
To start in this tutorial, first open the JCreator IDE, click new and paste the following code.
Sample Run:
outputs the value number before calling the method outputs the value of num before changing its value.
The statement outputs the value of number after calling the method
- public class PrimitiveType
- {
- {
- int number = 6;
- + "the method funcPrimFormalParam number = " + number);
- funcPrimFormalParam(number);
- + "the method funcPrimFormalParam number = " + number);
- }
- public static void funcPrimFormalParam(int num)
- {
- + "changing, num = " + num);
- num = 15;
- }
- }
Before calling the method funcPrimFormalParam, number = 6
In the method funcPrimFormalParam, before changing, num = 6
In the method funcPrimFormalParam, after changing, num = 15
After calling the method funcPrimFormalParam, number = 6
The preceding program works as follows:
The execution begins at the method main
.
The segment int number = 6;
declares and initializes the int variable number.
The statement - + "the method funcPrimFormalParam number = " + number);
funcPrimFormalParam
.
The statement funcPrimFormalParam(number);
calls the method funcPrimFormalParam
. The value of the variable number is passed to the formal parameter num.
The statement - + "changing, num = " + num);
num = 15;
changes the value of num to 15.
The statement System.out.println("the mehod funcPrimFormalParam, after changing, num = " + num);
outputs the value of num. After this statement executes, the method funcPrimFormalParam
exits and control goes back to the method main.
The statement- + "the method funcPrimFormalParam number = " + number);
funcPrimFormalParam
.
Add new comment
- 12 views