Java: Passing Arguments to methods

Passing Arguments to Methods

Java uses the ‘call by value’ method when passing values to a method, and it gives different results depending on the type of the actual parameter.

Java is a reference data type except for eight base types, and if the actual parameter is the base type, it copies the value of the actual parameter to the type parameter. In this case, the value of the actual parameter does not change even though the value of the type parameter changes.

If the actual parameter is a reference type, copy the address of the actual parameter object to the type parameter. In this case, the value of the actual parameter changes as the value of the type parameter changes. This is called call by reference effect.

class ParaPassing1 {
  public void change(int i, int j[], StringBuffer sb) {
    i = 20;
    j[3] = 400;
    sb.append("Easy explained Java!");
  }

  public void display(int i, int j[], StringBuffer sb) {
    System.out.println("The value of the object attribute variable i: " + i);
    System.out.print("The value of the array: ");
    for(int index=0; index<j.length; index++) {
      System.out.print(j[index] + "");
    }
    System.out.println("");
    system.out.println("The value of the string sb: " + sb);
  }
}

class ParaPassingDemo1 {
  public static void main(String args[]) {
    ParaPassing1 cb = new ParaPassing1();
    int 1 = 10;
    int j[] = {1, 2, 3, 4};
    StringBuffer sb = new StringBuffer("Easy to learn?");
    System.out.println("The first call to the display() method");
    cb.display(i, j, sb);
    cb.change(i, j, sb);
    System.out.println("===========================");
    system.out.println("Converts the value and then calls the second display() method");
    cb.display(i, j, sb);
  }
}
Example of passing a value to a method

Leave a Reply

Your email address will not be published. Required fields are marked *

Latest Post

  • 8 things you need to think about for work-life balance
    View on Youtube Image: juliezhuo.com who works as a design VP for Facebook, presented 8 ways for work and life balance in her article “The
  • 10 special abilities of Smartphone
    View on Youtube No matter where you go or what you do, your smartphone is always with us. We use the internet with a smartphone,
  • Java: abstract class, abstract method
    An abstract class in Java is a class that declares an abstract function to be implemented in a subclass as an abstract method. An abstract
  • Java: inheritance and constructors
    When an object is created from an inherited class, the constructor of the superclass is performed before the constructor of the subclass. That is, when
  • Java reserved words: super, final
    super is a Java reserved word that is used to access member variables or methods of a superclass that is obscured by a subclass. Access
  • Java: finalize method and garbage collection
    The Java Virtual Machine(JVM) automatically performs garbage collection for objects that are no longer needed for efficient system operation. At this point, the JVM calls
  • Java: Method overloading
    Method overloading is the same concept as constructor overloading. That is, you can use methods of the same name in the same class, but methods
  • Java final variable
    You can specify an end variable using the reserved word final. The final variable represents a constant value that can not be changed. Typically, final
  • Java class variables
    Class variables are declared with static and have the concept of global variables. Class variables are used for the following purposes. Object references and object
  • Member variables in Java
    Java’s member variables are all declared outside the methods in the class and represent the properties that the object can have. Member variables consist of