Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

ex. 8.23 Describe the difference between passing a parameter of a primitive type

ID: 3652785 • Letter: E

Question

ex. 8.23 Describe the difference between passing a parameter of a primitive type and passing a parameter of a reference type. Show the output of the following program: public class Test { public static void main(String[] args){ Count myCount = new Count(); int times = 0; for(int i=0; i<100; i++){ increment(myCount, times); System.out.println("count is "+myCount.count); System.out.println("times is "+times ); } public static void increment(Count c; int times){ c.count++: times++; } public class Count{ public int count; public Count(int c){ count = c; } public Count(){ count = 1; } }

Explanation / Answer

If u pass a primitive as a parameter then, what ever changes done to that variable will be reflected only within the function. Reference will reflect if any changes had been done in the function to the outside source. Reference means address of the variable will be passed. Pass by value means: Copy of your value will be passed.