Passing Data Among Methods Flashcards

1
Q

What is the characteristic of java when it comes to passing data to methods ?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Give an example of that and run it (example of a primitive type and for a reference type) !

A

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 */

	
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Give one use case that is often overlooked of method parameters with an example !

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Give me the ways data can be passed to methods with a clear definition !

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Give an example for each way and run it !

A

**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 */

	
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Give one trick often used in the exam when it comes to getting data back from methods with an example !

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly