-
Java Objects
Hi
I am very new to Java. I have heard that objects in Java are passed by reference
and not by value.
1. Is this correct
2. What is the main difference between passing by reference and passing by
value. What efficiciency does this gain you. If possible, please give an
example to clarify.
-
Re: Java Objects
"Raj" <jstudent01@hotmail.com> wrote:
>
>Hi
>
>I am very new to Java. I have heard that objects in Java are passed by reference
>and not by value.
>
>1. Is this correct
>2. What is the main difference between passing by reference and passing
by
>value. What efficiciency does this gain you. If possible, please give an
>example to clarify.
>
>
-
Re: Java Objects
Hello Raj:
1. Yes, Java objects are passed by reference.
2. Simply put, when you pass an object to a method, you are directly manipulating
the object. Should your method change the object, the changes are permanent.
When you pass a primitive data type, you are passing a copy of the value
of the data. Should your method change the value of the copy of the data,
the original remains unchanged outside the method.
Tom Duffy
"Raj" <jstudent01@hotmail.com> wrote:
>
>Hi
>
>I am very new to Java. I have heard that objects in Java are passed by reference
>and not by value.
>
>1. Is this correct
>2. What is the main difference between passing by reference and passing
by
>value. What efficiciency does this gain you. If possible, please give an
>example to clarify.
>
>
-
Re: Java Objects
Tom Duffy <td4729@hotmail.com> wrote in message
news:38eb54b7$1@news.devx.com...
> 1. Yes, Java objects are passed by reference.
This isn't exactly correct. Objects are not passed as parameters at all.
References to objects are passed and they are passed by value. In fact,
every parameter is passed by value in Java.
Since your passing copies of references, its like passing by reference
except for one subtle condition: assignments of reference parameters inside
methods are not realized by the caller.
For example:
void changeValue(String reference)
{
reference = "new string";
}
void caller()
{
String str = "old string";
changeValue(str);
// ASSERT(str.equals("new string"));
}
In Java, the assertion is false. In a true pass-by-reference scenario, the
assertion would be true.
Brent Worden
http://www.Brent.Worden.org
-
Re: Java Objects
Hello Brent:
You are absolutely correct and thanks for correcting my post.
Tom Duffy
"Brent Worden" <brent.worden@javelinsolutions.com> wrote:
>
>Tom Duffy <td4729@hotmail.com> wrote in message
>news:38eb54b7$1@news.devx.com...
>> 1. Yes, Java objects are passed by reference.
>
>This isn't exactly correct. Objects are not passed as parameters at all.
>References to objects are passed and they are passed by value. In fact,
>every parameter is passed by value in Java.
>
>Since your passing copies of references, its like passing by reference
>except for one subtle condition: assignments of reference parameters inside
>methods are not realized by the caller.
>
>For example:
>
>void changeValue(String reference)
>{
> reference = "new string";
>}
>
>void caller()
>{
> String str = "old string";
> changeValue(str);
> // ASSERT(str.equals("new string"));
>}
>
>In Java, the assertion is false. In a true pass-by-reference scenario,
the
>assertion would be true.
>
>Brent Worden
>http://www.Brent.Worden.org
>
>
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|