aspose file tools*
The moose likes Beginning Java and the fly likes Objects and References....Constructors and new Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Objects and References....Constructors and new" Watch "Objects and References....Constructors and new" New topic
Author

Objects and References....Constructors and new

Sumesh Kumar T N
Greenhorn

Joined: Jan 26, 2005
Posts: 10
Please forgive my ignorance. But I would like to know the intricacies related to memory allocation and construction in java. please explain the relation between construction and memory allocation. also tell about the steps that are taken for an object to get created on the heap. what would happen if i simply assign the fields in an object reference like in case of structures in C. Also tell me the relevance of new operator. include the difference between the actual object content and the reference variable and the confusion scenarios that may crop up when we thinki deep into them......waiting for a reply that would erase all my doubts.....i am sorry if the question went too long
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
it's all automatic.
And no, you can't do anything with an object that hasn't been created.


42
Pauline McNamara
Sheriff

Joined: Jan 19, 2001
Posts: 4012
    
    6
Hi Sumesh, welcome to javaranch.

That's a lot of questions all at once. How about you pick out just one to start.

Which one would you most like to get help with?


Cheers,
Pauline


Asking Questions 101
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
I'll give you an overview from which you can use Google to get more information.
  • The new operator is used to instantiate an object.
  • Instantiation involves allocating memory on the heap, initializing fields, calling the object's constructor, and returning a reference.
  • A reference is just a pointer to the object in memory (like a pointer in C). The object in memory (the instance) holds the field values.

  • what would happen if i simply assign the fields in an object reference like in case of structures in C[?]
    A NullPointerException would be thrown by the JVM (similar to a core dump in C, but you can catch it and take action if you want). You must always set references to a non-null value before using them to access fields or call methods.
    Sumesh Kumar T N
    Greenhorn

    Joined: Jan 26, 2005
    Posts: 10
    Pleased to hear from you, Pauline. I would go for a single question. I would like to know about everything that happens right from when the new is called till creation of an object...........


    Thanks to everyone who cast light on the subject
    Sumesh Kumar T N
    Greenhorn

    Joined: Jan 26, 2005
    Posts: 10
    Group one:
    Integer one = new Integer(1);
    Integer two = new Integer(1);

    Group two:
    String one = new String("Hello");
    String two = new String("Hello");

    Please explain the above code for me as to how many objects are created on heap.Is there any difference between the two groups of statements???
    Lionel Badiou
    Ranch Hand

    Joined: Jan 06, 2005
    Posts: 140
    Hi Sumesh,

    The JVM manage the second group in a special way. Look at this thread for details.

    Best regards,


    Lionel Badiou
    CodeFutures Software
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Objects and References....Constructors and new