Can I use pointers in Java?

Okay, so if I have


Class Main{
Image[10][4] = fillArrayWithImages();
Person p = new Person(Image);
}

Class Person{
    Person(Images[][] i){
        Images[10][4] = i;
    }
}

Does Person have a copy of the Image array? Or is p.Images pointing to the Image array?
Do I have two copies of the Image array in memory or just one?

it’s pointers & you have only 1 copy!

the code you provided makes little sense (as it won’t even compile). you are assigning an Image[][] to an element of an Image[][]

Anyway, if you pass an Image[][] to a method, it will not be copied. If you change something in the array, it will be visible to everything that refers to that Image[][].

And don’t call 'em pointers, you’ll only get confused with C. They’re references.

Cas :slight_smile:

I agree with princec. Pointers are more flexible but more dangerous. Using the same word for 2 very different notions is quite confusing.

Every Object is a reference, every primitive is a value. Arrays and Strings are objects.

Whenever you have an instance of an Object, any fields that “hold” that object are just references to the same Object in memory.


MyObject o = new MyObject();
MyObject o2 = o;
MyObject o3 = o;
MyObject o4 = o3;
MyObject o5 = o;
MyObject o6 = o2;
MyObject o7 = o;
MyObject o8 = o7;

What’s being copied when you set those equal is the address of the original MyObject you created, not the object itself. Think of it that you aren’t ever creating any new object unless you say “new” or you call a method that returns a new object (like clone()). In the latter case, the method is going to be calling new somewhere anyway.

Fantastic answer. Thank you very much for your reply.

I’m being a bit nitpicky, but clone happens to be special as it constructs an object without using “new” or even calling a constructor. This is the reason not to use clone (and Serializable, which also uses black demon magic to create new objects). Don’t take my word though, try Joshua Bloch and Doug Lea:
http://www.artima.com/intv/bloch13.html

I’m being even more nitpicky, but you are encouraged to use clone() for array copies.

Don’t take my word though, try Joshua Bloch and Doug Lea:
http://www.artima.com/intv/bloch13.html

Just saying those cats are reasonably well respected, whereas I gets no respect. :stuck_out_tongue: Plus every time I mention the crappyness of Serializable someone replies with, “just use Serializable”. :slight_smile:

The problem there is that you are biased. You plug your own API (kyro). Regardless of whether it is better, you’ll have a harder time convincing people, than somebody else.

Cool, good point to be specific. I was sort of trying to keep it simpler but I understand after reading it over that I was just being inaccurate.

As for the Serializable thing, I actually agree with you - one of my favorite things about Java is its overwhelming transparency with how it works. One thing I hate about Objective-C is that almost all of it is a black box. Serializable is one of the rare weird beasts in Java, and I rarely use it, to be honest. In the other thread I was more arguing about its potential uses (because it can indeed be useful).