Reputation: 2060
Passing parameters is common in daily programming, but should we pass parameters as object or values?
(A)
public boolean isGreaterThanZero(Payment object) {
if (object.getAmount() > 0) {
return true;
}
return false;
}
(B)
public boolean isGreaterThanZero(int amount) {
if (amount > 0) {
return true;
}
return false;
}
Upvotes: 4
Views: 1696
Reputation: 3813
Yes,we can pass object as parameter in the Java Program .
First Way
class demo
{private int length=1;
private int breadth=1;
private int area;
void input(int length,int breadth)
{
this.length=length;
this.breadth=breadth;
}
void add(demo d1,demo d2)
{
length=d1.length+d2.length;
breadth=d1.breadth+d2.breadth;
}
void output()
{
System.out.println("\nLength="+length+"\nBreadth="+breadth);
}
public static void main(String args[])
{
demo d1=new demo();
demo d2=new demo();
d1.input(1, 1);
d1.output();
d2.input(2, 2);
d2.output();
demo d3=new demo();
d3.add(d1, d2);
d3.output();
}
}
Second Way
class demo
{private int length;
private int breadth;
void input(int length,int breadth)
{
this.length=length;
this.breadth=breadth;
}
demo add(demo d2)
{ demo obj=new demo();//Neccesary as we want to return complete
object irrespective of its number of data fields
obj.length=length+d2.length;//Storing the length of two objects
in the obj
obj.breadth=breadth+d2.breadth;
return obj;
}
void output()
{
System.out.println("\nLength="+length+"\nBreadth="+breadth);
}
public static void main(String args[])
{
demo d1=new demo();
demo d2=new demo();
d1.input(1, 1);
d2.input(2, 2);
demo d3=d1.add(d2);//Here we created new object d3 called
add() by d1 and passed d2 object through it
d3.output();
}
}
Upvotes: 0
Reputation: 1
Addenum for my previous answer. It also depends on compiler optimisations and the method call context. If the object is already defined before the call to the method or if it's a method that could benefits from inlining and optimisations it's not a good idea to use the adress. If you are in a loop, then it's a good idea to use the adress. You have to think a bit about how the method will be used in the specific program ( = anticipate how it will be compiled).
Upvotes: -1
Reputation: 1
Sometimes, Passing the object address(or a pointer, a handle, whatever you call it) as parameter and then to cast it in the method (as an instance of the recquired object) is faster then passing the object as parameter. It just a way to constrain the way it will be compiled. If sometimes it doesn't change anything, in other cases passing the adress will widely make the program faster. It's a low level compiler stuff but IT really CAN make the code faster.
Upvotes: -1
Reputation: 11832
Assuming that you are asking about parameter passing, and not about object design...
You need to consider a number of aspects when designing a method.
Where it makes sense, try to reduce dependencies between modules in your code. In your example, (A) is introducing an unnecessary dependency on the Payment object. But if the method needed a couple of member variables of the Payment object (because it was specific to a payment) then by all means, pass a Payment object as the parameter.
Upvotes: 3
Reputation: 76436
I think we should use primitive types whenever possible (int, double, boolean, char) as parameters because it's not an accident that there are primitive types. I prefer the second option, because it's more general (it's not forcing you to provide a Payment object, any int value will be good enoug). But when you need the state of the object (at least two values of the object's data), the situation might be changed, you can either use values as parameters for a more general usage possibility or objects as parameters for simplicity.
Upvotes: 0
Reputation: 4012
The answer to this question is going to be specific to a given situation so there is no way to give a definitive answer, however the question of when to use which approach is still valid.
I would pass an object when:
I would pass a value when there are few parameters, and all of them are simple value types.
Upvotes: 2
Reputation: 28991
This is a contrived example, but if this is the only interaction the receiving class has with Payment, I would use the value in the interest of decoupling. No point creating superfluous class dependencies.
EDIT: Marcel and I were typing the same thing at the same time. :) This.
Upvotes: 2
Reputation: 12892
My goal is to keep things decoupled. If all isGreaterThanZero() really needs is the amount, then the int version is better, because it doesn't couple the implementation to the Payment class.
Upvotes: 1
Reputation: 40811
Neither of those.
With proper OOP, you would have isGreaterThanZero() on the Payment object, ie:
class Payment {
int amount
public boolean isGreaterThanZero() {
return amount > 0
}
}
and then:
Payment payment
boolean flag = payment.isGreaterThanZero()
Upvotes: 8
Reputation: 38899
As per OOP concepts, we should use Objects
. But, don't overuse it. For example, in your example, I would have used an int
. But in a method where I am using multiple values in many places, I would have used an object.
I wouldn't create a Payment
class just with one data member, "amount
".
Upvotes: 1