Understanding pointers
I've got a method which does something to an array.
privateboolean Process(String[] DestArray){
// Do something to the array DestArray and return true if all goes well
}
Now, how can I tell my Java compiler that DestArray is just a pointer, and how can I retrieve the name object it is pointing to?
I want the "Process" method to do different things depending on the name (and the class it belongs to) of the object the pointer "DestArray" point to.
Where are Java pointers? How can I use them? How can I tell Java compiler I'm not passing a variabile but just a pointer? I'm a bit confused.
Java does not use pointers. I think you'll need to start with some basic "Hello, World!" stuff until you're comfortable with the differences between Java and C/C++.Brian
Yes, I think you're right. In fact my program is very, very simple, indeed. Now I know Java does not use pointers. That's quite... unbelievable to me!Thank you :)
Every variable of an object type (which includes arrays) is a reference. So in the above example it's always a reference (which is as close to pointers as you're going to get).
> Now, how can I tell my Java compiler that DestArray
> is just a pointer...
It is a "pointer" of sorts, called a "reference". DestArray is referencing, or "pointing to" an array of String elements.
> and how can I retrieve the name
> object it is pointing to?
This makes no sense. It is just referencing an array of Strings. There is no "name object".
> I want the "Process" method to do different things
> depending on the name (and the class it belongs to)
> of the object the pointer "DestArray" point to.
The rest of your post makes no sense either.
Java does use pointers all over the place, it just doesn't have pointerarithmetic. Pointers are called 'references' in Java. In C++ lingo: allobjects in Java are dynamic.kind regards,Jos
Here's a wonderfully idiotic thread from the OO&Design Patterns board that explains everything you didn't want to know about java pointers :-) http://forum.java.sun.com/thread.jspa?threadID=771118&tstart=0
OK guys, I see this is quite an hot topic and telling a wrong thing in this place might take me to be killed with an EMP coming directly from my network card.
I ask you for some patience. Not everybody is experienced like you.
Reading your kind answers I've understood Java has no pointers. Cool.
So I humbly understand why this code:
class Example {
public void Process(String myString) {
myString = myString+"_edit";
}
public Example() {
String Foo = "foo";
Process(Foo);
System.out.println(Foo);
}
public static void main(String[] args) {
Example main = new Example();
}
}
...outputs "foo", and not "foo_edit".
Now I will make a very annoying question (so please don't fire me): is there any way I can get "foo_edit" as a result in this example, without editing the "Process" method so that it outputs the new string as a result:
public String Process(String myString) {
return myString+"_edit";
}
Thank you
If you want to return something, the best thing to do is return it as a return value.
But you can, if for some reason you really wanted to obfuscate your code, change the field of an object passed to the method. Java doesn't have pointers but it does have references, which in cases like this act similarly. (As already stated, refereces = pointers - pointer arithmetic, basically.)
Yes, you could take an argument of type StringBuffer instead, and call append() on the object reference you receive in Process(). Strings have no methods to change their contents (often called immutable), but StringBuffers do. You can turn a StringBuffer into a string by invoking its toString() method. Including it in a String concatenation will indirectly invoke StringBuffer.toString().
Brian
OK, so it's only a problem of Strings, ints, doubles against other Object types.
You're telling me Java is always passing (something like) pointers to the methods, unless the Object is primitive (like int, double, etc...) or semi-primitive (like String).
This would be a good news for me.
> You're telling me Java is always passing (something
> like) pointers to the methods, unless the Object is
> primitive (like int, double, etc...) or
> semi-primitive (like String).
If it's a primitive, it's not an Object.
A String is an Object. It's not semi-primitive. It has some language support that makes it look vaguely primitive, but it's a trick! That stuff just gets translated into regular method calls by the compiler.
Arguments are passed to method by value. In the case of primitives, the value is the value that you see. In the case of reference types (ie, objects) the value is a reference, which you normally see through to the referenced object.
So it's pass-by-value, but sometimes it smells like pass-by-reference.
Message was edited by:
paulcw
> OK, so it's only a problem of Strings, ints, doubles
> against other Object types.
>
> You're telling me Java is always passing (something
> like) pointers to the methods, unless the Object is
> primitive (like int, double, etc...) or
> semi-primitive (like String).
Close, but String is not "semi-primitive."
Every value that you can set, pass, or return, is either a primitive (byte, char, short, int, long, float, double, boolean), or a reference.
References--as already mentioned--are like C/C++ pointers in that they are a single level of indirection away from an object, but you can't do the arithmetic and arbitrary assignment and casting in Java that you can do in C/C++.
// Java
Foo foo = new Foo();
foo.doStuff();
// C++ equivalent (I think)
Foo* foo = new Foo();
foo->doStuff();
Strings are no different than other classes in this respect. The compiler lets you take some shortcuts--the "double quote string literals"--and there's the constant pool that gives some memory and CPU optimization. But as far as types and values go, Strings are nothing special.
Technically, null is its own type, but you can treat it as just a special value of a reference type.
Thank you all.Now it's quite clearer.