Hi all,
I have been told that variables containing objects are basically just references to a memory locations that contain the object data. And doing something like this:

TestObject o1 = new TestObject(..);
TestObject o2 = o1;

would make a new variable o2 that will simply point to the same memory location as o1.

To test this, i used to following code:

public static void main(String[] args) {
        String s1 = "82938";
        String s2 = s1;
        System.out.println(s1);
        s2 = "changed";
        System.out.println(s1);
}

If Objects really worked that way, then this code would have meant the following:
1)Reserve a memory location, store "82938" in it, and let the name s1 point to it
2)Let the name s2 also point to that same memory location
3)print whatever s1 points to
4)change what s2 points to to "changed"
5)print whatever s1 points to

since s1 and s2 point to the same thing, one would expect the last line to print "Changed".
However, i got the following output:

82938
82938

Can someone please tell me what knowlege am i missing?

That should be
4) Create new object "changed", and set s2 to point to that object
now s1 -> "82938", but s2 -> "changed"

Oohhhhh yes that makes sense. How silly of me.

So we basically can NOT explicitly overwrite any memory location right? For example doing something like

s1 = "nothing"

will just make s1 point to a new string "nothing", and leave "82938" in the memory jungle? Rathe than placing the string "Nothing" wherever "82983" was

That's true of Strings because java Strings are immutable - a String object, by design, cannot be changed.
If you have a mutable object then you can change it by calling that object's methods, eg

        StringBuilder s1 = new StringBuilder("abc");
        StringBuilder s2 = s1;
        s2.append("def");
        System.out.println(s1); // prints "abcdef"

I believe this thread is the best place to post this as it is directly tied to the confusion above.

float[] temp = matrix[r1];
for(int i=0; i<temp.length; i++){
    temp[i] *= num;
}

Matrix is basically a two dimensional array. What i am actually doing here is creating an elementary matrix row operation (Add a multiple of one row to another). As you can see, i have deliberately created a "temp" array so the row to which a number "num" will be multiplied remains unchanged at the end. However, strangely, the output, which displays the matrix[int][int] array shows the r1 row multipled by num, as well as the r2 row added with the multiple of r1.

I can obviously get what i want using manual array copying but I want to know what exactly is going on here with respect to memory allocation, pointers etc.

Here is the complete function code:

public static float[][] rowMultAdd(float [][] matrix, int r1, int r2, double num){
        /*float [] temp = new float[matrix[r1].length];
        for(int i=0; i<temp.length; i++){
            temp[i] = matrix[r1][i];
        }*/
        float[] temp = matrix[r1];

        for(int i=0; i<temp.length; i++){
            temp[i] *= num;
        }

        for(int i=0; i<temp.length; i++){
            matrix[r2][i] += temp[i];
        }
        return matrix;
    }

temp is a reference, and you set it to refer to matrix[r1]. Whenever you change the array that temp is currently pointing to you are changing matrix[r1].

In the earlier example we were changing the reference itself, so it referred to a different object. In this case the reference is not being changed and we are changing the object itself.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.