Results 1 to 20 of 20
Like Tree10Likes
  • 1 Post By GregBrannon
  • 1 Post By Toll
  • 1 Post By DarrylBurke
  • 1 Post By jim829
  • 1 Post By Toll
  • 1 Post By Toll
  • 1 Post By kjkrum
  • 2 Post By jim829
  • 1 Post By gimbal2

Thread: basic doubt how to allocate memory

  1. #1
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default basic doubt how to allocate memory

    Hi,
    I'm back

    is there an API for printing a pointer using
    Java Code:
    "System.out.println();"
    Arrays can be incremented by pointer?

    regards
    dhilip

  2. #2
    GregBrannon is offline Member
    Join Date
    Mar 2013
    Location
    Southern Maryland, USA
    Posts
    5
    Rep Power
    0

    Default Re: basic doubt how to allocate memory

    One doesn't allocate memory - at least not explicitly - in Java. But the body of your question is slightly different than the topic title, so I'll expand the discussion.

    I'm sure you know this, but I'm trying to understand your question: The elements of Java Arrays are accessed by an index. Where index = i, the (i + 1) element of array[] is array[i]. The beginning memory location of the array[] can be printed by simply:

    System.out.println( array );

    The offset of each successive memory location must be calculated = the array index, i, * the number of bits allocated for the data type of each array member.

    I've never had a need to do this, and I wouldn't recommend that anyone use Java in a way that requires them to determine the memory location of an array element. Besides, once you have it, what would you do with it?

    If I've completely misunderstood your question, please clarify.
    noobplus likes this.

  3. #3
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default Re: basic doubt how to allocate memory

    what about this..
    is there a need to use linked list in Java?

    sorry for too many questions..

    regards
    dhilip

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default Re: basic doubt how to allocate memory

    Quote Originally Posted by GregBrannon View Post
    The beginning memory location of the array[] can be printed by simply:

    System.out.println( array );
    No it can't.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default Re: basic doubt how to allocate memory

    Sorry, hold on, I will get what my doubt is, and I will come back..

    regards
    dhilip

  6. #6
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    393
    Rep Power
    4

    Default Re: basic doubt how to allocate memory

    LinkedList has its places, much like everything else... I guess technically you could use arrays for everything you can use a LinkedList for, but in some cases, LinkedList are easier to use.
    noobplus likes this.

  7. #7
    GregBrannon is offline Member
    Join Date
    Mar 2013
    Location
    Southern Maryland, USA
    Posts
    5
    Rep Power
    0

    Default Re: basic doubt how to allocate memory

    No it can't.
    Well, perhaps I wandered into an area lightly traveled and oversimplified, but running the simple demo below will show you what you DO get, and it has something to do with the object's memory location - not that one can do anything with it. Perhaps you could elaborate.
    Java Code:
    public class TestClass
    {
        public static void main( String[] args )
        {
            int[] myArray = new int[10];
            
            System.out.println( myArray );
        }
    }

  8. #8
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    268
    Rep Power
    2

    Default Re: basic doubt how to allocate memory

    Quote Originally Posted by GregBrannon View Post
    Well, perhaps I wandered into an area lightly traveled and oversimplified, but running the simple demo below will show you what you DO get, and it has something to do with the object's memory location - not that one can do anything with it. Perhaps you could elaborate.
    Java Code:
    public class TestClass
    {
        public static void main( String[] args )
        {
            int[] myArray = new int[10];
            
            System.out.println( myArray );
        }
    }
    it prints the toString method if you just put the variable there, which is the class name @ to hashcode in hex. It is somehow based on the memory address but objects are moved and there is not a
    certain way of knowing.

  9. #9
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    393
    Rep Power
    4

    Default Re: basic doubt how to allocate memory

    Looking through the source for Object, this is what I found:

    Java Code:
        /**
         * Returns a string representation of the object. In general, the 
         * <code>toString</code> method returns a string that 
         * "textually represents" this object. The result should 
         * be a concise but informative representation that is easy for a 
         * person to read.
         * It is recommended that all subclasses override this method.
         * <p>
         * The <code>toString</code> method for class <code>Object</code> 
         * returns a string consisting of the name of the class of which the 
         * object is an instance, the at-sign character `<code>@</code>', and 
         * the unsigned hexadecimal representation of the hash code of the 
         * object. In other words, this method returns a string equal to the 
         * value of:
         * <blockquote>
         * <pre>
         * getClass().getName() + '@' + Integer.toHexString(hashCode())
         * </pre></blockquote>
         *
         * @return  a string representation of the object.
         */
        public String toString() {
    	return getClass().getName() + "@" + Integer.toHexString(hashCode());
        }
    So what you likely mistook for the memory address is the hash value for the object in question. And, technically, arrays are objects (at least as far as I know).

  10. #10
    GregBrannon is offline Member
    Join Date
    Mar 2013
    Location
    Southern Maryland, USA
    Posts
    5
    Rep Power
    0

    Default Re: basic doubt how to allocate memory

    Thanks. So pretty much useless.

  11. #11
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default Re: basic doubt how to allocate memory

    Quote Originally Posted by GregBrannon View Post
    Well, perhaps I wandered into an area lightly traveled and oversimplified, but running the simple demo below will show you what you DO get, and it has something to do with the object's memory location
    Agreed that it may have *something* to do with the memory address. See the documentation for Object#toString() and hashCode(), specifically this (emphasis added):
    (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language.)
    db
    noobplus likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

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

    Default Re: basic doubt how to allocate memory

    Arrays don't have pointers, they have indices. And historically, the concept originates from mathematics. Ever seen something like an? Well that would correspond to a[n] for some value n within a certain range of 0 thru the size of the array.

    And here is something that I can't swear by but I believe to be true (those in the know will certainly correct me).

    Java does not provide for direct memory allocation other than normal variable declarations. It isn't needed. Normally, a buffer pool would be used to manage a series of buffers for some task. But as Java has automatic garbage collection, any memory which is needed is allocated as needed and when done is garbage collected. So in some sense, the entire JRE acts as one large memory management facility.

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

  13. #13
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default Re: basic doubt how to allocate memory

    Hi,
    could we use ping() on Strings?

    Java Code:
    public static void ping(char x[], char y[])
    {
    	j=y.length;
    	for( i=0;i<y.length;i++)
    		x[i]=y[j-i-1];
    }
    I was trying for
    Java Code:
    public static void ping(String x,String y)
    {
    j=y.length();
    for(//
    x.substring(i)=y.substring(j-i);
    }
    gives LValue error

    EDITED TO ADD: what is difference between string and char, string is array? is it different from char?
    int intarr[]={1,2,3};
    String str=new String(intarr);
    didn't work for me
    please help,

    regards
    dhilip
    Last edited by noobplus; 07-13-2013 at 09:08 PM.

  14. #14
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    393
    Rep Power
    4

    Default Re: basic doubt how to allocate memory

    Hmm. Your ping() is meant to just revert a String? Then you'd have to make it return a String; you can't modify an existing String. After that, you can make use of charAt(i) to get a single character from a String and use that to build the new String, one character at a time.
    noobplus likes this.

  15. #15
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default Re: basic doubt how to allocate memory

    Quote Originally Posted by Toll View Post
    you can't modify an existing String. .
    Actually, I want to come to the point, in interview, they asked me why String cannot be modified, and what is diference between string and char, and I blinked, the the HR asked me to come 1 week later after studying

    regards
    dhilip

  16. #16
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    393
    Rep Power
    4

    Default Re: basic doubt how to allocate memory

    Oh. Then ask that question instead of dancing around the subject. Unfortunately, it's been a while since I've been to school, so I'm unsure as to the reasons they made String unchangeable; it's just how it is, and I've never really thought enough about it (or even wanted them to be changeable) to bother digging into it, so I'm going to bow out and let someone else answer that question, heh.

    The difference between String and char is simple though. String is a class that represents a string of characters (i.e. "abc" is a string) as well as having methods to manipulate the String or otherwise work on it. char is a single character (i.e. 'a'). char arrays (i.e. char[]) are several characters that can kinda work as a String; you can create char-arrays from Strings, and you can create Strings from char-arrays.
    noobplus likes this.

  17. #17
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

  18. #18
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

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

    Default Re: basic doubt how to allocate memory

    Strings are used frequently as keys to maps. If they weren't immutable, then someone could change the string and corrupt the map. This was actually mentioned by Jos in an earlier thread somewhere.

    Regards,
    Jim
    kjkrum and noobplus like this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  20. #20
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,365
    Rep Power
    6

    Default Re: basic doubt how to allocate memory

    That and also for performance reasons - when you can't change the String the underlying code can make very easy and safe assumptions in stead of needing to question the state every time it is used. On top of that no synchronization is needed to make it thread safe.

    Immutability is just a really good idea, you should apply it when you can.
    noobplus likes this.

Similar Threads

  1. Unable to allocate more memory to java
    By musdem in forum New To Java
    Replies: 7
    Last Post: 06-06-2012, 01:27 PM
  2. Allocate memory for array of Objects.
    By Splanger in forum New To Java
    Replies: 7
    Last Post: 07-03-2011, 09:15 PM
  3. Synth.allocate null pointer exception
    By stevonator in forum Java Applets
    Replies: 0
    Last Post: 04-25-2011, 12:21 PM
  4. Allocate memory
    By thescratchy in forum New To Java
    Replies: 1
    Last Post: 12-17-2010, 11:49 PM
  5. SEVERE: Allocate exception for servlet ServletHandler
    By shyameni in forum Java Servlet
    Replies: 2
    Last Post: 03-05-2010, 01:35 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
  •