Objects as Parameters
• Java is strictly pass-by-value. But the scenario may change when
the parameter passed is of primitive type or reference type.
• If we pass a primitive type to a method, then it is called pass-by-
value or call-by value.
• If we pass an object to a method, then it is called pass-by-
reference or call-by reference.
Pass-by-value (Value as parameter) Pass-by-reference (Object as parameter)
Only values are passes to the function
parameters. So any modifications done
in the formal parameter will not affect
the value of actual parameter
Reference to the object is passed. So
any modifications done through the
object will affect the actual object.
Callee method will not have any access to
the actual parameter
Callee method will have the direct
reference to the actual object
call by value
class Operation
{
int data=50;
void change(int data)
{
data=data+100;
}
public static void main(String args[])
{
Operation op=new Operation();
System.out.println("before change "+op.data);
op.change(500);
System.out.println("after change "+op.data);
}
}}
}
Output:
• before change 50
• after change 50
call by Reference
class Operation2
{
int data=50;
void change(Operation2 op){
op.data=op.data+100;//changes will be in the instance variable
}
public static void main(String args[])
{
Operation2 op=new Operation2();
System.out.println("before change "+op.data);
op.change(op);//passing object
System.out.println("after change "+op.data);
}
}
• Output:
• before change 50
• after change 150

call by value.pptxGE3791 - Human Values and Ethics

  • 1.
    Objects as Parameters •Java is strictly pass-by-value. But the scenario may change when the parameter passed is of primitive type or reference type. • If we pass a primitive type to a method, then it is called pass-by- value or call-by value. • If we pass an object to a method, then it is called pass-by- reference or call-by reference. Pass-by-value (Value as parameter) Pass-by-reference (Object as parameter) Only values are passes to the function parameters. So any modifications done in the formal parameter will not affect the value of actual parameter Reference to the object is passed. So any modifications done through the object will affect the actual object. Callee method will not have any access to the actual parameter Callee method will have the direct reference to the actual object
  • 2.
    call by value classOperation { int data=50; void change(int data) { data=data+100; } public static void main(String args[]) { Operation op=new Operation(); System.out.println("before change "+op.data); op.change(500); System.out.println("after change "+op.data); } }} }
  • 3.
    Output: • before change50 • after change 50
  • 4.
    call by Reference classOperation2 { int data=50; void change(Operation2 op){ op.data=op.data+100;//changes will be in the instance variable } public static void main(String args[]) { Operation2 op=new Operation2(); System.out.println("before change "+op.data); op.change(op);//passing object System.out.println("after change "+op.data); } }
  • 5.
    • Output: • beforechange 50 • after change 150