Using Primitive Data Type Variables as Parameters

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.
  1. public class PrimitiveType
  2. {
  3. public static void main (String[] args)
  4. {
  5. int number = 6;
  6.  
  7. System.out.println("before calling "
  8. + "the method funcPrimFormalParam number = " + number);
  9.  
  10. funcPrimFormalParam(number);
  11.  
  12. System.out.println("after calling"
  13. + "the method funcPrimFormalParam number = " + number);
  14. }
  15. public static void funcPrimFormalParam(int num)
  16. {
  17. System.out.println("In the method funcPrimFormalParam, before "
  18. + "changing, num = " + num);
  19.  
  20. num = 15;
  21.  
  22. System.out.println("the mehod funcPrimFormalParam, after changing, num = " + num);
  23.  
  24.  
  25. }
  26. }
Sample Run:
  1. Before calling the method funcPrimFormalParam, number = 6
  2. In the method funcPrimFormalParam, before changing, num = 6
  3. In the method funcPrimFormalParam, after changing, num = 15
  4. 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
  1. System.out.println("before calling "
  2. + "the method funcPrimFormalParam number = " + number);
outputs the value number before calling the method funcPrimFormalParam. The statement funcPrimFormalParam(number);calls the method funcPrimFormalParam. The value of the variable number is passed to the formal parameter num. The statement
  1. System.out.println("In the method funcPrimFormalParam, before "
  2. + "changing, num = " + num);
outputs the value of num before changing its value. The statement 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
  1. System.out.println("after calling"
  2. + "the method funcPrimFormalParam number = " + number);
outputs the value of number after calling the method funcPrimFormalParam.

Add new comment