Java Call by Value and Call by Reference

Get Job-ready: Java Course with 45+ Real-time Projects! - Learn Java

Program 1

class TestCall
{
           int n=100;   // Instance variable
          void show( TestCall temp)  // call by value
          {
                   temp.n=temp.n+10;
          }
         void display(int ar[]) 
         {
                ar[0]=100;
                ar[1]=200;
         }
}

class TestCallbyValue
{
     public static void main(String args[])
     {
                 
                  TestCall T1=new TestCall();
                //    System.out.println("Before Call Value is :" + T1.n);
                //    T1.show(T1);
                //   System.out.println("Before call: " +T1.n);
                int a[]={1,2,3,4,5,6,7,8};
                 System.out.println("Before Call Value is :");
               for(int i=0;i<a.length;i++)
                   System.out.println(a[i]);
                   T1.display(a);

                  System.out.println("After Call Value is :");
                 for(int i=0;i<a.length;i++)
                   System.out.println(a[i]);



     }
}

 

Did you know we work 24x7 to provide you best tutorials
Please encourage us - write a review on Google

courses

TechVidvan Team

TechVidvan Team provides high-quality content & courses on AI, ML, Data Science, Data Engineering, Data Analytics, programming, Python, DSA, Android, Flutter, full stack web dev, MERN, and many latest technology.

Leave a Reply

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