Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Java What is the main difference between Pass by Value vs. Pass by Reference in Java

natrazcoder

New Coder
Hi All,

I read many articles but wanted to know which are the main difference between Pass by Value vs. Pass by Reference in Java from this community.

Also, is there any real-life example that you can share with me, this will help me in the assignment that I am working

Thank you!
 
If you do a Google search you will find a lot of answers on the web.

But, as you asked here are the main difference;

- Java supports Pass by value and doesn’t support Pass by reference. Instead of passing only the value part of a variable, Java passes a reference to the original object for non-primitives
- In the Call by value method original value is not modified whereas, in the Call by reference method, the original value is modified.
- Call by Value, variables are passed using a straightforward method whereas Call by Reference, pointers are required to store the address of variables.

Example:

Here is an example where we will be using call by value using Java Objects -

Code:
public class PassByValue {
  public static void main(String[] args) {
    Integer a = 2;
    Integer b = 3;
    add(a,b);
    System.out.println("Result from main: " +(a+b));
  }

  private static void add(Integer a, Integer b){
    a = 10;
    System.out.println("Result from method: " +(a+b));
  }
}


Result

Code:
Result from method: 13
Result from main: 5


I don't think so if is there any real-life example but if you want to learn jump
here.

Hope this help you!
 
If you do a Google search you will find a lot of answers on the web.

But, as you asked here are the main difference;

- Java supports Pass by value and doesn’t support Pass by reference. Instead of passing only the value part of a variable, Java passes a reference to the original object for non-primitives
- In the Call by value method original value is not modified whereas, in the Call by reference method, the original value is modified.
- Call by Value, variables are passed using a straightforward method whereas Call by Reference, pointers are required to store the address of variables.

Example:

Here is an example where we will be using call by value using Java Objects -

Code:
public class PassByValue {
  public static void main(String[] args) {
    Integer a = 2;
    Integer b = 3;
    add(a,b);
    System.out.println("Result from main: " +(a+b));
  }

  private static void add(Integer a, Integer b){
    a = 10;
    System.out.println("Result from method: " +(a+b));
  }
}


Result

Code:
Result from method: 13
Result from main: 5


I don't think so if is there any real-life example but if you want to learn jump
here.

Hope this help you!
Thank you for your quick response!
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom