0

So I was reading this book where it says that if I create a class Point and then instantiate it by doing Point p1 = new Point(); then I will have:

  • a Point object on the heap (as a result of the "new" keyword);
  • a reference to this object (p1);
  • and when an object has no references then it can be disposed by the garbage collector.

I guess I got the meaning, but it got me thinking. What happens "memory-wise" with primitive types and strings, i.e. what is the difference between:

 - int x1 = 100;
 - String s1 = "hello";

and

 - int x2 = new Integer(100);
 - String s2 = new String("hello");

In the first case, are '100' and 'hello' going to be instantiated and stored on the heap? Else, what are x1 and s1 referencing?

Thank you!

trincot
  • 317,000
  • 35
  • 244
  • 286
PLB
  • 881
  • 7
  • 20
  • 1
    possible duplicate: [Does the Java primitives go on the Stack or the Heap?](http://stackoverflow.com/questions/3646632/does-the-java-primitives-go-on-the-stack-or-the-heap) – zeller Jul 11 '12 at 16:18
  • 2
    A String is not a primitive, neither an `Integer`. But an `int` is. – SJuan76 Jul 11 '12 at 16:19
  • I know, that is why I made the distinction "primitive types and strings", and my question is particularly oriented on x1 and s1. – PLB Jul 11 '12 at 16:24

3 Answers3

2

First of all:

 int x2 = new Integer(100);

This means an Integer object is created, outboxed (the JVM returns its intValue) and 100 assigned to an int primitive. The Integer object is no longer referenced and can be disposed (of course maybe the JIT can optimize that to int x2 = 100).

I assume you are talking about local variables, because attributes are part of the object and so lie with it in the heap.

 int x1 = 100;

An int variable is declared in the stack and assigned a value

 String s1 = "Hello";

An String object is created (or referenced, see String.intern()) and a pointer is added to the stack.

The other possibilities are exactly the same.

SJuan76
  • 24,532
  • 6
  • 47
  • 87
  • thank you, it makes it much clearer. One more question (but maybe it would deserve an own post): - what about methods belonging to an object? are they also on the heap with it? I am having a hard time figuring out how the memory of my programs is "organised"... – PLB Jul 11 '12 at 21:38
  • 1
    Methods are part of the class definition (which is by itself an object of class `java.lang.Class`). When you load the class A, the object `Class A` is created with all the method info, but when you instantiate an object of class A its methods do not need extra space. When you execute one of the objects methods, the JVM goes to the `Class A` object and retrieves the code for it from it, not from each instance of A. – SJuan76 Jul 11 '12 at 23:41
1

local primitives will be put onto the stack, but a member field of an object will be put on the heap with that object (whether it is primitive or not). Some more info here.

strings are objects and exist on the heap. They do work a little differently though, some info here.

Community
  • 1
  • 1
Dima
  • 23,484
  • 6
  • 56
  • 83
0

Both S1 and S2 will create new memory references where the value is stored. Primitive data types may refer to a stack or to a heap depending on to circumstances. You can have a look here

Community
  • 1
  • 1
Subhajit Roy
  • 111
  • 3
  • 14