Results 1 to 8 of 8
  1. #1
    vinoth is offline Member
    Join Date
    Aug 2009
    Posts
    21
    Rep Power
    0

    Question An Interview Question.

    I recently faced this Question in an Interview. I could not crack it. Please help buddies.
    The question is..
    In C, we use malloc() to allocate memory dynamically. Similarly in Java, if we need to allocate memory dynamically how will you solve it? :confused:

  2. #2
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    Use new.

    -Gary-

  3. #3
    vinoth is offline Member
    Join Date
    Aug 2009
    Posts
    21
    Rep Power
    0

    Default

    Can u explain in detail?

  4. #4
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    Quote Originally Posted by vinoth View Post
    Can u explain in detail?
    Seriously? You're interviewing for Java programming jobs, and you want an explanation of new? Maybe I am misunderstanding you, since you posted in the New to Java forum. What was the interview for?

    -Gary-

  5. #5
    vinoth is offline Member
    Join Date
    Aug 2009
    Posts
    21
    Rep Power
    0

    Default

    The interview covered C,C++, and Java.
    Well, I know 'new' is used to instantiate an Object.

    It is used as..
    <class_name> <object_name>= new <class_name()>;

    But my question is.. How is 'new' equivalent to 'malloc()'??

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,225
    Rep Power
    20

    Default

    So, you know what new does...surely you can figure out where the equivalence is?

  7. #7
    dgaur's Avatar
    dgaur is offline Member
    Join Date
    Mar 2010
    Location
    Noida, india
    Posts
    7
    Rep Power
    0

    Post Malloc and New

    Actually you can not allocate memory dynamically in java like you do in C/C++ using malloc.
    Because in malloc you can specify the size of the memory which you want to allocate but in java thought we have new operator but we can not specify the size of the memory in new. ;)

    The size of the memory is calculated automatically by JVM execution engine.
    That's why Java is secure language. :cool:

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

    Default

    Quote Originally Posted by dgaur View Post
    Actually you can not allocate memory dynamically in java like you do in C/C++ using malloc.
    Because in malloc you can specify the size of the memory which you want to allocate but in java thought we have new operator but we can not specify the size of the memory in new. ;)
    Java Code:
    byte[] memory= new byte[42];
    kind regards,

    Jos

Similar Threads

  1. help for java interview
    By javastuden in forum New To Java
    Replies: 2
    Last Post: 03-09-2010, 10:24 AM
  2. Java Interview Question and Answer
    By gnomeom in forum Java Software
    Replies: 3
    Last Post: 06-04-2009, 12:53 PM
  3. Java Programmer for an Interview
    By meili in forum Reviews / Advertising
    Replies: 0
    Last Post: 01-18-2009, 03:49 PM
  4. Java Threads Interview Question
    By _tony in forum Threads and Synchronization
    Replies: 20
    Last Post: 12-22-2008, 09:31 AM

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
  •