Y in java objects are created on heap only and not on stack ??

Hi ,
I need a answer to the question " why all the objects are created on heap only in java and not on stack " ?
In java a object is created when we specify new i.e. for example,
consider the following cases.
1.
Vector v = new Vector();
here object is created and the reference to this object is assigned to variable v. okie.
2.
when i write something like
Vector vec ;
Here i am creating reference variable for Vector. No object is created and no seperate memory is allocated for it. Reference variablesa are stored along with other primitive variables on the java stack.
My question is, in C++ by writing case 2 i can create an object and it is creating this object on stack. So why in java object is not created on stack while i do like this ? Why in java objects are created on heap only ?
If my question is out of interest of this forum i apologize for it. But if somebody can throw some light on it, it would be of great help.
Cheers !!
Dipesh

Hi
I asked this question just to understand how compiler and jvm cud have been designed......i.e. to know the philosophy behind designing language.......
now what i cud surmise is that if objects were to be stored in stack then on calling garbage colllector , it may delete some unused obj and unreferenced object(unreachable object) and this in turn may give rise to non-contagious areas in stack, thus nullifying the garbage collector's objective of freeing the the memory. Again deallocating that intersperse spaces into one free block of space will be bit troublesome task.....
So considering above factor the designer of jvm and java compiler have opted for storing objects on heap rather than stack.....
If u have something to say bout this.......pls go ahead.......
Cheers !!
Dipesh

Similar Messages

  • Y in java objects are created on heap only n not on stack ?

    Hi ,
    I need a answer to the question " why all the objects are created on heap only in java and not on stack " ?
    In java a object is created when we specify new i.e. for example,
    consider the following cases.
    1.
    Vector v = new Vector();
    here object is created and the reference to this object is assigned to variable v. okie.
    2.
    when i write something like
    Vector vec ;
    Here i am creating reference variable for Vector. No object is created and no seperate memory is allocated for it. Here this reference is placed on the java stack along with other primitive data types.
    My question is, in C++ by writing case 2 i can create an object and it is creating this object on stack. So why in java object is not created on stack while i do like this ? Why in java objects are created on heap only ?
    If my question is out of interest of this forum i apologize for it. But if somebody can throw some light on it, it would be of great help.
    Cheers !!
    Dipesh

    Cross post!
    http://forum.java.sun.com/thread.jsp?forum=32&thread=260589

  • How many java String objects are created in string literal pool by executin

    How many java String objects are created in string literal pool by executing following five lines of code.
    String str = "Java";
    str = str.concat(" Beans ");
    str = str.trim();
    String str1 = "abc";
    String str2 = new String("abc").intern();
    Kindly explain thanks in advance
    Senthil

    virtuoso. wrote:
    jverd wrote:
    In Java all instances are kept on the heap. The "String literal pool" is no exception. It doesn't hold instances. It holds references to String objects on the heap.Um, no.
    The literal pool is part of the heap, and it holds String instances.
    [http://java.sun.com/docs/books/jvms/second_edition/html/Overview.doc.html#22972]
    [http://java.sun.com/docs/books/jvms/second_edition/html/ConstantPool.doc.html#67960]
    You're referring to the JVM. That's not Java.It's part of Java.
    There is nowhere in Java where it is correct to say "The string literal pool holds references, not String objects."

  • Newbie Help: Java Object to create an image

    Hi there.
    I'm trying to create a Java class that I can call from Coldfusion that will build a GIF file based on parameters.
    The idea is, the Java object opens a "background" gif file and then writes some text onto it and saves the whole thing out as a GIF.
    Bearing in mind this class will be used directly by Coldfusion, and so isn't an applet or an application...can anyone tell me what is wrong with this code as I get a null pointer exception on the getGraphics() line:
    // Methods available to Coldfusion
    public String CreateOverlay( String imageFile, String outPath, String overlayText, int x, int y, String fontFace, int PointSize)
    try{
    Toolkit tk = Toolkit.getDefaultToolkit();
    Image im = tk.getImage(imageFile);
    Gif89Encoder enc = new Gif89Encoder();
    OutputStream out = new BufferedOutputStream(
    new FileOutputStream(outPath)
    Image newimg = createImage(100,50);
    ***** Graphics g = newimg.getGraphics();
    g.drawImage(im, 0, 0, 100, 50, this);
    g.drawLine(0,0,50,50);
    enc.addFrame( newimg );
    enc.encode(out);
    out.close();
    catch (Exception e) {
    System.out.println("Error...");
    e.printStackTrace();
    return "Dont care at the moment";
    Any help to this troubled newbie would be appreciated.

    Hi Kurt,
    You must call "setVisible(true)" on your Frame before
    creating the image.But, the thing is, this object doesnt have (or need) a GUI. Its just an object that sits on the server and its methods are called to generate an image on disk. Basically what happens is Coldfusion receives information from a user and it loads the java object, calls a method which then generates a new GIF file that is made up of a simple button background and some text (entered by the user)....it then returns the name of this file to Coldfusion and Coldfusion stores it in a database for later retrieval.
    In simple terms:
    COLDFUSION===========
    1. Creates an instance of the JAVA Object
    2. Call the generateGif method passing an image filename and some text eg:
    newFile=c.GenerateGif( "simplebutton.gif", "Some text for button");
    JAVA OBJECT========= (GenerateGif method)
    1. Load the GIF simplebutton.gif
    2. Create a new blank image
    3. Draw the loaded GIF onto the blank one
    4. Write the Text ("some text for button") onto the image
    5. Take the image and encode it to GIF format
    6. Write this GIF to disk
    7. Return the filename of the new GIF image just created
    8. Finish
    As you can see, I dont want or need a GUI.....
    Is this possible ?
    Thanks in Advance,
    Tony Johnson

  • How many Objects are created in this?

    Hi,
    How many objects are created in the flwg code:
    String str = new String("Hello");
    String str2 = "Hey there";
    str = str + str2;

    Depends if you're counting objects created at compile time.
    "Hello" is created at compile time. That's 1.
    new String("Hello") creates a new String object at runtime, pointing its backing array at the "Hello" in the constant pool. That's 2.
    "Hey there" is created at compile time. 3.
    str + str2 I might do everything at compile time, or it might do some at runtime. I don't know if str is considered a compile time literal here. I think not, but I'm not sure. You can check by using javap. If I'm correct, then at runtime you'll create a StringBuffer (4) and another String (5).
    So I'd say 5 objects. But if the compiler treats str as a literal and therefore can do the str+str2 at compile time, then it might do so without creating the StringBuffer. Or it might still create it at compile time, use it, and throw it out.

  • How many objects are created?

    I have this question which some one posted to me.
    He asked me how many objects are created when an object of a simple class is created for example
    class A{
    public static void main(String args[])
    A a1 = new A();
    Now how many objects are created? I think only one. Can anyone tell me if any other objects are also created.
    Plz tell me .

    No, the answer is indeed 1. All the other objects
    (including the String[] in the main method
    parameters) are created outside of the A
    class, either by the JVM itself or by some other
    class that calls the A.main() method. You can try
    this out for yourself by adding a constructor such as
    A()
    System.out.println("Creating class A");
    } and see what prints out.Take a closer look at the question:
    how many objects are created when an object of a
    simple class is created for exampleIt doesn't say "how many objects of type A are created".
    I.e. The VM has to load class A before you can create an object from it, and thus an object of type Class is created. There are also a bunch of other objects which needs to be created before you can instantiate A.
    Kaj

  • We are creating a photo book and on the cover in the lower right hand corner there is part of the title. We did not create it and cannot find how to delete it. Any ideas.

    We are creating a photo book and on the cover in the lower right hand corner there is part of the title. The cover title is centred below the cover picture. We did not create it and cannot find how to delete it.  Any ideas?

    Preview according to this article and see what it does. http://support.apple.com/kb/PH2486
    The printed book will match the preview
    LN

  • Order types, why do we need to create our order types and not use the stand

    HI
    Order types, why do we need to create our order types and not use the standard ones during implementation.

    Hi Raj,
    Well there is no hard and fast rule that you have to copy nad create a new order type. Its just to meet our own requirements that we copy the standard and make the changes if required to meet our business needs.
    And by this way we keep unchanged the standard order type for further use.
    If you are sure that the standard order type satisfies your need you can directly use that only.
    I hope this clarifies the issue.
    Do reward if find useful
    Regards,
    Abhi

  • What do you do when your contacts with IPHONES or IPADS are not showing up in blue in IMESSAGE on my IPAD. The are showing up in RED and not allowing me to IMESSAGE them at all.

    What do you do when your contacts with IPHONES or IPADS are not showing up in blue in IMESSAGE on my IPAD. The are showing up in RED and not allowing me to IMESSAGE them at all.

    It was suggested I move this question to IPhoto or IMovie which I did. 
    Well moving to a different discussion group did not provide an answer to this question either. But what I finally did was import one batch of photos and videos into IPhoto for a given day at a time. Working with these I could change the date and times in order to get them in the original sequench taken. Then I would create an album with that batch. These would all be on the same day (IMove was closed for this phase). Then I would open IMovie, generate the thumbnails for that album, and select the album I had created. This was necessary because the importing process in IPhoto was using incorrect dates for my video so it was a real struggle finding them in IMove until I developed this approach.
    I believe that this whole process was so screwy because I was importing from an external hard drive not a camera. I had these photos on a PC and did not have the original cameras to use to import directly which I am fairly sure would have made this easier!

  • I have 100's of CDs that I have downloaded to my itunes library and they are in the library playable but they do not show up in the C drive under the iTunes Music folder under users.  Why are they in the libary and not in the itunes music folder. Win 7 OS

    I have 100's of CDs that I have downloaded to my itunes library and they are in the library playable but they do not show up in the C drive under the iTunes Music folder under users.  Why are they in the libary and not in the itunes music folder. Win 7 OS.  When I look in the library under the get info screen it says they are in the folder itunes music, under the c: drive but when I actually go to that folder to look for them they are not there....HELP!

    Hello RumDog,
    I think this article will help you find the media in your library.
    Where are my iTunes files located?
    http://support.apple.com/kb/ht1391
    Discovering and changing the iTunes Media folder location
    Note: You would usually only change the iTunes Media folder location to share music between accounts on the same computer. See this article for specific steps on how to accomplish this. Also, see this article if you want to know how to move your music to a new computer.
    Mac OS X: Click the iTunes menu and choose Preferences.
    Windows: Click the Edit menu and choose Preferences.
    Click the Advanced pane. iTunes displays the location of your iTunes Media folder.
    You can then:
    Note where your media folder is, such as for backing up your media.
    Use the Reset button to reset it's location to the default (which is the iTunes folder).
    Click the Change button to select a folder for a new location. Once you change this location:
    If you make a new or alternate iTunes library, the new location will be used by that library.
    Existing files will not be moved unless you choose File > Library > Organize library and choose the option to "Consolidate files."
    From: Where are my iTunes files located?
              http://support.apple.com/kb/ht1391
    Cheers,
    Sterling

  • I recently got an iphone 5S and my songs not purchased from iTunes are grayed out (phone only) and are skipped past when playing them through my iPhone.

    I recently got an iphone 5S and my songs not purchased from iTunes are grayed out (phone only) and are skipped past when playing them through my iPhone. i have tried the following:
    1) Removing all music from phone and resynced to iTunes PC library.
    2) Erased phone and restored from back up
    3) Erased phone and started as a new iPhone
    4) Reformated the music files in my library that weren't playing
    5) Deleted songs from iPhone and computer and re-imported CDs
    6) Tried to redownload my COMPLETE MUSIC LIBRARY from iCloud.
    7) I have also turned it off and turned it back on several times
    8) I tried syncing through my macbook (only moved partial songs to it to see if it would even work).
    9) I have tried to get Siri to play them, did not work.
    10) All music does play fine on my iPod Touch 3rd (iOS6 i think) gen and iTunes on Mac and PC
    11)  I added a CD to my roommates iTunes and it plays on their iPhone 5S but not mine.
    After all of these things it is now WORSE!!!  My iTunes purchased songs are now experiencing the same issue along with some (not all) of the original music files that were skipping and grayed out.  My conclusion is i have no clue at this point!  I have done everything i have found online in EVERY discussion, i have spent over 60 hours trying to get this to work and i am asking myself, i bought this product why am having to fix this when it worked just fine on my iPod touch (3rd Gen) and iPod Classic!!!!  PLEASE APPLE FIX THIS ISSUE FOR US!!!! 

    I had this problem too after i upgdatd to OS X Mavericks... only the songs on my iphone 5s that i purchased from itunes would play.
    i read something about it being a syncing issue. when you plug your phone into your computer with the usb cable, the person said to switch from automatic syncing of itunes to the "manual" option. mine was already set to manual so i wasn't sure anything would work. BUT, when i plugged my phone into my computer, it started to sync all of my music on my phone again (top of itunes bar where song playing/time is listed said "now syncing 123/534 songs to x's iphone"). once that was completed, i checked the music app on my iphone and everything was back to normal!
    so, i would suggest setting your music syncing settings to manual if you haven't tried that yet!
    hope this helps!

  • Sp L records are coming to the PSA and not updating in the data target

    Special Ledger records are coming to the PSA and not updating in the data target in the month end and the info package is failing - because of that we are doing manual updating from PSA to the data target -
    Can so one can tell the reasons why this is happening? And give the solutions to it?

    Hi Sankar,
    If your Infopackage uses Only PSA and Update subsequently in Data Target on the Processing tab, then you will need to add a process for Read PSA and Update Data Target in your process chain, after the Infopackage load process. Then it will take data from the PSA and load to the data target.
    Hope this helps...

  • Hit something when editing presentation and now slides are all reduced in size and not filling screen.  Preferences, scale to fit doesn't work.  What did I do?

    Hit something when editing presentation and now slides are all reduced in size and not filling screen.  Preferences, scale to fit doesn't work.  What did I do?

    Please give complete details of your system and the media and the project properties.

  • New headset, but certain programs are transmitting sound through speakers and not headphones.

    Spoiler (Highlight to read)
    Hello,
    Hello,
    I just recently purchased a new headset for gaming but have been having some issues with it and the system sound. One major issue is when I call someone on skype, the sound from Skype or the call will go through the headphones, but any other audio comes out of the speakers. I have also notices that iTunes also does not function when I am in a call as well. Some examples would be if I were to want to watch a YouTube video while in a call, the sound would come through the speakers and not the headphones. However, I did find a fix for it. All I had to do was disable the main speakers and the audio was transmitted to the headphones. Another example would be if I wanted to run games via Steam (Garry's Mod, TF2, ect.), I wouldn't get any sound whatsoever from the game. Not through my speakers or headphones. With one exception, Garry's Mod volume I can hear through the speakers while in a Skype call, but I want the sound to come through the headphones. But with TF2, I get no sound whatsoever. If you need any more information about my issue, please feel free to ask. Thank you.
    This question was solved.
    View Solution.

    Hello @LayneStaley,
    Welcome to the HP Forums, I hope you enjoy your experience! To help you get the most out of the HP Forums I would like to direct your attention to the HP Forums Guide First Time Here? Learn How to Post and More.
    I have read your post on how certain programs are transmitting sound through speakers and not the headphones, and I would be happy to assist you in this matter!
    To ensure that your headphones are utilized when plugged into your notebook, I recommend following the steps below:
    1. Click the Start button
    2. Click Control Panel
    3. Click on Hardware and Sound (sorting the control panel icons by "category"), and then click on Sound
    4. Under Playback, you should see one or two speaker icons. One of them should already have a green checkmark next to it (which will be your notebook's main speakers). If the second Speakers icon is not visible, right-click in the list of devices and select Show Disabled Devices
    5. A second, disabled icon of "speakers" should appear. Right-click this icon and click Enable
    6. Select the newly enabled speaker device, right-click it, and select "Set as Default Device".
    7. Click Apply.
    Please re-post with the results of your troubleshooting, and I look forward to your reply!
    Regards
    MechPilot
    I work on behalf of HP
    Please click “Accept as Solution ” if you feel my post solved your issue, it will help others find the solution.
    Click the “Kudos, Thumbs Up" on the right to say “Thanks” for helping!

  • Why are my contacts showing numbers and not names

    Why are my contacts showing numbers and not names?

    When you added your contact details in the contact app did you fill in the fields correctly.  For example in contacts where it says "first" did you enter a number instead of a name?

Maybe you are looking for