Answer»
- When you pass primitive types such as the types float, boolean, hit and char to a method, Java passes the variables by value. In other words, Java makes a copy of the original variable which the method can access and the original remains unchanged. Within a method, the code can change the values as much as it needs because Java created these values as copies of the originals.
- WHEREVER you pass a primitive type as a parameter to a method, Java copies this parameter to a SPECIAL memory location known as the stack. The stack maintains information about variables used by the method while the method EXECUTES. When the method is complete, Java discards the stack's contents and the copies of the variables you passed into the method are gone FOREVER.
- Because Java copies your original primitive type parameters, there is never any DANGER of a method altering your original values. Remember, this only applies to primitive types, which are automatically passed by value. Objects and arrays are not passed by value (instead they are passed by reference) and they are in danger of being changed.
|