Sign in to follow this  
broady

[java] Reference in java

Recommended Posts

Hello guys, i am new to java and i have got some question. Take this snip:
package javaapplication3;
import java.util.*;
import java.io.*;
import java.lang.*;


class prova {

private List<Integer> list=  new ArrayList<Integer>();


public prova(){
    list.add(9);
    list.add(4);
    list.add(1);
    list.add(3);
    list.add(8);
    list.add(1);
    list.add(2);
}

public List<Integer> getList(){
    return this.list;
}

public void print(){
    System.out.println(this.list.toString());
}


}


public class Main {


    public static void main(String[] args) {

        prova p = new prova();
        p.print();
        List<Integer> mylist = p.getList();
        mylist.remove(0);
        p.print();
    }

}

Well i come from c++ and this is pretty strange for me. The second p.print() shows that the line mylist.remove(0) has effects even on the private member of p. How is this possible? Can i access the list private member via mylist? This is not possible in c++ (it's only possible if getList() had returned a reference to the member and this is to avoid of course). I coded some snip and noticed that this work happens just with lists(and other collections i think). I.e.
package javaapplication3;
import java.util.*;
import java.io.*;
import java.lang.*;


class prova {

private List<Integer> list=  new ArrayList<Integer>();
private integer myInt;


public prova(){
    list.add(9);
    list.add(4);
    list.add(1);
    list.add(3);
    list.add(8);
    list.add(1);
    list.add(2);
    myInt=10;
}

public List<Integer> getList(){
    return this.list;
}
public Integer getInt(){
    return myInt;
}

public void print(){
    System.out.println(this.myInt;);
}


}


public class Main {


    public static void main(String[] args) {

        prova p = new prova();
        p.print();
        Integer temp = p.getInt();
        temp+=1;
        p.print();
    }

}

This time the 2nf p.print() prints 10 so temp+=1; has no effect no the private member myInt. Can u explain please? Thank all Bro!

Share this post


Link to post
Share on other sites
First, there is no reference in Java. You can think of working always with pointers.

Second, temp+=1 creates a new object with value of temp + 1 and assigns this to temp.
The list returned is in fact the private member of prova (or a pointer to this).

Share this post


Link to post
Share on other sites
Remember, all Java Object types are references. This is distinct from the C++ concept of a reference. So in the first case, a copy of a reference/object to the list is returned, and then the list is manipulated. In C++, this would be similar (deallocation omitted):

class prova
{
list<int *> *list;

public:
prova()
{
list = new list<int *>();
list->add( new int(9) );
list->add( new int(4) );
list->add( new int(1) );
list->add( new int(3) );
list->add( new int(8) );
list->add( new int(1) );
list->add( new int(2) );
}

list<int *> *getList()
{
return list;
}

// print implementation
}

int main() {

prova *p = new prova();
p->print();
list<int *> *mylist = p->getList();
mylist->remove(0);
p->print();
}




The second case is different. Java supports Autoboxing, which can implicitly convert primitive types to reference/object types and vice versa. For the primitive type int, the reference/object type is Integer.

I believe the code you posted would be similar to this in C++:

class prova
{
int *myInt;

public:
prova()
{
myInt = new int(10);
}

int *getInt()
{
return myInt;
}

void print()
{
// do printing
}
}

int main() {

prova *p = new prova();
p->print();

int *temp = p->getInt();

temp = new int(*temp + 1);

p->print();
}



So you can see that Java references/objects are similar to pointers, in that you can manipulate the pointed-at thing but not the reference itself. So in the first example, a reference to the list member was returned so you could manipulate the list. In the second, you could get the reference to the integer member, but you cannot assign to the reference itself. You can manipulate the integer itself, but AFAIK Integers are immutable (once created, they expose no way of changing their state). I hope this illustrates the difference in a way a C++ programmer can understand.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this