Message Passing in Java

tech channel with DSK
2 min readMar 12, 2021

Pass-by-value

  • Primitive members (int, long, float, double, char, boolean) passed as arguments into method are called as pass-by-value.
  • In pass-by-value the value of the variable is copied into method argument, so that even though the parameter value changes in the methods the /argument or value doesn’t reflect in the original copy.

// PBV.java class PBV { }

program continues here………

Pass-by-reference

  • Arrays and all objects except String passed as arguments into method are called as pass-by-reference.
  • In pass-by-reference the address of the object is passed into method, so that if any changes made to object in the method will be modified in the origin object also.

class Emp { int eid; String ename; double sal; String desig; Emp() {} Emp(int id, String en, double sa, String de) { eid=id; } public String toString() { return eid+” “+ename+” “+sal+” “+desig; } } // PassByReference.java public class PassByReference { public static void main(String rags[]) { System.out.println(d1); // 40.89, 20.45, 30.65 System.out.println(e); // 1 Mr. ABC 200000.00 Architect } Access specifiers that can be applied on: class: default, public var: all 4 constr: all 4 method: all 4 Access modifiers that can be applied on: class: abstract, final var: static, final, volatile, transient constr: No access modifier method: abstract, final, static, native, synchronized

--

--

tech channel with DSK
0 Followers

Hi there , I am kashyap I have started writing this posts inorder to give more information on programming languages and my blog is useful for both ECE and CSE