Passing Data to and from Actions
Understanding Parameters in Object-Oriented Programming
Parameters
Variables used in methods to receive data from the caller.
Arguments
Actual values passed to the parameters when the method is called.
- In Java, methods are the primary way to define actions within a class.
- They can accept parameters and return values, making them versatile tools for encapsulating behaviour.
Designing Methods with Parameters
Methods can accept multiple parameters but return only one value.
- Single Parameter: Useful for simple actions.
- Multiple Parameters: Allows complex operations by providing all necessary data.
- When designing methods, consider the minimum number of parameters needed to perform the action.
- This keeps your code clean and efficient.
Consider a Calculator class with the following methods:
- add(int a, int b): Takes two integers and returns their sum.
- increment(int a): Takes an integer, adds one, and returns the result.
public class Calculator{
public int add(int a, int b){
return a + b;
}
public int increment(int a){
return a + 1;
}
public static void main(String[] args){
Calculator myCalc = new Calculator();
// test functions
System.out.println(myCalc.add(2, 3));
System.out.println(myCalc.increment(2));
}
}
Pass-by-Value
In Java, all parameters are passed by value.
This means that a copy of the argument is made and used within the method.
- Primitive Types: Changes to the parameter do not affect the original argument.
- Objects: The reference is copied, so changes to the object's state are reflected outside the method, but reassigning the reference does not affect the original object.
An exception to changing objects inside the method is immutable objects, such as String in Java, as they are designed to be unchangeable.
public class Test{
static class myClass{
public int myAttribute = 5; // attribute with initial value 5
}
public void testPrimitive(int b){
b += 1; // increment by 1
}
public void testString(String str){
str += " hello!"; // add " hello!" in the end
}
public void testObject(myClass obj){
obj.myAttribute += 1; // increment attribute by 1
}
public static void main(String[] args){
Test tester = new Test();
int a = 5;
System.out.println("Initial value of a: " + a);
tester.testPrimitive(a); // pass primitive variable
System.out.println("New value of a: " + a);
String st = "Alice";
System.out.println("Initial value of st: " + st);
tester.testString(st); // pass immutable object variable
System.out.println("New value of st: " + st);
myClass myObject = new Test.myClass();
System.out.println("Initial value of object attribute: " + myObject.myAttribute);
tester.testObject(myObject); // pass object variable
System.out.println("New value of object attribute: " + myObject.myAttribute);
}
}
- What is the difference between parameters and arguments in methods?
- How does pass-by-value work for primitive types and objects in Java?
- Why are immutable objects like String not affected when modified inside a method?