PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, July 4, 2022

[FIXED] How to solve change contents of variables having values from call by reference facility in Java?

 July 04, 2022     java, pass-by-reference     No comments   

Issue

I had written the following code as follows:

class Numbers
{

    int temp,a1,b1;

    void swapping(){
        temp = a1;
        a1 = b1;
        b1 = temp;
        System.out.println("In numbers class value of a after swapping ="+a1 + " and that of b=" + b1);
    }
}
public class Swap{

    public static void main(String[] args) {
        int a=2;
        int b=3;
        System.out.println("In swapping class value of a before swapping ="+a + " and that of b=" + b);

        Numbers num = new Numbers();        

        num.a1= a;
        num.b1= b;
        num.swapping();
        System.out.println("In swapping class value of a after swapping ="+a + " and that of b=" + b);
    }

}

Now the values of a1 and b1 in void swapping() are unchanged as per the dummy println statement just after the swapping operation is implemented on both a1 and b1 . This is the same as the values printed in the main function just before the swapping function was called . However the println statement in the main function after the function call shows the swapped values . Suppose instead of the swapped values some garbage value is stored due to some unforeseen abnormalities in Java compiler , then how to change that to whatever is desired?


Solution

The trick here is understanding that Java has two parallel type systems:

  • primitives (for faster execution, least memory used, similarity to C)
  • classes & objects (for benefits of object-oriented programming)

In your code:

num.a1 = a;

… you are copying the quantity of two from a, and putting that quantity into a1. After that statement executes, a and a1 are independent. They are two separate numbers, two separate values, without knowledge of one another. This is because they are primitives.

In your code:

Numbers num = new Numbers();   

… you switched to the OOP type system. Here you instantiated an Numbers object, which means you grabbed a piece of memory somewhere else, carved out room in that memory for the three primitives defined in that class. You also carved out room for a reference to implementation of the method named swapping that lives inside the class definition.

A reference (or link, or pointer) was made from that instantiated Numbers object floating around in memory to your local variable named num.

When you called the swapping method, that code worked on the a1 and b1 primitive values stored within your object. That code knows nothing about the a and b variables that live outside the object, that live locally within the main method only.

So after the swapping code finishes, you can think of memory in your JVM as looking like this diagram. After swapping, the a and b variables remain in their original condition, untouched. Only the a1 and b1 variables (and temp) were touched.

digram of main method distinct from object

So code in separate scopes can share references to the same object. But code in separate scopes cannot share the very same primitive variable, they can only get a copy of the other's primitive variable's value.



Answered By - Basil Bourque
Answer Checked By - Pedro (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing