Question

In: Computer Science

[6 marks] Java uses pass by value to pass parameters to a method. What are the...

[6 marks] Java uses pass by value to pass parameters to a method. What are the important differences between passing a value of variables of primitive data types and passing arrays? Use a specific example to explain

Solutions

Expert Solution

Differences between the passing value of variables of primitive data types and passing arrays:


-> While passing a primitive value of variables, you pass a copy of the value, and while passing arrays you pass a copy of the handle.
-> Passing value of variables of primitive data types always have value but in passing arrays can be null.
-> a value of variables of primitive data types is original while passing arrays value can be modified.

I will explain with the program:

Example :

import java.util.*;


class Example
{
public static void demoFunction(int arr[], int size)
{
System.out.println("Size is "+size);
arr=new int[size];
arr[0]=100;
arr[1]=200;
arr[2]=300;
System.out.println("Array elements: "+Arrays.toString(arr));
}
public static void main(String args[])
{
int size;//primitive
int arr[]={0}; //array
size=3;
demoFunction(arr, size);
}
}

Output:


Related Solutions

Java uses pass by value to pass parameters to a method. What are the important differences...
Java uses pass by value to pass parameters to a method. What are the important differences between passing a value of variables of primitive data types and passing arrays? Use a specific example to explain
What is a “void” method? What does “pass by value” mean? What is the scope of...
What is a “void” method? What does “pass by value” mean? What is the scope of a variable? What does it mean to say that a method is overloaded?
What is Last Pass? What is Java Language Applets?
What is Last Pass?What is Java Language Applets?What is Code Red?What is computer incident?
What is pass-by-reference? What is pass-by-value? What is a memory leak? What happens if the function...
What is pass-by-reference? What is pass-by-value? What is a memory leak? What happens if the function makes a change to that received array and why? Functions in C++ can return only one data item (e.g., variable) using the return statement and they also cannot return arrays. There is, however, another way to "return" more than one item from a function without using the return statement. Can you explain how and why? How are arrays represented in memory? What is the...
Part A) Java Programming Exercise #2: Write a method, remove, that takes three parameters: an array...
Part A) Java Programming Exercise #2: Write a method, remove, that takes three parameters: an array of integers, the length of the array, and an integer, say, removeItem. The method should find and delete the first occurrence of removeItem in the array. If the value does not exist or the array is empty, output an appropriate message. (Note that after deleting the element, the array size is reduced by 1.) Here you assume the array is not sorted. Do not...
art A) Java Programming Exercise #2: Write a method, remove, that takes three parameters: an array...
art A) Java Programming Exercise #2: Write a method, remove, that takes three parameters: an array of integers, the length of the array, and an integer, say, removeItem. The method should find and delete the first occurrence of removeItem in the array. If the value does not exist or the array is empty, output an appropriate message. (Note that after deleting the element, the array size is reduced by 1.) Here you assume the array is not sorted. Do not...
6. Crane Sales Company uses the retail inventory method to value its merchandise inventory. The following...
6. Crane Sales Company uses the retail inventory method to value its merchandise inventory. The following information is available for the current year:                                                                                             Cost                    Retail Beginning inventory                              $ 60,000               $ 90,000 Purchases                                                 380,000                 520,000 Freight-in                                                     5,000                       —   Net markups                                                   —                      17,000 Net markdowns                                              —                      20,000 Sales revenue                                                 —                    410,000 What is the cost of ending inventory using the conventional method?
Net Present Value Method, A method of analysis of proposed capital investments that uses present value...
Net Present Value Method, A method of analysis of proposed capital investments that uses present value concepts to compute the rate of return from the net cash flows expected from the investment.Internal Rate of Return Method, and Analysis The management of Quest Media Inc. is considering two capital investment projects. The estimated net cash flows from each project are as follows: Year Radio Station TV Station 1 $360,000 $760,000 2 360,000 760,000 3 360,000 760,000 4 360,000 760,000 Present Value...
Net Present Value Method, A method of analysis of proposed capital investments that uses present value...
Net Present Value Method, A method of analysis of proposed capital investments that uses present value concepts to compute the rate of return from the net cash flows expected from the investment.Internal Rate of Return Method, and Analysis The management of Quest Media Inc. is considering two capital investment projects. The estimated net cash flows from each project are as follows: Year Radio Station TV Station 1 $360,000 $760,000 2 360,000 760,000 3 360,000 760,000 4 360,000 760,000 Present Value...
In java please create a method that will insert a value after a specific node for...
In java please create a method that will insert a value after a specific node for a linked list. public void insertAfter(Node a, int newData){ }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT