Sunday, 5 November 2017

Java Program of Pass by value and pass by reference

Java Program of Pass by value and pass by reference:

Example of passing arrays:

Code Screenshots:



Output of Above Java Program of Pass by value and pass by reference:

Screenshot:



Java Program of Pass by value and pass by reference:

Source Code:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package passbyvalue;

/**
 *
 * @author selectivetopics
 */
public class Passbyvalue
{
   // main creates array and calls modifyArray and modifyElement
   public static void main( String[] args )
   {
      int[] array = { 1, 2, 3, 4, 5 };
      
      System.out.println( "The values of the original array are:" );
      // output original array elements 
      for ( int value : array )
         System.out.printf( "   %d", value );
   
      modifyArray( array ); // pass array reference
      System.out.println( "\n\nThe values of the modified array are:" );
      // output modified array elements 
      for ( int value : array )
         System.out.printf( "   %d", value );
   
      System.out.printf( 
         "\n\nEffects of passing array element value:\n" +
         "array[3] before modifyElement: %d\n", array[ 3 ] );
     modifyElement( array[ 3 ] ); // attempt to modify array[ 3 ]
      System.out.printf( 
         "array[3] after modifyElement: %d\n", array[ 3 ] );
   } // end main
   
   // multiply each element of an array by 2 
   public static void modifyArray( int array2[] )
   {
      for ( int counter = 0; counter < array2.length; counter++ )
         array2[ counter ] *= 2;
   } // end method modifyArray
   
   // multiply argument by 2
   public static void modifyElement( int element )
   {
      element *= 2;
      System.out.printf( 
         "Value of element in modifyElement: %d\n", element );
   } // end method modifyElement  
} // end class PassArray

1 comment:

  1. done lab assignment from this post thanks alot ,
    very helpful

    ReplyDelete