Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you pass by reference in Java?

Sorry if this sounds like a newbie question, but the other day a Java developer mentioned about passing a paramter by reference (by which it was ment just pass a Reference object)

From a C# perspective I can pass a reference type by value or by reference, this is also true to value types

I have written a noddie console application to show what i mean.. can i do this in Java?

namespace ByRefByVal
{
    class Program
    {
        static void Main(string[] args)
        {
            //Creating of the object
            Person p1 = new Person();
            p1.Name = "Dave";
            PrintIfObjectIsNull(p1); //should not be null

            //A copy of the Reference is made and sent to the method
            PrintUserNameByValue(p1);
            PrintIfObjectIsNull(p1);

            //the actual reference is passed to the method
            PrintUserNameByRef(ref p1);    //<-- I know im passing the Reference
            PrintIfObjectIsNull(p1);

            Console.ReadLine();
        }

        private static void PrintIfObjectIsNull(Object o)
        {
            if (o == null)
            {
                Console.WriteLine("object is null");
            }
            else
            {
                Console.WriteLine("object still references something");
            }
        }

        /// <summary>
        /// this takes in a Reference type of Person, by value
        /// </summary>
        /// <param name="person"></param>
        private static void PrintUserNameByValue(Person person)
        {
            Console.WriteLine(person.Name);
            person = null; //<- this cannot affect the orginal reference, as it was passed in by value.
        }

        /// <summary>
        /// this takes in a Reference type of Person, by reference
        /// </summary>
        /// <param name="person"></param>
        private static void PrintUserNameByRef(ref Person person)
        {
            Console.WriteLine(person.Name);
            person = null; //this has access to the orginonal reference, allowing us to alter it, either make it point to a different object or to nothing.
        }


    }

    class Person
    {
        public string Name { get; set; }
    }
}

If it java cannot do this, then its just passing a reference type by value? (is that fair to say)

Many thanks

Bones

like image 420
dbones Avatar asked Nov 17 '09 12:11

dbones


People also ask

Is there pass-by-reference in Java?

Java is always Pass by Value and not pass by reference, we can prove it with a simple example. Let's say we have a class Balloon like below. And we have a simple program with a generic method to swap two objects, the class looks like below.

Why pass-by-reference is not allowed in Java?

The reason is that Java object variables are simply references that point to real objects in the memory heap. Therefore, even though Java passes parameters to methods by value, if the variable points to an object reference, the real object will also be changed.

Does Java pass-by-reference or by value explain?

Arguments in Java are always passed-by-value. During method invocation, a copy of each argument, whether its a value or reference, is created in stack memory which is then passed to the method.


2 Answers

No, Java cannot do this. Java only passes by value. It passes references by value too.

like image 64
Cory Petosky Avatar answered Nov 09 '22 21:11

Cory Petosky


Pass by reference is a concept often misunderstood by Java devs, probably because they cannot do it. Read this:

http://javadude.com/articles/passbyvalue.htm

like image 29
David Hedlund Avatar answered Nov 09 '22 21:11

David Hedlund