Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6794747
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:15:34+00:00 2026-05-26T18:15:34+00:00

Java is pass by value. What if I need a pass by reference. For

  • 0

Java is pass by value. What if I need a pass by reference. For example in the following code I need a pass by reference mechanism.

public class BinaryTree {
    public TreeNode root;

    public BinaryTree(){
        root = null;
    }

    public TreeNode insert(TreeNode temp,int x){
        if(temp == null){
            temp = new TreeNode();
            temp.key = x;
            temp.left = temp.right = null;
            return temp;
        }
        if(temp.key > x)
            temp.left = insert(temp.left,x);
        else if(temp.key < x)
            temp.right = insert(temp.right,x);
        return null;
    }
 }

When insert is called with root, I need root to be passed as a reference so as to change its value. But this does not happen in Java, since it’s pass by value. In C/C++ the above could be easily achieved. Don’t you think that this is a drawback of Java? How could such problems be solved in Java?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T18:15:35+00:00Added an answer on May 26, 2026 at 6:15 pm

    Don’t you think that this is a drawback of Java?

    No. Because:

    1. There are few cases where you really need it.
    2. There are workarounds (see below).
    3. Implementing pass by reference in Java would be difficult. It makes code generation and garbage collection significantly more complicated.

    (OK … so really these are counter-arguments. But we are talking about a language design issue here, and any rational discussion of language designs has to weigh up the pros and cons of supporting a particular feature. And that includes implementation cost and performance issues.)

    How could such problems be solved in Java?

    The general approach is to restructure your code so that the variable you need to update in the called method is replaced with a reference to a mutable object or an array. This may entail the caller doing a bit more work, but that is generally acceptable.

    Alternatively (and in your example) restructure the code so that call by reference is unnecessary.


    In your example, there are two observations to make:

    • The “call-by-reference” mechanism is only used in the case where the tree is empty. It is not difficult to change this so that it is not necessary.

    • In fact, your use of call-by-reference, and in fact the entire insert method, is a leaky abstraction. There is nothing to stop you calling the method with a node object that is nothing to do with the current BinaryTree instance. You are relying on the caller to maintain the (implied) invariants of the tree.

    The following version addresses both of these issues:

    public class BinaryTree {
        private static class TreeNode { ... }
    
        public TreeNode root;
    
        public BinaryTree(){
            root = null;
        }
    
        public void insert(int x) {
            root = insert(root, x);
        }
    
        private TreeNode insert (TreeNode node, int x) {
            if (node == null) {
                return new TreeNode(x);
            }
            if (node.key > x)
                node.left = insert(node.left, x);
            else if (node.key < x)
                node.right = insert(node.right, x);
            return node;
        }
     }
    

    (I don’t exactly like the way that we reassign the left / right pointers at each level after the insertion, but it does make the insertion logic simple.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I pass an array by reference in Java? For instance I need
This Java code: public class XYZ { public static void main(){ int toyNumber =
I was really looking at the differences between pass by value and how Java
What regex pattern would need I to pass to java.lang.String.split() to split a String
I have a java application I need to pass some info to a C++
Is there any way to pass a callback to java code, from C. And
OK, before you jump to your feet, you need to understand what pass-by-value compared
I have a java code, where I give some variable dynamically. I need to
I need to pass a variable from Admin.java file to index.jsp. I get the
I have an object and I need to pass its class to an annotation

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.