aspose file tools*
The moose likes Beginning Java and the fly likes which code is more efficient Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "which code is more efficient" Watch "which code is more efficient" New topic
Author

which code is more efficient

rakhee gupta
Ranch Hand

Joined: May 01, 2008
Posts: 43
Out of these 2 piece of code which one will use less memory and why?Also what is the difference between these 2 piece of codes.

1.Contact [] ca = new Contact[10];
while(x<10)
{
ca[x]=new Contact();
x=x+1;
}

2.Contact ca ;
while(x<10)
{
ca = new Contact();
x=x+1;
}
Vishal Matere
Ranch Hand

Joined: Jan 22, 2008
Posts: 81
Originally posted by rakhee gupta:


Rakhi,
1. its ca is array of contacts. So it will consume more space.10 contact objects will be created & stored in array ca.
2.ca is single object so it will consume less space. Inside loop you are creating 10 contact objects but only 1 contact object will retain reference. All other contact objects are eligible for garbage collection

HTH
V


SCJP <br />SCWCD <br />SCBCD <br />SCEA-1
rakhee gupta
Ranch Hand

Joined: May 01, 2008
Posts: 43
You mean that in first case 10 active references to 10 objects will be there in the memory heap. but in 2nd case only 1 will be active at a time but then what about the other 9 contacts in the 2nd case will they be stored anywhere and can we access them any how?
Vishal Matere
Ranch Hand

Joined: Jan 22, 2008
Posts: 81
Originally posted by rakhee gupta:
In 2nd case only 1 will be active at a time but then what about the other 9 contacts in the 2nd case will they be stored anywhere and can we access them any how?


NOW you cant access them once you have lost reference to those 9 objects. You should have stored that reference inside the method somewhere , so that you can again access them

V
rakhee gupta
Ranch Hand

Joined: May 01, 2008
Posts: 43
Thanks for the help.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36514
    
  16
Why are you worrying about 10 object references' difference? The two bits of code are totally different in that one creates and retains 10 Contacts and that the other loses 9 of them. If you need 10 Contacts you need 10 Contacts. You are probably sitting in front of enough silicon to hold 1000000 Contacts easily.
Bill Shirley
Ranch Hand

Joined: Nov 08, 2007
Posts: 457
Because Java is such a high-level language, it's hard to say for sure how much memory or CPU might be used on any given piece of code.

I can write a JVM (theoretically) that could break any assumptions you have about what occurs with your code.


Bill Shirley - bshirley - frazerbilt.com
if (Posts < 30) you.read( JavaRanchFAQ);
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

Originally posted by Bill Shirley:

I can write a JVM (theoretically) that could break any assumptions you have about what occurs with your code.


Well, yeah, but no. If your JVM made Vishal Matere's solution untrue, then it would not be a compliant VM.


[Jess in Action][AskingGoodQuestions]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: which code is more efficient
 
Similar Threads
array
IllegalArgumentException
Doubt in Array
Help with Chess?
Q on Label statements