Passing Data Among Methods Flashcards
What is the characteristic of java when it comes to passing data to methods ?
Data is passed by value to methods in java, which means that when passing
a variable to a method, it’s an actual copy of that variable that is passed to the method and not the variable itself, which means that :
- if it’s a primitive variable that is being passed to a method, modifications undergone to that variable inside the method do not affect at all the original value of that variable
- if it’s a reference variable that is passed to that method, modifications undergone to that reference type affect the original value of the object that reference type points to
CLARIFICATION :
In Java, when you pass an object reference to a method, the reference itself is passed by value. This means that while the reference to the object is copied, both the original reference and the copied reference point to the same object in memory. As a result, changes made to the object’s state within the method will affect the original object, but changing the reference itself (i.e., pointing it to a new object) will not affect the original reference.
Give an example of that and run it (example of a primitive type and for a reference type) !
EXAMPLE 1 : primitive type :
public static void method(int n) {
n=47;
}
public static void main(String[] args) {
/* TODO Auto-generated method stub */
int a=20;
method(a);
System.out.println(a); // prints 20
}
EXAMPLE 2 : reference type
public static void speak(StringBuilder s) {
s.append(“Webby”);
}
static public void main(String[] args) {
/* TODO Auto-generated method stub */
StringBuilder name = new StringBuilder("TEST"); speak(name); System.out.println(name); /* TESTWebby */ }
Give one use case that is often overlooked of method parameters with an example !
If the method parameter is a reference type, we can use that reference to call
methods that perform actions on the object referenced by that variable inside the method body.
Give me the ways data can be passed to methods with a clear definition !
Data can be passed to methods in java by value or what’s called wrongfully pass by reference which is technically a pass by value, let me clarify :
-
Data passed by value : means that when passing a variable as a parameter to a method, it’s not the variable that’s being passed it’s a copy of the variable that’s being passed to that method which means that if the variable
is a primitive type , changes undergone to that variable inside the method do not affect the original value of the variable but if it’s a reference variable that’s being passed to a method in java, that operation is called wrongfully ‘pass by reference’ here’s why - Pass by reference : when passing a reference type variable to a method in java, some people think that we’re passing the variable by reference, but we’re passing it by value because it’s a copy of the original reference variable that’s being passed, and since the original variable and its copy reference the same object, changes made to the copy affect the original state of the object
Give an example for each way and run it !
**EXAMPLE 1 : Pass by value ** :
n=47;
}
public static void main(String[] args) {
/* TODO Auto-generated method stub */
int a=20;
method(a);
System.out.println(a); // prints 20
}
EXAMPLE 2 : Pass by reference :
public static void speak(StringBuilder s) {
s.append(“Webby”);
}
static public void main(String[] args) {
/* TODO Auto-generated method stub */
StringBuilder name = new StringBuilder("TEST"); speak(name); System.out.println(name); /* TESTWebby */ }
Give one trick often used in the exam when it comes to getting data back from methods with an example !
One common pitfall when it comes to java methods is ignored return values.
When a method returns a value, it’s often important to handle or utilize that returned value in some way. Ignoring return values can lead to unexpected behavior or unintended consequences in your code. It’s important to carefully consider the return values of methods and handle them appropriately in your code.