View Poll Results: Is Network Programming a demanded speciality?
- Voters
- 1. You may not vote on this poll
-
Its a growing speciality
-
Yes
-
No
-
It's almost disappeared
-
I'm not familiar with it
-
Do we need "pass by reference" in Java?
Currently the Java language allows to pass method parameters only by value. This means that if one wants to change a parameter in the method and keep the changed value outside the method there is no direct way to do it. For example if you want to write a method to swap values you cannot do it like that:
public void swap(int a, int b) {
int t = a;
a = b;
b = t;
}
you have to do it like that:
public void swap(int[] a, int[] b) {
int t = a[0];
a[0] = b[0];
b[0] = t;
}
Do you think including pass by reference construct similar to C++ "&", Pascal's "var" or C#'s "ref" is a good idea?
-
I'm afraid your information is a little bit off...java passes all objects by references, and all simple values by copy...for instance
int method(Object a) {
}
passes the object by reference, not value. So to pass an integer or the likes by reference, you simply need to wrap it as an object like Integer(int)
So in effect, I don't think that they should change anything...while pointers can be powerful at times, it would somewhat defeat the java runtime
-
"I'm afraid your information is a little bit off"....no it isn't, it's just quite subtle.
All passing is Java is PASS BY VALUE. No exceptions. If you do Java Programmer Certification, or look into any books on the subject, you will find that sentence.
You don't actually "pass the object by reference". You can never pass an object, you can only pass REFERENCES to objects and the value of references passed to methods are COPIED i.e. PASS BY VALUE.
ArchAngel.
O:-)
-
Ah, I see your point. Thanks for the clarification.
But in effect, pointers in C++ are pass by value too...they pass the value of the memory address. So how is it different than Java passing the value of the reference to an object? Just confused. Thanks.
-
in C++ you can do pointers to pointers to .... :)
But in C/C++ you can pass a pointer to the pointer 
The interesting thing is that despite the pointer operators in C they desided to add the reference operator in C++ just to make it a little bit more convenient and I think they did right!
In C you can do it like that:
void swap(int * a, int * b) {
int t = *a;
*a = *b;
*b = t;
}
then you call:
int a = 2;
int b = 3;
swap(&a, &b);
But in order to save you the constant referencing/dereferencing in C++ they added the & operator, and it looks like that:
void swap(int & a, int & b) {
int t = a;
a = b;
b = t;
}
and you call it like that:
int a = 2;
int b = 3;
swap(a, b);
Cleaner, heh :?)
-
Yep, but how is that still different than
public int method(Object a) {
}
public void main(String argv[]) {
method(new Object(...))
}
Anyway, you get the point...when I pass the method, and I change any of the values, it's changed in the original memory...so isn't that the same as passing a reference in C++?
-
returning multiple values?
If you want to return multiple values you have no way but to use a wrapper object.
How are you going to do that now:
(1)
public void createMultiple(Object a, Object b) {
a = new Object();
b = new Object();
}
Object a = null;
Object b = null;
createMultiple(a, b);
// a and b are still null after the method call
In order for this to work you have to create a wrapper
(2)
public class Wrapper {
public Object a;
public Object b;
}
public void createMultiple(Wrapper w) {
w.a = new Object();
w.b = new Object();
}
then this will work:
Wrapper w = new Wrapper();
createMultiple(w);
Object a = w.a;
Object b = w.b;
If we have references we do just that
public void createMultiple(Object & a, Object & b) {
a = new Object();
b = new Object();
}
this way the first snippet (1) will work.
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
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks