Results 1 to 4 of 4
- 01-12-2017, 01:47 PM #1
Member
- Join Date
- Jan 2017
- Posts
- 2
- Rep Power
- 0
- 01-12-2017, 05:33 PM #2
Member
- Join Date
- Dec 2016
- Posts
- 20
- Rep Power
- 0
Re: Stack and Heap memory object allocation
Looks right to me.
Primitives data types also go on the stack.
- 01-12-2017, 10:54 PM #3
Re: Stack and Heap memory object allocation
If I'm not mistaken, guys, correct me if I'm wrong: the literal String "Hello" in the constructor for s3 (and "Hai" in s4) is also stored in the String pool, but it adds an extra String object on the heap that points to this pooled String.
That's why s1 == s2 will be true (same object reference) and s1 == s3 is false (different object references). Using s1.equals(s3) will be true, because that compares content and not reference."It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2™ © 2013
- 01-13-2017, 02:13 AM #4
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 13
Re: Stack and Heap memory object allocation
I believe you are correct. Here is some test code. It presumes the default hash code is the memory address of the object.
Java Code:public class StringMemory { public static void main(String[] args) { String a = new String("hello"); System.out.println(Integer.toHexString(System.identityHashCode(a))); System.out.println(Integer.toHexString(System.identityHashCode(a.intern()))); } }
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
Similar Threads
-
Memory allocation of an OBJECT
By sathishvegeta in forum New To JavaReplies: 8Last Post: 04-22-2013, 05:28 PM -
Memory Allocation
By nathey in forum New To JavaReplies: 1Last Post: 07-18-2011, 04:48 AM -
object stack memory id apeears instead of object itself
By Embercloud in forum New To JavaReplies: 7Last Post: 01-02-2011, 06:06 PM -
JNI Memory Allocation
By Smokin' Caterpillar in forum Advanced JavaReplies: 3Last Post: 09-16-2010, 06:00 PM -
Memory Allocation
By kishan in forum New To JavaReplies: 3Last Post: 09-19-2009, 06:47 PM
Bookmarks