Call By Reference/call By Value Concept In Java

ravula suryaravula surya
1 min read

IT IS ALWAYS PASS BY VALUE ALL THE TIME BY DEFAULT.IT CAN BE CLEARLY EXPLAINED BY A SMALL PIECE OF CODE.

NOTE: READ COMMENTS ALSO..

/* IN JAVA ALWAYS THE IT IS PASS BY VALUES NOT PASS BY REFERENCE 
SEE HERE IF WE RUN THE COMMENTED CODE THE ACTUAL MONEY AND FINAL MONEY THERE THE money is local to that particular method
and the money variable stored in the doubleScheme method is destroyed after that method execution
and the money variable present in the main method value remains 100..

*/
//class Main
//{
//    public static void doubleScheme(int money)
//    {
//        money = money*2;
//        System.out.println("THE FINAL MONEY"+money);
//        
//    }
//    public static void main(String args[])
//    {
//        int money=100;
//       doubleScheme(money);
//      System.out.println("THE actual MONEY"+money);
//
//    }
//}

class Main{
    public static int doubleScheme(int money)
    {
        money=money*2;
        return money;
    }
    public static void main(String args[])
    {
        int money=100;
        money=doubleScheme(money);
        System.out.println("the final money is"+money);
    // the money is returned and its like we got the updated money since we returned the money and we updated in the main method itself.
    }
}

OUTPUT FOR THIS PROGRAM IS-

the final money is200
1
Subscribe to my newsletter

Read articles from ravula surya directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

ravula surya
ravula surya