The Artima Developer Community
Sponsored Link

Java Answers Forum
reallocation of memory in Java

2 replies on 1 page. Most recent reply: May 10, 2004 3:31 AM by Sushil Srivastava

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
limor

Posts: 1
Nickname: limorh
Registered: May, 2004

reallocation of memory in Java Posted: May 9, 2004 12:42 PM
Reply to this message Reply
Advertisement
Hi,
I am just starting to learn java, though I know C++ very good.
I would like to know how can I reallocate mem in Java.
suppose I have created an array of 10 items of class A, and now I want to reallocate 12 items, should i just "free" mem, by setting my array to null, and than call to new again while allocating 12 elements ?
Is this is how it should be done in java ?
whare are my options?
10x.


twc

Posts: 129
Nickname: twc
Registered: Feb, 2004

Re: reallocation of memory in Java Posted: May 9, 2004 4:44 PM
Reply to this message Reply
> Hi,
> I am just starting to learn java, though I know C++ very
> good.
> I would like to know how can I reallocate mem in Java.
> suppose I have created an array of 10 items of class A,
> and now I want to reallocate 12 items, should i just
> "free" mem, by setting my array to null, and than call to
> new again while allocating 12 elements ?
> Is this is how it should be done in java ?
> whare are my options?

You don't have to mess with reallocation in Java. That is what the garbage collector is for.
//creates an array of 10 objects.
Object[] someObject = new Object[10];
 
//sometime later old array is discarded, and new 12 element array is created.  
someObject = new Object[12]; //Garbage collector will reallocate the memory from the discarded array.  Not your problem.
 

Sushil Srivastava

Posts: 6
Nickname: sushilsri
Registered: May, 2004

Re: reallocation of memory in Java Posted: May 10, 2004 3:31 AM
Reply to this message Reply
Use Vectors instead of Array. Vector allow dynamic resizing unlike Arrays.

Flat View: This topic has 2 replies on 1 page
Topic: Class path problem Previous Topic   Next Topic Topic: Problem with input from keyboard

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use