Declaring Methods  «Prev 


Java Argument program

The Argument program illustrates the effects of argument passing. It displays the following results:

10
abcdef

The value of i is not changed by the increment() method. Neither is the value of s. It still refers to the same StringBuffer object. However, the StringBuffer object referenced by s was updated.

Java Argument program

class Argument {

 public static void main(String[] args) {
   new Argument();
 }
    
 Argument() {
   int i = 10;
   increment(i, 100);
   System.out.println(i);
   StringBuffer s = new StringBuffer("abc");
   append(s, "def");
   System.out.println(s);
 }

 void increment(int n, int m) {
   n += m;
 }

 void append(StringBuffer s1, String s2) {
   s1.append(s2); 
   s1 = new StringBuffer("xyz");
 }
}