Question

Java passes arguments by value. Which of the following correctly describes how variables are passed to...

Java passes arguments by value. Which of the following correctly describes how variables are passed to a method?

Select one:

Both primitive and object types are passed by the value that they refer to in memory.

Primitive types are passed by reference while object types are passed by values stored at the object location in the memory.

A variable is passed to a method and its value will no longer exist within the scope of where the method was originally called.

A variable is passed to a method and the method uses the variable name to access and modify the variable.

Primitive values are passed by value while object types are passed by reference.

Homework Answers

Answer #1

Both primitive and object types are passed by the value that they refer to in memory.

EXPLAINATION

Object references are passed by value

All object references in Java are passed by value. This means that a copy of the value will be passed to a method. But the trick is that passing a copy of the value also changes the real value of the object. To understand why, start with this example:

public class ObjectReferenceExample {

            public static void main(String... doYourBest) {

                Simpson simpson = new Simpson();

                transformIntoHomer(simpson);

                System.out.println(simpson.name);

            }

            static void transformIntoHomer(Simpson simpson) {

                simpson.name = "Homer";

            }

}

class Simpson {

            String name;

}

What do you think the simpson.name will be after the transformIntoHomer method is executed?

In this case, it will be Homer! The reason is that Java object variables are simply references that point to real objects in the memory heap. Therefore, even though Java passes parameters to methods by value, if the variable points to an object reference, the real object will also be changed.

Primitive types are passed by value

Like object types, primitive types are also passed by value. Can you deduce what will happen to the primitive types in the following code example?

class Simpson {

            String name;

}

public class PrimitiveByValueExample {

            public static void main(String... primitiveByValue) {

                int homerAge = 30;

                changeHomerAge(homerAge);

                System.out.println(homerAge);

            }

            static void changeHomerAge(int homerAge) {

                homerAge = 35;

            }

}

If you determined that the value would change to 30, you are correct. It’s 30 because (again) Java passes object parameters by value. The number 30 is just a copy of the value, not the real value. Primitive types are allocated in the stack memory, so only the local value will be changed. In this case, there is no object reference.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Consider the following code snippet: Employee programmer = new Employee(10254, "exempt"); String str = programmer.toString(); Assume...
Consider the following code snippet: Employee programmer = new Employee(10254, "exempt"); String str = programmer.toString(); Assume that the Employee class has not implemented its own toString() method. What value will the variable str contain when this code is executed? Group of answer choices the variable will become a null reference the code will not compile because Employee has not implemented toString() the default from Object, which is the class name of the object and its hash code the string representations...
Complete the redblacktree in Java. Add a public boolean isBlack field to the Node inner class....
Complete the redblacktree in Java. Add a public boolean isBlack field to the Node inner class. Make every Node object have a false isBlack field, all new node is red by default. In the end of the insert method, set the root node of your red black tree to be black. Implement the rotate() and recolor() functions, and create tests for them in a separate class. import java.util.LinkedList; public class BinarySearchTree<T extends Comparable<T>> { protected static class Node<T> { public...
Sign In INNOVATION Deep Change: How Operational Innovation Can Transform Your Company by Michael Hammer From...
Sign In INNOVATION Deep Change: How Operational Innovation Can Transform Your Company by Michael Hammer From the April 2004 Issue Save Share 8.95 In 1991, Progressive Insurance, an automobile insurer based in Mayfield Village, Ohio, had approximately $1.3 billion in sales. By 2002, that figure had grown to $9.5 billion. What fashionable strategies did Progressive employ to achieve sevenfold growth in just over a decade? Was it positioned in a high-growth industry? Hardly. Auto insurance is a mature, 100-year-old industry...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT