Programming methods: passing objects or values ​​as a parameter to a method

Transfer parameters are often used in daily programming, but should we pass parameters as objects or values?

(A)

public boolean isGreaterThanZero(Payment object) { if (object.getAmount() > 0) { return true; } return false; } 

(IN)

 public boolean isGreaterThanZero(int amount) { if (amount > 0) { return true; } return false; } 
+4
source share
10 answers

None of them.

With the right OOP, you must have isGreaterThanZero () in the Payment object, that is:

 class Payment { int amount public boolean isGreaterThanZero() { return amount > 0 } } 

and then:

 Payment payment boolean flag = payment.isGreaterThanZero() 
+8
source

Assuming you are asking about passing parameters, not about object design ...

When developing a method, you need to consider a number of aspects.

  • Can the method be used elsewhere? In your example, the answer may be yes - it can determine if any int is greater than zero - in this case (B) is more useful.
  • If this method requires more than one parameter and is specific to a specific object in your code, pass the object.

If that makes sense, try reducing the dependencies between the modules of your code. In your example (A), an unnecessary dependency on a Payment object is introduced. But if for this method a couple of member variables of the Payment object were required (because it was specific to the payment), then by all means pass the Payment object as a parameter.

+3
source

This is a contrived example, but if this is the only interaction the class receives with payment, I would use the value in the interests of decoupling. It makes no sense to create redundant class dependencies.

EDIT: Marcel and I introduced the same thing at the same time. :) This.

+2
source

The answer to this question will be specific to this situation, so there is no way to give a final answer, however, the question of when to use some kind of approach remains valid.

I would pass the object when:

  • The method must deal with a lot of data transferred from outside. It is advisable to wrap these parameters in an object, because this means that you can have a method accept one parameter, and not 1 per data value. You can also flip the parameter checking logic into a shell class for better locality.
  • When the same combination of parameters is often used in an API

I would pass a value when there are several parameters, and all of them are simple value types.

+2
source

In accordance with the concepts of OOP, we must use Objects . But do not abuse it. For example, in your example, I would use int . But in a method where I use multiple values ​​in many places, I would use an object.
I would not create a Payment class with only one data element, " amount ".

+1
source

My goal is to keep things untied. If all isGreaterThanZero () really needs quantity, then the int version is better because it does not bind the implementation to the Payment class.

+1
source

I think we should use primitive types when possible (int, double, boolean, char) as parameters, because it is no coincidence that primitive types exist. I prefer the second option, because it is more general (it does not force you to provide a Payment object, any int value will be good enoug). But when you need the state of an object (at least two values ​​of the object's data), the situation can be changed, you can use the values ​​as parameters for a more general possibility of use, or objects as parameters for simplicity.

0
source

Yes, we can pass an object as a parameter in a 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(); } } 
0
source

Sometimes, passing the address of an object (or a pointer, a descriptor, whatever you call it) as a parameter, and then passing it in a method (as an instance of an object with a request), is faster than passing the object as a parameter. This is just a way to limit how it will be compiled. If sometimes this does not change anything, in other cases, passing the address will significantly speed up the program. This is a low-level compiler, but IT really CAN make code faster.

-1
source

Addenum for my previous answer. It also depends on optimizing the compiler and the context of the method call. If an object is already defined before the method is called, or if it is a method that can benefit from embedding and optimization, it is not recommended to use this address. If you are in a loop then using an address is useful. You need to think a bit about how the method will be used in a particular program (= anticipate how it will be compiled).

-1
source

All Articles