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?
Use Java to: 1. Write a method with a Rectangle and a Point as parameters. Without...
Use Java to: 1. Write a method with a Rectangle and a Point as parameters. Without using methods from the Rectangle class, return true if the point is inside the rectangle and false otherwise. 2. Write a second method with the same functionality as Exercise 1. However, use a method from the Rectangle class this time. 3. Normally, the == operator cannot be used to compare two strings for equality. There are 2 main exceptions we talked about. The first...
Write a Java method that takes an array of char and a String as input parameters...
Write a Java method that takes an array of char and a String as input parameters and and returns an boolean. The method returns true if we can find the input string inside the array by starting at any position of the array and reading either forwards or backwards.
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...
(java) Part 1 Write a method named compare that accepts two String arrays as parameters and...
(java) Part 1 Write a method named compare that accepts two String arrays as parameters and returns a boolean. The method should return true if both arrays contain the same values (case sensitive), otherwise returns false. Note - the two arrays must be the same length and the values must be in the same order to return true. Part  2 Write a method named generateArray that accepts an int size as its parameter and returns an int[] array where each element...
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?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT