What is a parameterized reference in Java?

Passing by value and reference in java?

  • I understand in Java when you pass one of the eight primitive data types to a method, you are passing by value, but when you pass an object you pass by reference. I'm somewhat confused why in this short java program why the string object still retains its original value, even after being passed to a method and with its value changed. public class MyApp { public static void main(String[] a) { String str = "Initial value of the string."; printMessage(str); System.out.println(message); // prints "Initial value of the string". } public static void printMessage(String m) { m = "Hello, there!"; } } Can you help me to understand this

  • Answer:

    There are two viable options to accomplish what you're trying to do. The first is to call a returnable method and store it in a String in the Main method and then print, like this: import java.util.Scanner; public class MyApp { public static void main(String[] args) { String str = "Initial value of the string."; str = printMessage(str); System.out.println(str); } public static String printMessage(String m) { m= "Hello, there!"; return m; } } Or, if you would prefer to have a void method, you can print directly from the method like this: import java.util.Scanner; public class MyApp { public static void main(String[] args) { String str = "Initial value of the string."; printMessage(str); } public static void printMessage(String m) { m = "Hello, there!"; System.out.println(m); } }

Thinker at Yahoo! Answers Visit the source

Was this solution helpful to you?

Other answers

"String" in java is immutable meaning they are constants and cannot be changed.However you can try using StringBuffer which is mutable.

Rajasekhar

I'm assuming you're just learning about methods and parameters. The reason it's returning its original value is NOT because strings are immutable. Immutable means you cannot change its properties. However, you can still tell 'str' to store another value. When you pass 'str' through printMessage, it passes it through as 'm' in the method. The variable 'm' holds "Initial value of the string.". In the method, it tells to replace "Initial value of the string." in the variable 'm' to "Hello, there!". So, 'str' has nothing to do with this method, really. If in the method it said: str = "Hello, there!"; then when you print 'str', it would say "Hello, there!"

Lonely Soul

Key concept: Java manages memory and stores data in two different ways. The heap: An amorphous mass of bytes. When an object is created, it is allocated from the heap. At any time any byte in the heap is either in the free space or it is part of an object. The stack: Each thread has a stack. The stack is where the method parameters and local variables are stored. The only data on the stack are primitives or a reference to an object. A reference to an object is on the order of the size the primitives. It only holds enough information to tell the java virtual machine (JVM) where to find an object in the heap. When you compile your class, the compiler puts information into the class file for each literal string, i.e. "some constant text". 1. In your example, when the JVM loads a class, like MyApp, into memory, it will use the literal string information in the class file to create a String object in the heap for literal. 2. When the main method is entered, the JVM will make room for any local variable on the stack. In your example, str. It the executes the assignment operator, putting the address of the string object into the local variable. 3. To make the call to printMessage, the JVM will: a. Make room on the stack for another object reference b. Copy the address of the out of str into the space allocated in step a. c. Transfer control to the printMessage method. 4. In printMessage, the JVM executes the assignment operator putting the address of the "Hello, there!" string into the space allocated in step 3a. 5. The JVM reached the end of the method. Control returns to where it was the main method. All the space allocated on the stack in 3a is tossed away. Notice how the contents of str were never touched. It is said the Java passes objects by reference because the only information that goes onto the stack is the "reference", the address of the object. This is why setting the value of m in printMessage doesn't change the value of str.

Mike T

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.