Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By JosAH

Thread: What happens in the memory?

  1. #1
    jaylimix is offline Member
    Join Date
    May 2011
    Posts
    29
    Rep Power
    0

    Default What happens in the memory?

    Suppose I write this statement:

    JButton btn1= new JButton();

    How many memory space is created? Is it two memory space or one?

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

    Default Re: What happens in the memory?

    Quote Originally Posted by jaylimix View Post
    Suppose I write this statement:

    JButton btn1= new JButton();

    How many memory space is created? Is it two memory space or one?
    The JButton object itself is created on the heap (all objects are created on the heap) and depending on your reference variable btn, whether it is a local variable or not, it is created on the stack (if it's a local variable) or also on the heap (if it's a member variables).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    jaylimix is offline Member
    Join Date
    May 2011
    Posts
    29
    Rep Power
    0

    Default Re: What happens in the memory?

    Okay suppose I write:
    JButton btn1= new JButton("Increase");
    This would mean that the word Increase has been passed into the heap named btn1 correct?
    What if I write:
    btn1.setText("Decrease");
    What actually happens to the memory? Is it correct to say that Decrease has been passed into the heap named as setText? What relation does btn1 and setText has? I understand that setText() is contained in the JButton class and btn1 is a JButton object but I don't understand what happens in the memory.

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

    Default Re: What happens in the memory?

    Quote Originally Posted by jaylimix View Post
    Okay suppose I write:
    JButton btn1= new JButton("Increase");
    This would mean that the word Increase has been passed into the heap named btn1 correct?
    What if I write:
    btn1.setText("Decrease");
    What actually happens to the memory? Is it correct to say that Decrease has been passed into the heap named as setText? What relation does btn1 and setText has? I understand that setText() is contained in the JButton class and btn1 is a JButton object but I don't understand what happens in the memory.
    The string "Increase" is another object; a String type object. It is also stored in the heap and your code snippet passes a pointer (or a 'reference' as they call it) to the constructor of the JButton class. It uses it for the text on the button. The new JButton object is also stored on the heap (with a part of it 'pointing' to that "increase" String object on the heap). Objects themselves stay on that heap as long as at least one reference, points (or 'refers') to them. Otherwise the garbage collector removes them from the heap because the objects are not needed anymore by your program. All your variables (except primitives such as int, double etc.) are 'pointers' (or 'references') to objects on the heap.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    jaylimix is offline Member
    Join Date
    May 2011
    Posts
    29
    Rep Power
    0

    Default Re: What happens in the memory?

    So what you mean is there are two objects on the heap with two separate memory locations? One only points but the other one contains Increase?

    Okay that would mean that btn1.setText("Decrease") also means that btn1 points and setText() contains Decrease?

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

    Default Re: What happens in the memory?

    Quote Originally Posted by jaylimix View Post
    So what you mean is there are two objects on the heap with two separate memory locations? One only points but the other one contains Increase?

    Okay that would mean that btn1.setText("Decrease") also means that btn1 points and setText() contains Decrease?
    JButton.setText( ... ) is a method and methods don't 'contain' any data; they manipulate data and this method stores the reference to the String somewhere in the JButton object (refered to by the btn1 reference variable).

    kind regards,

    Jos
    Solarsonic likes this.
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 1
    Last Post: 07-18-2011, 04:24 PM
  2. Replies: 2
    Last Post: 08-21-2008, 07:33 PM
  3. Out of memory
    By mew in forum New To Java
    Replies: 1
    Last Post: 01-20-2008, 08:55 AM
  4. Memory
    By mew in forum CLDC and MIDP
    Replies: 0
    Last Post: 12-28-2007, 11:02 AM

Posting Permissions

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