Results 1 to 9 of 9
  1. #1
    chardsigkit is offline Member
    Join Date
    Mar 2011
    Posts
    5
    Rep Power
    0

    Default Assigning an existing class to a new variable (memory issue)

    String name = new String("Something");
    String other = name;

    Does the second line allocates new memory or creates new String instance for 'other' or a reference to 'name' is just assigned to it? Thanks!

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,998
    Blog Entries
    7
    Rep Power
    19

    Default

    Quote Originally Posted by chardsigkit View Post
    String name = new String("Something");
    String other = name;

    Does the second line allocates new memory or creates new String instance for 'other' or a reference to 'name' is just assigned to it? Thanks!
    'name' and 'other' are two separate references that both 'point' to the same String object with contents "Something".

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    chardsigkit is offline Member
    Join Date
    Mar 2011
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    'name' and 'other' are two separate references that both 'point' to the same String object with contents "Something".

    kind regards,

    Jos
    So in a way, no memory is wasted?

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,998
    Blog Entries
    7
    Rep Power
    19

    Default

    Quote Originally Posted by chardsigkit View Post
    So in a way, no memory is wasted?
    In your example one additional String object is created but isn't necessary, better make that:

    Java Code:
    String name = "Something"; // one String object
    String other = name; // another reference to it
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    chardsigkit is offline Member
    Join Date
    Mar 2011
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    In your example one additional String object is created but isn't necessary, better make that:

    Java Code:
    String name = "Something"; // one String object
    String other = name; // another reference to it
    kind regards,

    Jos
    Oh I see. Thanks Jos. But here's the thing, I have an instance of a class called Simulation. Inside it I have an instance of a class called ControlPanel. It looks something like this:

    //Simulation Class
    public class Simulation{
    int x;
    int y;
    ....
    ControlPanel simControlPanel;

    public Simulation(int x, int y){
    this.x = x;
    this.y = y;
    simControlPanel = new ControlPanel();
    }

    private void someFunction(){
    ...
    simControlPanel.someFunc(this);
    }
    }

    //ControlPanel Class
    public class ControlPanel{
    Simulation parentSim;

    public ControlPanel(){
    ...
    }

    ...
    ...

    public someFunc(Simulation parentSim){
    this.sim = parentSim;
    ...
    }

    private someOtherFunc(){
    sim.setX(someX);
    sim.setY(someY);
    }
    }

    So if I called someFunc of the simControlPanel, would this.sim = parentSim create a new copy of parentSim or a reference only? I'm saving a copy of parentSim inside the ControlPanel class because I also need to access the parentSim in other functions inside ControlPanel (ex. someOtherFunc). Thanks again Jos!

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,998
    Blog Entries
    7
    Rep Power
    19

    Default

    No new object is created; just another copy of the reference to the one and only object is passed around. If/when you want a new object, it has to be created somewhere with the 'new' operator. Java is not C++ and doesn't have value semantics for non-primitive (i.e. class) objects.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    chardsigkit is offline Member
    Join Date
    Mar 2011
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    No new object is created; just another copy of the reference to the one and only object is passed around. If/when you want a new object, it has to be created somewhere with the 'new' operator. Java is not C++ and doesn't have value semantics for non-primitive (i.e. class) objects.

    kind regards,

    Jos
    Great! Now I can move on with my project. But I got a little confused with the String explanation so I would like to clear the first example. You said here that no new object is created. But in the first example:

    String name = new String("Something");
    String other = name;

    How come an additional String object is created?

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,998
    Blog Entries
    7
    Rep Power
    19

    Default

    Quote Originally Posted by chardsigkit View Post
    Great! Now I can move on with my project. But I got a little confused with the String explanation so I would like to clear the first example. You said here that no new object is created. But in the first example:

    String name = new String("Something");
    String other = name;

    How come an additional String object is created?
    "Something" already is a String object (the compiler took care of that) and the constructor new String(<another String>) creates another String object. You don't need that other object so "Something" is enough.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    chardsigkit is offline Member
    Join Date
    Mar 2011
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    "Something" already is a String object (the compiler took care of that) and the constructor new String(<another String>) creates another String object. You don't need that other object so "Something" is enough.

    kind regards,

    Jos
    Oh, so the 'additional string' was created at 'name'. I thought you were pertaining to 'other'. Thanks Jos, have a good day. :)

Similar Threads

  1. Java Memory Issue
    By personal in forum Advanced Java
    Replies: 12
    Last Post: 01-07-2012, 02:05 PM
  2. Replies: 3
    Last Post: 11-23-2010, 02:19 PM
  3. Replies: 0
    Last Post: 11-23-2010, 12:32 PM
  4. help required to solve jasper report memory issue
    By kiranrajan in forum Advanced Java
    Replies: 0
    Last Post: 11-04-2009, 09:25 AM
  5. Replies: 3
    Last Post: 11-24-2008, 07:01 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •