Results 1 to 4 of 4
  1. #1
    sandybo is offline Member
    Join Date
    Jan 2017
    Posts
    2
    Rep Power
    0

    Default Stack and Heap memory object allocation

    Can any one let me know did this object allocation is right in stack and heap please check Image attached
    Attached Thumbnails Attached Thumbnails Stack and Heap memory object allocation-stackheap.png  

  2. #2
    scarecrowkid is offline Member
    Join Date
    Dec 2016
    Posts
    20
    Rep Power
    0

    Default Re: Stack and Heap memory object allocation

    Looks right to me.

    Primitives data types also go on the stack.

  3. #3
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,993
    Rep Power
    9

    Default 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

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default 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())));
       }
    }
    The code prints out two different values implying two different memory locations.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Memory allocation of an OBJECT
    By sathishvegeta in forum New To Java
    Replies: 8
    Last Post: 04-22-2013, 05:28 PM
  2. Memory Allocation
    By nathey in forum New To Java
    Replies: 1
    Last Post: 07-18-2011, 04:48 AM
  3. object stack memory id apeears instead of object itself
    By Embercloud in forum New To Java
    Replies: 7
    Last Post: 01-02-2011, 06:06 PM
  4. JNI Memory Allocation
    By Smokin' Caterpillar in forum Advanced Java
    Replies: 3
    Last Post: 09-16-2010, 06:00 PM
  5. Memory Allocation
    By kishan in forum New To Java
    Replies: 3
    Last Post: 09-19-2009, 06:47 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
  •