Java - Pass By Value - Printable Version +- The Bytecode Club - Reverse Engineering Forum (https://the.bytecode.club) +-- Forum: Lobby (https://the.bytecode.club/forumdisplay.php?fid=1) +--- Forum: Programming (https://the.bytecode.club/forumdisplay.php?fid=86) +--- Thread: Java - Pass By Value (/showthread.php?tid=389) |
Java - Pass By Value - Explicit - 01-20-2015 This was written quite awhile ago on another site, which is why it may look familiar to some. Note that I am in fact the original author. I feel like this is a common issue among people who are just learning the language, and took me awhile to fully grasp the underlying concept. If you want to even begin understanding this you're going to need to know about two areas of memory, the stack and the heap. To start off, let's have a quick look at what happens in memory when we define variables in Java. When we start our Java program, the system automatically allocates a chunk of RAM (if you don't know what this is just think of it as memory) for the JVM to use. Two categories contained inside of that memory are called the stack and the heap. You should be familiar with them if you're an experienced Java developer, but if not here's a quick definition of both (and a nice little picture to go along with it):
Now were going to create a basic Java class with a main method, followed by a simple primitive variable declaration: Code: public class Learning { What happened to the Stack/Heap? As the stack holds the variable values, we are going to push (I will be using common stack terminology in this tutorial so if you're unfamiliar with it a quick Google search will do you wonders) the initialized variable exampleInt onto the stack (the JVM will allocate the appropriate memory to hold the variable value) with the value of 5: Easy enough right? Now to give an example of what happens when I create an object, let's first make a simple class called Person with a field called name: Code: public class Person { Create a simple Person object with the name of Bob, and the JVM will create the necessary memory location for our new Person object: Code: public class Learning { Wait, didn't you just say variables were initialized and placed on the stack? Yes they are, however when it comes to reference types the only thing created on the stack is a variable with a pointer to the object that is created on the heap (in this case if you can read it it's 709F) with a bunch of meta data among other things were not going to worry about right now. Now that we finished covering the absolute basics of what's going on under the hood when we make variables, let's take a look at how this applies to passing variables to methods. Let's create a method which takes our exampleInt and attempts to change the value: Code: public class Learning { Now before you look at my commented code try to think about what's going to happen. Remember, Java is pass by value, so what do you expect is going to happen to this int? Code: public class Learning { When we passed our exampleInt to the #attemptToChange(int testInt) method, we passed the actual value of the variable and created a whole new variable called testInt which now holds the exact same value exampleInt did (essentially a complete copy)! That's why when we printed our the value of exampleInt after we invoked the method on it, it still contained the same value. Just to reiterate that one more time:
Code: public class Learning { Think carefully! When you think you know the outcome look below at my comments: Code: public class Learning { Let's see what happened here if you don't understand:
Sorry for the abrupt ending, but hopefully this was a semi decent tutorial for people learning Java and possibly even advanced programmers alike. RE: Java - Pass By Value - Konloch - 01-20-2015 Stickied this, extremely informative about memory handling, good job on this. |